Statalist


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

st: AW: Data Management


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: Data Management
Date   Wed, 15 Apr 2009 18:41:19 +0200

<> 



*************
clear*

input  ID	year	LPt	Pt 
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
end

compress

by ID Pt (year), sort : generate change=_n-1

list, noobs
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Rijo John
Gesendet: Mittwoch, 15. April 2009 18:07
An: stata
Betreff: 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/



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