Hi All,
I am having a brain freeze with this parallel double loop.
I have a variable (shape) with nominal level data, and I want to (1) generate the same number of dummy variables as there are unique levels of shape, (2) populate each dummy variable with a 1 if the value in shape matches the level (in order) produced by levelsof, or 0 if not.
With the data below, there are three categories, "round", "square", and "triangular". Thus, 3 dummy variables are produced. The first complete loop works as expected, that is, in shape[1] the value is "square" and it is assigned correctly to dummy variable 2 while the other two dummies get coded as 0.
The problem starts at the next loop, where it appears that the first category ("round") is not assessed, and the code moves on to "square". My guess is that it has to do with the line - local ++cat - which moves forward one level at each loop rather than resetting back to "round"
Any help would be appreciated!
Ariel
I am having a brain freeze with this parallel double loop.
I have a variable (shape) with nominal level data, and I want to (1) generate the same number of dummy variables as there are unique levels of shape, (2) populate each dummy variable with a 1 if the value in shape matches the level (in order) produced by levelsof, or 0 if not.
With the data below, there are three categories, "round", "square", and "triangular". Thus, 3 dummy variables are produced. The first complete loop works as expected, that is, in shape[1] the value is "square" and it is assigned correctly to dummy variable 2 while the other two dummies get coded as 0.
The problem starts at the next loop, where it appears that the first category ("round") is not assessed, and the code moves on to "square". My guess is that it has to do with the line - local ++cat - which moves forward one level at each loop rather than resetting back to "round"
Any help would be appreciated!
Ariel
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str10 shape "square" "round" "round" "round" "round" "round" "square" "square" "round" "round" "triangular" "triangular" end
Code:
levelsof shape, local(levs) local cnt : word count `levs' forvalues i = 1/`cnt' { gen dummy`i' = 0 } local x = 1 forvalues cat = 1/`cnt' { forvalues col = 1/`cnt'{ replace dummy`col' = 1 if shape[`x'] == "`:word `cat' of `levs''" in `x' local ++cat } local x = `x' + 1 }
Comment