# st: Re: Panel data - generating variabels & looping

 From "Lee Chuntao" To Subject st: Re: Panel data - generating variabels & looping Date Tue, 22 Oct 2002 17:35:24 +0800

```Hi,

I think you can run the following code:
gen year = int(date/10000)
gen month = int((date-year*10000)/100)
gen day = date-10000*year-100*month
replace date = mdy(month, day, year)
gsort id  -date
by id: gen obs_num = _n

iis id
tis obs_num
tsset id obs_num

by id: gen distance = l.date-date
gen ratea = purchase/distance
by id: gen rateb=sum(ratea)

good luck

Lee Chuntao

----- Original Message -----
From: "Alexander Severinsen" <alex@markedsfakta.as>
To: "Statalist" <statalist@hsphsun2.harvard.edu>
Sent: Tuesday, October 22, 2002 5:22 PM
Subject: st: Panel data - generating variabels & looping

> Dear members of the list,
>
> 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
>
> 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    ... ...
>
> Any help would be greatly appreciated.
>
>
> Regards,
> Mvh,
> Alexander Severinsen
> markedsfakta as
>
> t: 777 56167
> m: 957 83425
>
> *
> *   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/

*
*   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/
```