Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: RE: forvalues, panel data


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: forvalues, panel data
Date   Thu, 30 Sep 2010 12:36:31 +0100

Your -forvalues- scaffolding is completely redundant, and in fact causes the same changes to be executed 11 times. But your problem does call for a loop over a varlist. 

foreach v of var <whatever> { 
	gen d`v' = . 
	replace d`v' = `v' if fyear == 1980
	replace d`v' = `v' * 0.967059 if fyear == 1981
	<etc> 
}

Nick 
n.j.cox@durham.ac.uk 

Biljana Dlab

I have panel data (companies, fiscal years) and need to deflate numbers
to US$ 1980

tsset  gvkey_n  fyear

I used forvalues function and did it like this:

First I created variable just with dots:

gen dI2=.

Then simple forvalues loop

.forvalues fyear = 1980/1990 {
.replace dI2=I2*1 if fyear==1980
.replace dI2=I2*0.967059 if fyear==1981
.replace dI2=I2*0.421106 if fyear==1982
...
.replace dI2=I2*0.307175 if fyear==1990
}

So that works, but since I have 10 variables (from I1 to I10) that need
to be deflated, repeating the whole procedure is time consuming... so I
tried by using var i=I1-I10, etc... but always got reply invalid
syntax...

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index