[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: AW: Flexible end of numlist

From   "Martin Weiss" <>
To   <>
Subject   st: AW: Flexible end of numlist
Date   Thu, 13 Aug 2009 09:31:06 +0200


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'


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Michael
Gesendet: Donnerstag, 13. August 2009 02:40
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"

Thanks for your help

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index