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
Annual and Monthly Stock PricesDear All Hope you are well. I am working on some share prices and I would really appreciate some h…
Issues with unbalanced Panel DataHello everyone: I have a few questions concerning the choices I have to make with respect to unbala…
Maximum value for each quarterDear all, I'm stuck with something that I think you may help me : ) I have a data set on a monthly b…
Get latitude using IP addressHi all, I want to pull latitude/longitude from IP addresses using: Code: geocode_ip ip_address, …
Lead and Lag Variables in RegressionsHi All I have an example of a regression model with leads and lags, and I was wondering if I am cor…
Subscribe to:
Post Comments (Atom)
0 Response to Foreach and forevalues loop
Post a Comment