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]

From |
Sergiy Radyakin <serjradyakin@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: problem with forvalues loop |

Date |
Tue, 25 Feb 2014 11:45:22 -0500 |

Holly, these lines: gen retgrade`s'13=retgrade`y' if resubj`y'==`s' & rect`s'13==1 gen resc`s'13=resc`y' if resubj`y'==`s' & rect`s'13==1 of your code will be executed 10 times (y-loop) with identical LHS. They are expected to fail for y=2 (second iteration of the y-loop). Best, Sergiy On Tue, Feb 25, 2014 at 11:37 AM, Holly Heard <hheard@rice.edu> wrote: > Dear Statalisters (apologies for long post): > > I have data on exams that high school students take in 2013. Each student > can take more than one exam. The data were originally long (one observation > per exam), but I reshaped it wide, so that I have one observation per > student. Variables pertaining to each exam are numbered 1 to 10, based on > the maximum number of exams taken by a student (most students took 3-4 > exams, but one student took 10). There are multiple variables based on each > exam number: retgrade1-retgrade10 (grade student was in when took exam), > resc1-resc10 (scale score of exam), etc. I need to reorganize and rename > variables based on the subject of the exam (algebra, reading, etc.), which > is denoted by resubj1-resubj10, which is the subject category of the exam > (1=Algebra I, 2=Algebra 2, 4=Chemistry). There are 15 exam categories, > although as noted, a maximum of 10 exams were taken by any one student. > > I created a forvalues loop to go through each variable denoted by exam > number, and generate new variables based on subject number. My code is: > > forvalues y=1/10 { > forvalues s=1/15 { > if `s'<10 { > local s 0`s' > } > gen retgrade`s'13=retgrade`y' if resubj`y'==`s' & rect`s'13==1 > gen resc`s'13=resc`y' if resubj`y'==`s' & rect`s'13==1 > . > > } > } > > Here, y denotes exam number 1-10, and s denotes subject number 1-15. I add > 13 to the end of the new variables to denote the year of data collection, > and to distinguish new vars from old vars. It is possible for a student to > have taken an exam for a particular subject twice (if they didn't pass the > first time), so I create new variables only if the student took the subject > exam only once (rect`s'13==1). > > This loop runs many times, based on the many *(23851 missing values > generated)* statements I get, but then I get the following error: > > retgrade0113 already defined > r(110); > > So apparently the loop repeats itself at some point. I realize that y and s > are not fully nested (that would be too easy), but I'm not sure how else to > write the code to do what I want to do. I tried reversing the order of the > forvalues commands (y within s), but get the same error. As far as I can > tell, I should only have one retgrade0113 as long as rect0113==1. Can anyone > tell me what I'm doing wrong and how to fix it? Thanks, Holly > > Holly E. Heard, Ph.D. > Senior Social Scientist > HERC Data Specialist > Houston Education Research Consortium, MS-28 > Rice University > 6100 Main Street > Houston, TX 77005 > Phone: 713-348-3415 > Fax: 713-348-5296 > > > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: problem with forvalues loop***From:*"Holly Heard" <hheard@rice.edu>

- Prev by Date:
**Re: st: Stripping ASCII characters** - Next by Date:
**Re: st: problem with forvalues loop** - Previous by thread:
**st: problem with forvalues loop** - Next by thread:
**Re: st: problem with forvalues loop** - Index(es):