I am fairly new to Stata, but I am used to Python and R where something like this seems easier to do. Basically, I want to iterate over each variable and rename it the value of its first and second row. For example, if the data looked like this:
I want to transform it to this:
So far, I have a loop that looks like so:
unab varlist : *
foreach i of loc varlist {
display `i'[1]
}
Which displays the value of each first row. I am confused on how you rename based on those values though. If I try the following to just name it by the first row:
unab varlist : *
foreach i of loc varlist {
rename `i' `i'[1]
}
It does not appear to work. Any help would be greatly appreciated!
A1 | A2 | A3 | A4 | ... |
C99 | C92 | C91 | C30 | ... |
J | J | K | K |
C99J | C92J | C91K | C30K | |
C99 | C92 | C91 | C30 | ... |
J | J | K | K | ... |
So far, I have a loop that looks like so:
unab varlist : *
foreach i of loc varlist {
display `i'[1]
}
Which displays the value of each first row. I am confused on how you rename based on those values though. If I try the following to just name it by the first row:
unab varlist : *
foreach i of loc varlist {
rename `i' `i'[1]
}
It does not appear to work. Any help would be greatly appreciated!
Comment