Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE:st: RE: Trouble with -forval- and -gen- new variables -solution


From   "Deborah Garvey" <[email protected]>
To   <[email protected]>
Subject   RE:st: RE: Trouble with -forval- and -gen- new variables -solution
Date   Fri, 15 Aug 2003 12:51:12 -0700

Thanks to Thomas Steichen for his suggestion (below).  Oddly enough, Stata doesn't appear to care whether a delimiter is present after the -forval- command or after the command initially following -forval-.

My error was in the use of the -gen- statement.  I needed to initialize the variables to 0 for all observations and then replace with time trend values for the appropriate group/year combination.  My old code (naturally) set the time trend variables to missing except for the particular group/year combination to which the -gen- condition applied.

The correct -forval- loop is below.

Best,

Deb Garvey

forval i = 1/52 {
        gen time`i'  = 0;
        gen timep`i' = 0;
        replace time`i' = year - 74 if group == `i';
       replace timep`i' = (year - 79)*(year >=80) if group == `i';
 };


"Steichen, Thomas" <[email protected]> wrote:

It looks like there is a missing ; after line 
 
  . forval i = 1/`r(max)' {


> -----Original Message-----
> From: Deborah Garvey [mailto:[email protected]] 
> Sent: Friday, August 15, 2003 2:49 PM
> To: [email protected]
> Subject: st: Trouble with -forval- and -gen- new variables
> 
> 
> Hi, all.
> 
> I hate to trouble the list with such a simple problem, but I 
> can't figure out what is wrong with my code.  I am attempting 
> to generate a series of state-specific and national time 
> trends for a 12-year panel of data using -forval- like an 
> obedient Stata user instead of the now-defunct -for- command.
> 
> I am running Intercooled Stata 7.0 for Win98/95/NT (Born:  11 
> June 2002). Annotated log file follows:
> 
> use "D:\school finance centralization\data\StatAbstract 
> data\stateexpY7586";
> 
> . keep fipsst pubst state tsrgex* rexcap* rgexed* rgexpw* 
> rcapy* rpery* 
> >  rexk12* k12pct* rppe*;
> 
> . unab vars: *79;
> 
> . local stubs: subinstr local vars "79" "", all;
> 
> .  reshape long `stubs', i(fipsst) j(year);
> (note: j = 75 76 77 78 79 80 81 82 83 84 85 86)
> (note: k12pct75 not found)
> (note: rexk1275 not found)  -->  OK.  Missing exp data in 1975
> (note: rppe75 not found)
> 
> Data                               wide   ->   long
> --------------------------------------------------------------
> ---------------
> Number of obs.                       52   ->     624
> Number of variables                 108   ->      13
> j variable (12 values)                    ->   year
> xij variables:
>          k12pct75 k12pct76 ... k12pct86   ->   k12pct
>          tsrgex75 tsrgex76 ... tsrgex86   ->   tsrgex
>          rexcap75 rexcap76 ... rexcap86   ->   rexcap
>          rgexed75 rgexed76 ... rgexed86   ->   rgexed
>          rgexpw75 rgexpw76 ... rgexpw86   ->   rgexpw
>             rcapy75 rcapy76 ... rcapy86   ->   rcapy
>             rpery75 rpery76 ... rpery86   ->   rpery
>          rexk1275 rexk1276 ... rexk1286   ->   rexk12
>                rppe75 rppe76 ... rppe86   ->   rppe
> --------------------------------------------------------------
> ---------------
> 
> . gen lrppe = ln(rppe);
> (52 missing values generated)
> 
> . egen group = group(fipsst), missing;
> 
> . su group, meanonly;
> 
> . di `r(max)';
> 52
> 
> . forval i = 1/`r(max)' {
> >   gen time`i'= year - 74 if group == `i';
>   2.   gen timep`i' = (year - 79)*(year >=80) if group == `i';
>   3. };
> (612 missing values generated)
> [<<snip>> Repeated 104 times (=52 groups *2 vars)]
> 
> Summing by group & year shows that time* and timep* are 
> non-missing, valid only for group 1.  For all other 
> groups/year combinations, both variables are missing (=51*12 = 612).
> 
> . by group year: su time1 timep1 year fipsst;
> 
> ____________________________________________________________
> -> group = 1, year = 75
> 
>     Variable |     Obs        Mean   Std. Dev.       Min        Max
> -------------+-----------------------------------------------------
>        time1 |       1           1          .          1          1
>      timep1 |       1           0          .          0          0
>          year |       1         75          .         75         75
>         fipsst |       1           1          .          1          1
> 
> <<snip>>
> 
> What am I doing wrong?  It doesn't matter if I substitute the 
> number of groups (52) for the `rmax' saved result in the 
> -forval- command.
> 
> The program runs perfectly if I substitute -for- commands for 
> the -forval- loop: for num 1/`r(max)': gen timeX  = (year - 
> 74)*(group == X); for num 1/52: gen timepX = (year - 
> 79)*(group == X)*(year >=80);
> 
> Thanks for any assistance you care to offer.
> 
> Deb Garvey
> 
> 

******************************
Deborah Garvey, Ph.D.
Department of Economics
Kenna Hall
Santa Clara University
Santa Clara, CA  95053
408/554-5580
408/554-2331 (FAX)
[email protected]    
http://lsb.scu.edu/~dgarvey
**********************************

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index