Say that I have numbers that I would like to loop over, but these numbers are nonconsecutive. What is the best way to loop over them?
I can, of course, list them individually:
I would prefer to include ranges, though, especially if I have hundreds (or, hypothetically thousands) of numbers. In pseudo code, I want to do something like this (borrowing some R syntax):
In the Stata documentation for forvalues, it says that this is allowed:
I tried a modified version, but I get an error:
What should I do?
I can, of course, list them individually:
Code:
foreach i in 1 3 5 6 9 10 12 16 18 19 20 21 23 24 25 26 27 28 29 30 32 33 34 35 36 37 38 39 40 41 42 45 46 47 { di "`i'" }
Code:
foreach i in c(1, 3, 5:6, 9:10, 12, 16, 18:21, 23:30, 32:42, 45:47) { di "`i'" }
Code:
forvalues k = 5 10 to 300 { summarize x`k' }
Code:
forvalues i = 1 3 5 6 9 10 12 16 18 to 21 23 to 30 32 to 42 45 to 47 { di "`i'" }
Comment