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

st: RE: Panel data - generating variabels & looping


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Panel data - generating variabels & looping
Date   Tue, 22 Oct 2002 10:32:42 +0100

Alexander Severinsen
> 
> I have panel data looking like this,
> 
> Member ID	Date		Purchase (grams)
> 1001		20010813	600
> 1001		20010817	1000
> 1001		20010824    ...
> 1002
> 1002
> etc
> 
> Does anyone know or have any tips on how to generate a new 
> variabel that
> gives the distance in days between the different dates? Eg.
> 
> Member ID	Date		DateD (Distance)
> 1001		20010813	4
> 1001		20010817	7
> 1001		20010824	...
> etc
> 

I guess these dates are held as integers. Also, it seems
that you want to look ahead to the next purchase: 

. bysort id (date) : gen distance = date[_n+1] - date 

If these dates are strings, check out 
How do I convert date variables into Stata elapsed 
dates when the numbers run together, like 4151999?
http://www.stata.com/support/faqs/data/dateseq.html
including the canned solution -todate- available 
from SSC. 

> Based on the new variabel DateD i also want to generate a 
> new variabel
> ConsumptionRate that is:
> ConsumptionRateA = Purchase/Distance and then RateB=RateA/number of
> observations. Eg
> 
> Member ID	Date		Purchase (grams) DateD	RateA	
> 		RateB
> 1001		20010813	600			4     
> 600/4=150		150+142.8=292.8
> 1001		20010817	1000			7	
> 1000/7=142.8	150+142.8=292.8
> 1001		20010824    ...			...


. generate ratea =  purchase / distance 

Check out -egen- for group summaries. 

N.B. there is no need for any looping. 

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