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
Margins for DiD return not estimable!Hi everyone, I am evaluating the effect of Grab's entry in Vietnam on firm performance indicators s…
areg cluster identificationHello experts of Statalist, Sorry in advance for my inexperience. I am running "areg" for a panel d…
kappaetc: Testing for signficance of a difference in before / after interventionI am using kappaetc (SSC) Inter-rater reliability, comparing diagnostic agreement before vs. after a…
Creating long format time series from start and end date in wide formatHello, I have a data set on judges with a term start year and term end year. I would like to have a…
Simple commands not working....wonder why (Stata/IC 15.1)Hi, I wanted to run a simple command for moving one of my variables called "comment" to the end of …
Subscribe to:
Post Comments (Atom)
0 Response to Numlist in foreach loop, when number is within variable name
Post a Comment