# Re: st: AW: Flexible end of numlist

 From "Eric A. Booth" To statalist@hsphsun2.harvard.edu Subject Re: st: AW: Flexible end of numlist Date Thu, 13 Aug 2009 09:45:28 -0500

MIchael may not want to pass the -forvalues- loop a value from another -local-, in which case using " `=_N' " could do the trick:
```
*****

forvalues i=2/`=_N'  {
di in red `i'
}
*****

Best,

Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754
Fax: +979.845.0249

On Aug 13, 2009, at 2:31 AM, Martin Weiss wrote:

```
```
<>

```
The help advises against this use of the -foreach- loop structure. See -h
```foreach-:" If you wish to loop over many equally spaced values, do not
code...", instead use -forvalues-. Apart from that, you can have Stata
```
evaluate an -expression- as the endpoint of a -numlist-, in this particular
```case half the -local- "r":

*************
local r 10

forv i=1/`=`r'/2'{
di in red `i'
}
*************

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Michael
Stuetzer
Gesendet: Donnerstag, 13. August 2009 02:40
An: statalist@hsphsun2.harvard.edu
Betreff: st: Flexible end of numlist

Dear stata listers,

```
I need to program a loop and want to use the "foreach i of numlist 1/10" begin. However I want to have the end of numlist not to be fixed, but more flexible. To be more precise, the end of the numlist should be half the number of the variables involved in that loop. Does anyone have an idea how
```to do that?

```
The variables which are involved in that loop are for instance pat_se1 pat_se2 pat_se3 pat_se4 and contain the month (pat_se[odd]) and the years (pat_se[even]) of a specific business starting activity. I have here a
```first draft of that loop

foreach i of numlist 1/2 {
local j=`i'*2
local k=`j'-1
local t=round(`i'/2)
gen PATENTS_SH`i' = ym(pat_se`j', pat_se`k')
format PATENTS_SH`i' %tmn_CY
if mod(`i',2)==0 {
lab var PATENTS_SH`i' "Patenting, end `t'. spell, real time"
}
else lab var PATENTS_SH`i' "Patenting, start `t'. spell, real time"
}

Michael

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```