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

Re: st: Using forval when one variable is not incremented


From   "Neil Shephard" <[email protected]>
To   [email protected]
Subject   Re: st: Using forval when one variable is not incremented
Date   Wed, 13 Aug 2003 09:21:52 +0100

Deborah Garvey wrote...
> I need to read in several years of state-level expenditure data and
> would like to automate the process using -forval-.  (Maybe this is not
> the way to go.)  
> 
> Here's my problem.  I don't have a CPI variable in the raw data, but I
> need the CPI to calculate real spending.  I can't -gen- the variable,
> as I must clear the data at each year's iteration.  All other
> variables are the same each year.
> 
> The code as currently written is below.  Any help is greatly
> appreciated.
> 
> Best,
> 
> Deb Garvey
> ----------
> 
> forval i=76/86  {
> 
> ***gen cpi`i' = 0.887788779;  --- NO.  This only works for the
> selected year.
> 
> insheet fipsst pubst state tsgex`i' sexcap`i' sgexed`i' sgexpw`i'
> capy`i' pery`i' using stateexpY`i'.txt;
> 
> **Real spending;
> 
> gen tsrgex`i'  = tsgex`i'/cpi`i';
> ...more in a similar vein
> 
> label var tsgex`i'   "Tot state genl exp in $mils";
> label var tsrgex`i'  "Tot real state genl exp in $mils";
> ..more in a similar vein
> 
> su if pubst != .;
> save stateexpY`i', replace;
> clear;
> };

Maybe I'm missing something, but you could simply move the -gen 
cpi`i' = 0.88788779- line to after you've read in each years data set, 
thus generating the cpi`i' variable variable for each data set you are 
reading in. Your loop would then look like 

forval i=76/86  {

insheet fipsst pubst state tsgex`i' sexcap`i' sgexed`i' sgexpw`i'
capy`i' pery`i' using stateexpY`i'.txt;

gen cpi`i' = 0.887788779;
**Real spending;

gen tsrgex`i'  = tsgex`i'/cpi`i';
...more in a similar vein

label var tsgex`i'   "Tot state genl exp in $mils";
label var tsrgex`i'  "Tot real state genl exp in $mils";
..more in a similar vein

su if pubst != .;
save stateexpY`i', replace;
clear;
};

HTH

Neil

Neil Shephard
Genetics Statistician
ARC Epidemiology Unit, University of Manchester
[email protected]
[email protected]

"Contrariwise, if it was so, it might be; and if it
were so it would be; but as it isn't, it ain't. That's
logic" - Tweedledee (Alice Through the Looking Glass)
*
*   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