Hi All,
I am hoping to get some help with the following if possible. This code does not work and I get the error message "varlist not allowed".
I am trying to automate creation of variables that is done often. In this case I am categorising a count variable based on below the mean and above the mean and then it is labelled.
Basically the macro lists just needs to be adapted and then the code below in the loops runs accordingly.
My checks tells me that it breaks down at the very first line in the 2nd loop - egen vmax = max(`prfx'`var') + 1 but I am pretty sure the other lines are suspect too probably for the same reason - perhaps the way I have concatenated the two macros.
label def Cat_X_scale 0 "low values" 1 "high values"
local var_list "var_name" /* var_name is a count */
local prfx_list "x y z"
local newvar "var_name_cat"
local varlab "Categorised - scale"
local varval "Cat_X_scale"
foreach prfx in `prfx_list'{
foreach var in `var_list'{
egen vmax = max(`prfx'`var') + 1
local vmax2 = vmax
egen vmean = mean(`prfx'`var')
local vmean2 = ceil(vmean)
egen `prfx'`newvar' = cut(`prfx'`var'), at(0 `vmean2' `vmax2') icodes
label var `prfx'`newvar' `varlab'
label val `prfx'`newvar' `varval'
display " this is a check for `prfx'`newvar' & `prfx'`var' "
table `prfx'`newvar', contents(min `prfx'`var' max `prfx'`var')
drop vmax vmean
}
}
Any help would be appreciated and thank you in advance,
Don
I am hoping to get some help with the following if possible. This code does not work and I get the error message "varlist not allowed".
I am trying to automate creation of variables that is done often. In this case I am categorising a count variable based on below the mean and above the mean and then it is labelled.
Basically the macro lists just needs to be adapted and then the code below in the loops runs accordingly.
My checks tells me that it breaks down at the very first line in the 2nd loop - egen vmax = max(`prfx'`var') + 1 but I am pretty sure the other lines are suspect too probably for the same reason - perhaps the way I have concatenated the two macros.
label def Cat_X_scale 0 "low values" 1 "high values"
local var_list "var_name" /* var_name is a count */
local prfx_list "x y z"
local newvar "var_name_cat"
local varlab "Categorised - scale"
local varval "Cat_X_scale"
foreach prfx in `prfx_list'{
foreach var in `var_list'{
egen vmax = max(`prfx'`var') + 1
local vmax2 = vmax
egen vmean = mean(`prfx'`var')
local vmean2 = ceil(vmean)
egen `prfx'`newvar' = cut(`prfx'`var'), at(0 `vmean2' `vmax2') icodes
label var `prfx'`newvar' `varlab'
label val `prfx'`newvar' `varval'
display " this is a check for `prfx'`newvar' & `prfx'`var' "
table `prfx'`newvar', contents(min `prfx'`var' max `prfx'`var')
drop vmax vmean
}
}
Any help would be appreciated and thank you in advance,
Don
Comment