Especially in the process of data preparation, but also when one runs whole sets of analysis, we start repeating commands and sets of commands for similar variables. For example in one of my projects, I had to process salary information, that was monthly in wide format, and for several reasons I could not use reshape:
I could have typed:
gen str10 v201_1=""
replace v201_1=c2 if c1=="201"
gen str10 v201_2=""
replace v201_2=c3 if c1=="201"
[...]
gen str10 v201_12=""
replace v201_12=c13 if c1=="201"
Note that there are two numbers, the first one identifying the month (1,2,…,12), the other identifying the variable where the information is (being in this case c2-c13). The elegant way to solve this is the use of loops:
forvalues m=1/12 {
gen str10 v201_`m'=""
local c=`m'+1
replace v201_`m'=c`c' if c1=="201"
}
Leave a Reply
You must be logged in to post a comment.