Hi,
I have a dataset that looks like the below (except there are like 15 n_* variables).
I want to loop through all the variables beginning with n_ and create a new variable for each with the suffix "_average" that is divided by 5 (so looking like this):
How do I get to this? Right now I am trying this:
local a = r(varlist)
local b state
local c: list a-b
foreach x in local c {
gen `x'_average=`x'/5
}
I get the error that " is not a valid command name". I've tried a bunch of different ways, and am just not coming up with an answer, I know there are a bunch of different ways to do it, but I just need one to work, haha.
Thank you in advance!!
I have a dataset that looks like the below (except there are like 15 n_* variables).
state | n_women | n_men | n_dogs |
TX | 20 | 25 | 40 |
VA | 30 | 10 | 10 |
NY | 20 | 15 | 25 |
CA | 10 | 5 | 15 |
state | n_women | n_men | n_dogs | n_women_average | n_men_average | n_dogs_average |
TX | 20 | 25 | 40 | 4 | 5 | 8 |
VA | 30 | 10 | 10 | 6 | 2 | 2 |
NY | 20 | 15 | 25 | 4 | 3 | 5 |
CA | 10 | 5 | 15 | 2 | 1 | 3 |
local a = r(varlist)
local b state
local c: list a-b
foreach x in local c {
gen `x'_average=`x'/5
}
I get the error that " is not a valid command name". I've tried a bunch of different ways, and am just not coming up with an answer, I know there are a bunch of different ways to do it, but I just need one to work, haha.
Thank you in advance!!

Comment