Hello Stata Lovers,
I'm wondering whether there is a way to generate new variables that capture data from merges of one to many. I would also want to make sure the variables are in a certain order. Here is an example:
merge 1:m id using "document.dta", keepusing(year_1 amount_1)
after the merge lets say id==1 has multiple observations as follows:
Currently, in order to sort by date and generate new variables with endings _2 and _3, I do the following:
sort year
gen year_2=.
gen year_3=.
gen amount_2=.
gen amount_3=.
replace year_2=2000 if id==1
replace year_3=2002 if id==1
replace amount_2=50 if id==1
replace amount_3=100 if id==1
duplicates drop id, force
This process is very time-consuming, especially when an id has as many as 37 observations. Is there a quicker way of automatically generating these additional variables without having to manually input year and amount data for each observation?
Thanks in advance for any help with this!!
Kind Regards,
Alyse
I'm wondering whether there is a way to generate new variables that capture data from merges of one to many. I would also want to make sure the variables are in a certain order. Here is an example:
merge 1:m id using "document.dta", keepusing(year_1 amount_1)
after the merge lets say id==1 has multiple observations as follows:
id | year_1 | amount_1 |
1 | 2000 | 100 |
1 | 2002 | 50 |
1 | 1998 | 20 |
sort year
gen year_2=.
gen year_3=.
gen amount_2=.
gen amount_3=.
replace year_2=2000 if id==1
replace year_3=2002 if id==1
replace amount_2=50 if id==1
replace amount_3=100 if id==1
duplicates drop id, force
This process is very time-consuming, especially when an id has as many as 37 observations. Is there a quicker way of automatically generating these additional variables without having to manually input year and amount data for each observation?
Thanks in advance for any help with this!!
Kind Regards,
Alyse