This week’s blog will feature a set of Stata tricks we used to addresses a particular issue that we encountered in our dataset. Many of the variables were in string form and were not useable for Stata analysis. Furthermore, the values of the variables were not in the correct order for our purposes. A couple of commands came in handy here. Loops are useful for many different repetitive commands. They allowed us to quickly recode the values of a set of variables that have similar categorical values and also enabled us to destring sets of variables, setting them to numeric values. These numeric values were in turn reordered to fit a desired pattern. Finally, the labels for the numeric values were recoded to appear as the original text instead of just “1, 2, 3, etc”.
The first step is to set the labels for the values using “lab de” followed by the specific value labels (see Figure B). Next we set a loop that will destring the variables by making a new set of variables. The “foreach” command begins a loop. The variables that follow after the word varlist (D201_education_serv, D202_basic_health_serv, D203_transportation D204_security_pub_safe D205_H20_sani_elec, D206_roads D207_employment D208_housing D209_cult_sport_leis, D210_basic_hum_relief, and D211_concern_and_priorit) are de-stringed using the “encode” command and a new variable is generated. The new variable corresponds to the temporary variable created in the “tempvar” command. The original variable is referenced by using `var’ and the new variable is created by adding 1 to the end of `var’ . Then a new variable is made ~var’2 and set to equal zero.
Figure A
The values of the variables are then reordered according to the pattern in the “replace” commands section. After that we drop the temporary variable `var’1 and the original variable `var’. Then we rename the `var’2 with the name of the dropped original variable `var’. Finally, we apply the original variable labels from the “lab de” command at the beginning and tie it to the newly destringed variable.