Hi All,
Assume I have two or more nominal variables, and I want to generate individual categorical (dummy) variables that account for the two original variables sequentially. So in the example data below, there are two original nominal data variables (shape and color), which have 3 and 4 unique levels, respectively. Thus, I would like the new dummy variables to be sequentially named (v1 - v7).
Using tab , gen() on the first variable creates the correct dummies and sequence, however, this will not work when implementing tab, gen() on the second (or thereafter) nominal variable(s)
The only thing I can think of is keeping track of the count of dummies generated r(r) and have a forvalues loop that adds to that in the second (and thereafter) variables. But that would still be problematic because I'd have to rename the variable first to have them consistent (e.g. r) and only then add on the numeric sequencing value...
Thanks in advance!
Ariel
Assume I have two or more nominal variables, and I want to generate individual categorical (dummy) variables that account for the two original variables sequentially. So in the example data below, there are two original nominal data variables (shape and color), which have 3 and 4 unique levels, respectively. Thus, I would like the new dummy variables to be sequentially named (v1 - v7).
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str10 shape str6 color "square" "blue" "round" "blue" "round" "red" "round" "red" "round" "blue" "round" "green" "square" "green" "square" "green" "round" "red" "round" "blue" "triangular" "yellow" "triangular" "red" end
Code:
local varlist shape color foreach v of local varlist { tabulate `v', generate(r) nofreq }
Thanks in advance!
Ariel
Comment