Hi everyone,
I'm writing a program which takes binary, categorical, and continuous variables in varlist. However, since the program I'm writing will need all k-level categorical variables converted to k-1 dummy variables, I'm currently trying to add this functionality to the program. Where I'm getting stuck is: I want the original categorical variable name removed from varlist and the new dummy variable names added to varlist. (I may eventually change it to have the user only enter the categorical variables in the option, to remove the step of having to remove it from varlist, but want to make it as general as possible right now.) Eventually, `varlist' will be passed to a Mata function for more processing. Below I've reproduced part of the Stata program:
As you can see, `varlist' still contains var1 and var10. Once I figure out the code, what I want it to contain is var1 var10_2 var10_3 var10_4. Thank you in advance for your help. I'm using Stata 14.
John
I'm writing a program which takes binary, categorical, and continuous variables in varlist. However, since the program I'm writing will need all k-level categorical variables converted to k-1 dummy variables, I'm currently trying to add this functionality to the program. Where I'm getting stuck is: I want the original categorical variable name removed from varlist and the new dummy variable names added to varlist. (I may eventually change it to have the user only enter the categorical variables in the option, to remove the step of having to remove it from varlist, but want to make it as general as possible right now.) Eventually, `varlist' will be passed to a Mata function for more processing. Below I've reproduced part of the Stata program:
Code:
capture program drop testing program define testing version 14 syntax varlist(min=1 numeric) [if] [in], [categorical(varlist)] marksample touse /* categorical variables */ if "`categorical'"!="" { local ncatvars: word count `categorical' tokenize `categorical' forval i=1/`ncatvars' { /* generate dummy variables */ tabulate ``i'', gen(``i''_) /* drop one of the dummy variables */ drop ``i''_1 /* count number of dummy variables */ local ``i''_num=r(r) - 1 forval j=2/```i''_num' { /* code will go here to add the dummy variables to `varlist' */ } /* code here to remove original categorical variables from varlist */ } } di "Varlist contains: `varlist'" end testing var1 var10, categorical(var10)
Code:
. testing var1 var10, categorical(var10) var10 | Freq. Percent Cum. ------------+----------------------------------- 1 | 4 22.22 22.22 2 | 3 16.67 38.89 3 | 4 22.22 61.11 4 | 7 38.89 100.00 ------------+----------------------------------- Total | 18 100.00 Varlist contains: var1 var10
John
Comment