Hi,
I have a list of variables that look as follows: max_1 max_2 max_3. All the numbers in the variables correspond to a certain colour e.g. 1==blue, 2==yellow, 3==orange.
What I want to do is:
gen blue=1 if max_1!=.
gen yellow=1 if max_2!=.
gen orange=1 if max_3!=.
and so on.
I have tried to do this with a loop as follows:
local colours "blue yellow orange"
foreach colour of local colours{
forvalues i = 1(1)3{
preserve
gen `colour'_present = 1 if max_`i'!=.
restore
}
}
however, this does not work because it keeps on looping. Is there a way around this/ or to achieve the same result?
Thanks
Related Posts with Foreach and forevalues loop
unit root test short panelHi guys, I was wondering if it is possible to test for unit root on a T=2 panel dataset. I only have…
shortcut referring to independent variables for regressionHi STATA, I have a dataset with about 400 independent variables and 4 mill rows. I want to run a lo…
Help required with identifying observations between two datasetsHello, I am working with a dataset where I have identified 7,286 observations. These observations b…
Drop panel based on percentage missing rows (panel data)Hi stata forum, I would like to delete the whole firm if revenue is missing for more than or equal …
dynamic GMM analysis and confounder problemHi everyone, I am conducting research using the dynamic system GMM analysis. My model specification…
Subscribe to:
Post Comments (Atom)
0 Response to Foreach and forevalues loop
Post a Comment