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
Generating new variable using subtractionHello! I have data on the year that employees joined a company, startyear. In order to figure out th…
Compare observation to previous ones: Patent DataHello everyone I have an extensive dataset about individual patents (patnum), belonging to a techno…
Count with nearstatHi all, My dataset is made up of gasoline stations located by latitude and longitude. These stations…
Generating dummy variables using non-numerical dataHello. I am working with survey data where participants are asked if they agree with a statement, in…
Rendering of Stata homepage in MS EdgeI just noticed that my company browser doesn't render the Stata homepage properly. There is a huge "…
Subscribe to:
Post Comments (Atom)
0 Response to Foreach and forevalues loop
Post a Comment