[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Mike Kim" <argonomos@gmail.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Re: Data Management |

Date |
Wed, 15 Apr 2009 11:23:10 -0500 |

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" <rmjohn@gmail.com> To: "stata" <statalist@hsphsun2.harvard.edu> 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/

**Follow-Ups**:**Re: st: Re: Data Management***From:*Rijo John <rmjohn@gmail.com>

**st: RE: Re: Data Management***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**References**:**st: Data Management***From:*Rijo John <rmjohn@gmail.com>

- Prev by Date:
**st: RE: Data Management** - Next by Date:
**AW: st: AW: simple sum() question** - Previous by thread:
**Re: st: RE: Data Management** - Next by thread:
**st: RE: Re: Data Management** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |