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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

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

Subject |
st: RE: Re: Data Management |

Date |
Wed, 15 Apr 2009 17:33:06 +0100 |

This looks OK if prices never decrease, but would give incorrect answers if an earlier price was ever repeated in a later period. My earlier solution makes no such assumption. Nick n.j.cox@durham.ac.uk Mike Kim 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. Rijo John 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? * * 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/

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

**st: Re: Data Management***From:*"Mike Kim" <argonomos@gmail.com>

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

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