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

st: RE: Extracting data to create a new variable


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Extracting data to create a new variable
Date   Wed, 26 Jun 2002 12:31:50 +0100

Christine_Tan@baruch.cuny.edu
> 
> Let's say I have the following dataset:
> 
> company  year    assets   liabilities
> a                   2001   100         25
> a                   2000    90          30
> a                   1999    105        45
> b                   1999     205       105
> b                   1998    200         80
> b                   1997    60           15
> 
> 
> I wish to generate a new variable called assets1 where asset1=assets for
> the previous year for that company.  How would I do this in Stata?  Thank
> you for your help.

Nick Winter suggested 

> . bysort company (year): gen assets1 = assets[_n-1] if year==year[_n-1]+1

> But take a look at the following on Stata's ability to deal with
> time-series explicitly:

> [U] 15.5.4 Time-series formats,
> [U] 27.3 Time-series dates,
> [U] 29.12 Models with time-series data,
> [R] tsset

Nick's suggestion is excellent. It solves your problem in one line. 

But I would like to expand on his second point. You have panel data. 
Declaring that to Stata via 

tsset company year 

and then being able to get the previous year's assets by 

gen assets1 = L.assets 

has the great advantages that Stata automatically respects
the panel structure of your data when calculating -assets1-, 
that it does the smart thing with respect to any gaps, 
and that you can do similar things directly with other 
variables. 

Nick 
n.j.cox@durham.ac.uk 
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index