Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Question on forvalues


From   M Hollis <m73hollis_stata@yahoo.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Question on forvalues
Date   Mon, 22 May 2006 09:26:56 -0700 (PDT)

I believe the suggestion below is not quite right.  As
I understand it, there are two choices.  The first is
to simply replace forvalues with foreach in Shige's
code:

  foreach n in 1989 1991 1993 1997 2000 {
     replace waglevl_f`n' = . if waglevl_f`n'<0
     replace waglevl_m`n' = . if waglevl_m`n'<0
  }

Foreach will treat the year values as a list and
iterate through each element.  The second choice
follows Maarten's approach, but since the entire
variable name is invoked in the foreach line you have
to replace the entire variable name in the subsequent
commands, and in this case you only have to issue the
-replace- command once since it iterates through the
variables one at a time:

local varlist waglevl_f1989 waglevl_f1991
waglevl_f1993 /*
           */ waglevl_f1997 waglevl_f2000
waglevl_m1989 /*
           */ waglevl_m1991 waglevl_m1993
waglevl_m1997 /*
           */ waglevl_m2000

foreach var of varlist `varlist' {
	replace `var' = . if `var'<0
}

Matissa
--- Maarten buis <maartenbuis@yahoo.co.uk> wrote:
> Shige:
> -forvalues- expects a range not a list. If you want
> to loop over a list
> you should use -foreach-. See -help forvalues- and
> -help foreach- for
> more information. Correct code would be:
> 
> local varlist waglevl_f1989 waglevl_f1991
> waglevl_f1993 /*
>            */ waglevl_f1997 waglevl_f2000
> waglevl_m1989 /*
>            */ waglevl_m1991 waglevl_m1993
> waglevl_m1997 /*
>            */ waglevl_m2000
> 
> foreach var of varlist `varlist' {
> 	replace waglevl_f`n' = . if waglevl_f`n'<0
> 	replace waglevl_m`n' = . if waglevl_m`n'<0		
> }
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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