Hello all,
I have been working on a dataset that I am constantly expanding, adding new variables daily. I have some loops as well, where I have used "foreach" in some cases. One of my commands is like "foreach i of num 108/1 {" and does the job fine. The problem is I have a command like this in many parts of my do file, and the max value of the numlist range sometimes shall be 107 (so n-1). Now, everytime I add more variables, say 40 new columns, my previous command shall become "foreach i of num 148/1 {", but I have been doing this by hand (changing every value from say 108 to 148) everytime I added variables in every loop that is like that. This is of course not efficient. I have been wondering about using macros here, something like:
. global x = 108
. foreach i of num $x/1 {
But also (when I need n-1):
. foreach i of num $x-1/1 {
But this does not work, STATA reporting "invalid numlist".
Can someone help? I am quite a novice to loops and macros in general. Thanks in advance, regards.
Andrea
I have been working on a dataset that I am constantly expanding, adding new variables daily. I have some loops as well, where I have used "foreach" in some cases. One of my commands is like "foreach i of num 108/1 {" and does the job fine. The problem is I have a command like this in many parts of my do file, and the max value of the numlist range sometimes shall be 107 (so n-1). Now, everytime I add more variables, say 40 new columns, my previous command shall become "foreach i of num 148/1 {", but I have been doing this by hand (changing every value from say 108 to 148) everytime I added variables in every loop that is like that. This is of course not efficient. I have been wondering about using macros here, something like:
. global x = 108
. foreach i of num $x/1 {
But also (when I need n-1):
. foreach i of num $x-1/1 {
But this does not work, STATA reporting "invalid numlist".
Can someone help? I am quite a novice to loops and macros in general. Thanks in advance, regards.
Andrea
Comment