I have been trying to use ‘numlist’ in a foreach loop as I have a list of about 100 variables, all repeated 8 times (as if in a longitudinal study) and the only difference in each variable name is the numbers 1 to 8.
Example variables:
v1_msg v2_msg v3_msg v4_msg v5_msg v6_msg v7_msg v8_msg
v1_age v2_age v3_age v4_age v5_age v6_age v7_age v8_age
And I’m using the code:
foreach num in numlist 1 2:8 {
tab v`num'_msg, m
tab v`num'_age, m
}
I got the error r(111) "variable vnumlist_msg not found"
I then added a display command first to see if my loop was even working:
foreach num in numlist 1 2:8 {
di `num'
tab v`num'_msg, m
tab v`num'_age, m
}
And I got the error r(111) "numlist not found"
My question is, is this possible with this command? And if not - how can I achieve this outcome without repeatedly typing each variable name 8 times. I also want to do more then just tabulate each variable in foreach.
I have also tried using variable wild cards such as:
tab v*_msg
tab v#_msg
These also gives me the errors: r(103) "too many variables specified" and then r(111) "v ambiguous abbreviation" (respectively).
Thank you for your help!
Victoria
Related Posts with Numlist in foreach loop, when number is within variable name
range function generating over 400 missing valuesHello, I am trying to make a Stata variable that stores 20 numbers between 0 and 1. I am using the …
Is it possible to use the Stata-python integration module inside of foreach Stata loop?Hello, I am trying to execute the code below and it is giving me an error, mainly because the Stata…
Fixed effects (in Accelerated Failure Time Survival Model), Cross-sectionalHi, For my research I am examining private equity strategies and their subsequent exit-types. My da…
stata command unrecognized r(199) errorHello, I am trying to run the Stata code below, and everything runs except at the very end I am gett…
Fixed effects (in Accelerated Failure Time Survival Model)Hi, For my research I am examining private equity strategies and their subsequent exit-types. My da…
Subscribe to:
Post Comments (Atom)
0 Response to Numlist in foreach loop, when number is within variable name
Post a Comment