Hi all,
I am trying to automate a process using a loop to run X number of times, where X is equal to the value of a specific variable that has been created. This number needs to vary based on the value of the variable in each dataset that gets created in different sections of my .do file. I've tried the following code, but Stata does not recognize the value of `n' below, even though it is stored in the local macro. Note that 'var1' is a variable I generated that only has the same value in all rows.
The content of the loop is somewhat irrelevant here--basically it's a process that creates different variables based on row numbers etc. -- but what I'm interested in is, how can you program a loop to run a set number of times, equal to the value of a specific variable (and/or cell).
Thanks in advance!
Cheers,
John
I am trying to automate a process using a loop to run X number of times, where X is equal to the value of a specific variable that has been created. This number needs to vary based on the value of the variable in each dataset that gets created in different sections of my .do file. I've tried the following code, but Stata does not recognize the value of `n' below, even though it is stored in the local macro. Note that 'var1' is a variable I generated that only has the same value in all rows.
Code:
local n (var1 - 1) forvalues i = 1/`n' { bysort var1: replace var2 = 1 if _n == 1 & var3 == 1 egen var5 = total(var4), by(id) replace var4 = var5 if var5 == 1 drop var5 }
Thanks in advance!
Cheers,
John
Comment