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
[email protected]
*
* 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/