I'm trying to generate a new set of variables from two parallel lists in Stata. I have a set of columns that are the percentage of each demographic group in the labor force and another set of columns that are the percentage of each demographic group in a particular occupation. I want to divide the latter by the former to create a crowding score for each group.
local pct_lf "pct_lf_tot_Male-pct_lf_tot_o_f_65_plus
* pct in labor force by demo group
local pct_occ "pct_Male-pct_o_f_65_plus"
* pct in occupation by demo group
local n : word count `pct_occ'
forvalues i = 1/`n' {
local a : word `i' of `pct_lf'
local b : word `i' of `pct_occ'
gen crd_`b'=`b'/`a'
}
There's a syntax error with this code which I haven't figured out. I am wondering is there another way to do this? Generate a new variable by dividing a list of variables by another list of variables in order as parallel lists?
The code is incorrect. word
# of
will not unabbreviate varlists.
The rules on words are that words are separated by spaces, except that quotation marks bind. The exception is irrelevant here. Thus each wildcard varlist is precisely one word.
. display `: word count "pct_lf_tot_Male-pct_lf_tot_o_f_65_plus"'
1
We can test nothing for real without your data, but a confident guess is that you need something more like
unab pct_lf : pct_lf_tot_Male-pct_lf_tot_o_f_65_plus
* pct in labor force by demo group
unab pct_occ : pct_Male-pct_o_f_65_plus
* pct in occupation by demo group
local n : word count `pct_occ'
forvalues i = 1/`n' {
local a : word `i' of `pct_lf'
local b : word `i' of `pct_occ'
gen crd_`b'=`b'/`a'
}