Hello all,
I am trying to change many variables in a time series data set, and would ideally like to do it in a manner such as "replace var* = var*[_n-1] if..." where the variables to be changed are var1 var2 var3 etc.
But Stata does not allow such use of the wildcard * in this context, and I understand why.
Is there a workaround? Perhaps using language such as foreach or forvalues, but I haven't found a way to do it with those commands yet.
For what it's worth, a manual version of what I am trying to do, that I would need to do for each of the hundreds of variables (intervention1, intervention2, etc.), looks like this:
by ccode: replace intervention6 = intervention6[_n-1] if intervention6[_n-1]!=0 & intervention6[_n-1]!=.
by ccode: replace intervention6 = 0 if startyear>intervention6
replace intervention6 = 1 if intervention6!=0
Where the unit of analysis is country-year, ccode=country code, and each variable indicates an intervention involving the country.
Thanks in advance.
I am trying to change many variables in a time series data set, and would ideally like to do it in a manner such as "replace var* = var*[_n-1] if..." where the variables to be changed are var1 var2 var3 etc.
But Stata does not allow such use of the wildcard * in this context, and I understand why.
Is there a workaround? Perhaps using language such as foreach or forvalues, but I haven't found a way to do it with those commands yet.
For what it's worth, a manual version of what I am trying to do, that I would need to do for each of the hundreds of variables (intervention1, intervention2, etc.), looks like this:
by ccode: replace intervention6 = intervention6[_n-1] if intervention6[_n-1]!=0 & intervention6[_n-1]!=.
by ccode: replace intervention6 = 0 if startyear>intervention6
replace intervention6 = 1 if intervention6!=0
Where the unit of analysis is country-year, ccode=country code, and each variable indicates an intervention involving the country.
Thanks in advance.
Comment