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: problem with forvalues loop
From
"Holly Heard" <[email protected]>
To
<[email protected]>
Subject
RE: st: problem with forvalues loop
Date
Tue, 25 Feb 2014 11:29:45 -0600
That worked, thanks for the simple solution that I wish I had figured out
myself. And thanks for the shorter code to add 0 to my `s'. Best, HH
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
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: Tuesday, February 25, 2014 10:49 AM
To: [email protected]
Subject: Re: st: problem with forvalues loop
Apart from advising -reshape long-, I recommend considering something like
this
forval s = 1/15
local S : di %02.0f `s'
gen retgrade`S'13 = .
gen resc`S'13 = .
forval y=1/10 {
replace retgrade`S'13=retgrade`y' if resubj`y'==`s' &
rect`S'13==1
replace resc`S'13=resc`y' if resubj`y'==`s' & rect`S'13==1
.}
}
Nick
[email protected]
On 25 February 2014 16:37, Holly Heard <[email protected]> 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/
!DSPAM:1516,530cc9c1233834692841240!
*
* 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/