Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: RE: inexplicable foreach problem


From   Joerg Luedicke <joerg.luedicke@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: inexplicable foreach problem
Date   Thu, 14 Jul 2011 00:03:41 -0400

Yes, this is a -forvalues- loop, like:

forval i=2005/2010 {
...
}

On Wed, Jul 13, 2011 at 11:34 PM, Millimet, Daniel
<millimet@mail.smu.edu> wrote:
> Given that you are looping over values, why not try forvalues instead?
>
> *******************************************
> Daniel L. Millimet, Professor
> Department of Economics
> Box 0496
> SMU
> Dallas, TX 75275-0496
> phone: 214.768.3269
> fax: 214.768.1821
> web: http://faculty.smu.edu/millimet
> *******************************************
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Jennifer S. Earl
> Sent: Wednesday, July 13, 2011 10:29 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: inexplicable foreach problem
>
> I am hoping someone else can give me some perspective on this. I have a
> program in which I am using lots of foreach statements. For instance, here
> is one that works (statement first and then log):
>
> Programming (pardon the wrapping text if you email program wraps the line):
>
> foreach i in 2005/2010 {
>        replace mod1_err013=eventid if population_`i'==. & sample_rate_`i'!=.
>        replace mod1_err013=eventid if population_`i'!=. & sample_rate_`i'==.
>        replace mod1_err013=eventid if population_`i'==. & man_weight_`i'!=.
>        replace mod1_err013=eventid if population_`i'!=. & man_weight_`i'==.
>        replace mod1_err013=eventid if sample_rate_`i'==. & man_weight_`i'!=.
>        replace mod1_err013=eventid if sample_rate_`i'!=. & man_weight_`i'==.
> }
>
> Successful log entry:
> . foreach i in 2005/2010 {
>  2.         replace mod1_err013=eventid if population_`i'==. &
> sample_rate_`i'
>> !=.
>  3.         replace mod1_err013=eventid if population_`i'!=. &
> sample_rate_`i'
>> ==.
>  4.         replace mod1_err013=eventid if population_`i'==. &
> man_weight_`i'!
>> =.
>  5.         replace mod1_err013=eventid if population_`i'!=. &
> man_weight_`i'=
>> =.
>  6.         replace mod1_err013=eventid if sample_rate_`i'==. &
> man_weight_`i'
>> !=.
>  7.         replace mod1_err013=eventid if sample_rate_`i'!=. &
> man_weight_`i'
>> ==.
>  8. }
> (0 real changes made)
> (0 real changes made)
> (0 real changes made)
> (2 real changes made)
> (0 real changes made)
> (0 real changes made)
>
>
> But, when I try to execute exactly the same foreach line after that, the
> program doesn't try to iterate between 2005-2010;
>
> Unsuccessful programming:
>
> foreach i in 2005/2010 {
>        gen plus3000_`i'=0
>        replace plus3000_`i'=1 if sample_rate_`i'==98
>        replace plus3000_`i'=2 if sample_rate_`i'==99
> }
>
> Here's what the trace shows is happening when it breaks (which reveals the
> new variable is being named plus3000_2005/2010 instead of plus3000_2005):
>
> . foreach i in 2005/2010 {
>  2.
> . gen plus3000_`i'=0
>  3.
> . replace plus3000_`i'=1 if sample_rate_`i'==98
>  4.
> . replace plus3000_`i'=2 if sample_rate_`i'==99
>  5.
> . }
> - foreach i in 2005/2010 {
> - gen plus3000_`i'=0
> = gen plus3000_2005/2010=0
> / invalid name
>  replace plus3000_`i'=1 if sample_rate_`i'==98
>  replace plus3000_`i'=2 if sample_rate_`i'==99
>  }
> r(198);
>
> I know its not a typo because I have copied and replaced the foreach line
> from the good programming about 10 times to the same result. The only thing
> in between the foreach loops that work and those that don't are some
> comments and some egen statements.
>
> Any ideas on this one-- its a total mystery to me!
>
> Thanks,
>
> Jenn
>
> *********************************
> Jennifer Earl
> Professor of Sociology
> SS&MS 3129
> University of California, Santa Barbara
> Santa Barbara, CA  93106-9430
>
> P: (805) 893-7471
> F: (805) 893-3324
> http://www.soc.ucsb.edu/faculty/earl/
> *********************************
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index