Thanks Mike. Your formula did the job. The change variable I listed
here doesnt give the number of years since the last change in P_t
because the years are not continuous. In the actual data, I have all
the years present and there is no lose of continuity.
Thanks,
Rijo.
On Wed, Apr 15, 2009 at 11:23 AM, Mike Kim <[email protected]> wrote:
> Hi Rijo,
>
> This should do it:
> bys id p_t (year): gen change=_n-1
>
> However your question is not clear. The two examples are different. You also
> said that change is the number of years passed since the last change in P_t,
> but Change as you wrote is not this one.
>
> Mike.
>
>
> ----- Original Message -----
> From: "Rijo John" <[email protected]>
> To: "stata" <[email protected]>
> Sent: Wednesday, April 15, 2009 11:07 AM
> Subject: st: Data Management
>
>
> Hi,
>
> I have a time series data set as follows.
>
> ID year P_t-1 P_t
> 1 1990 16 16
> 1 1991 16 16
> 1 1996 16 16
> 1 2005 42 42
> 1 2006 42 42
> 1 2007 42 42
> 2 1990 29 29
> 2 1991 29 29
> 2 1992 29 29
>
> Where P_t and P_t-1 represents price in year t and t-1 respectively.
> What I wish to achieve is to create a new variable "Change". This
> variable should show the number of years passed since the last change
> in P_t. In the above example the data should finally look like below.
>
> ID year P_t-1 P_t Change
> 1 1990 . 16 0
> 1 1991 16 16 1
> 1 1996 16 16 2
> 1 1997 16 16 3
> 1 2005 16 42 0
> 1 2006 42 42 1
> 1 2007 42 42 2
> 2 1990 . 29 0
> 2 1991 29 29 1
> 2 1992 29 29 2
> 2 1993 29 35 0
>
>
> If I do
>
> bysort ID P_t : gen Change=_n-1 I get the varible numbered but it
> doesnt sort the year. If I sort ID and year then I cant use the _n to
> generate these numbers.
> Can someone help me with this?
>
> Thanks in advance,
>
> Rijo.
> *
> * 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/
>
> *
> * 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/
>
--
Rijo M John, PhD
Institute for Health Research and Policy (MC 275),
1747 West Roosevelt Road, Room 558,
Chicago, Illinois 60608.
Ph: 312-413-9057
Fax: 312-355-2801
URL: http://ihrp-web.ihrp.uic.edu/
*
* 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/