In my dataset I have 4 "characteristics" (like income, type of degree etc.) for 3 individuals. In the datafile, the variables are numbered consecutively, say var1-var12. So var1 is the first characteristic for the first individual, var2 is the second characteristic for the first individual, and var10 is the second characteristic for the third individual.
I want to assign labels to a subset of the characteristics (say the 1st, 3rd and 4th) for all the individuals. I therefore defined 3 labels, lbl_var1, lbl_var3 and lbl_var4. Now I tried to set up a loop for this purpose, but it didn't work out. Here is what I tried at first:
local itemlist "1 3 4"
foreach i of local itemlist {
foreach j of numlist 0(1)2 {
label values var(`i' + `j'*4) lbl_var`i'
}
}
This apparently doesn't work out, because Stata then reads only the part inside the brackets (`i' + `j'*4) as variable name. I get the error "1 invalid name".
I then tried a workaround for this problem. In a first step I wanted to set up a local itemlist that contains all the numbers of the variables to be labeled, and then use this in a loop to label the variables. This is what I did:
local itemlist "1 3 4"
foreach j of numlist 0(1)2 {
foreach i of local itemlist {
local itemlist2 `itemlist2' `i' + `j' * 4
}
}
foreach i of local itemlist {
foreach j of local itemlist2 {
label values var`j' lbl_var`i'
}
}
But this didn't work as well. The setup of the local doesn't seem to work, I get the error "variable var not found"
Can someone tell me, how I can set up a nested loop over variable names as in this example?
Note: The number of variables I have is actually much larger. I kept the numbers small in this example to keep things simple.
I want to assign labels to a subset of the characteristics (say the 1st, 3rd and 4th) for all the individuals. I therefore defined 3 labels, lbl_var1, lbl_var3 and lbl_var4. Now I tried to set up a loop for this purpose, but it didn't work out. Here is what I tried at first:
local itemlist "1 3 4"
foreach i of local itemlist {
foreach j of numlist 0(1)2 {
label values var(`i' + `j'*4) lbl_var`i'
}
}
This apparently doesn't work out, because Stata then reads only the part inside the brackets (`i' + `j'*4) as variable name. I get the error "1 invalid name".
I then tried a workaround for this problem. In a first step I wanted to set up a local itemlist that contains all the numbers of the variables to be labeled, and then use this in a loop to label the variables. This is what I did:
local itemlist "1 3 4"
foreach j of numlist 0(1)2 {
foreach i of local itemlist {
local itemlist2 `itemlist2' `i' + `j' * 4
}
}
foreach i of local itemlist {
foreach j of local itemlist2 {
label values var`j' lbl_var`i'
}
}
But this didn't work as well. The setup of the local doesn't seem to work, I get the error "variable var not found"
Can someone tell me, how I can set up a nested loop over variable names as in this example?
Note: The number of variables I have is actually much larger. I kept the numbers small in this example to keep things simple.
Comment