03-21-2015 01:08 PM
Hi
I have a longitudinal dataset with many firms each month , 9 years so I have (12*9) months for each firm an example of my dataset is mentioned below.
ID | Monthend(MM/YYYY) | Returns(%) | Level (Diff in Returns) |
---|---|---|---|
1212 | 082003 | 2.5 | |
1212 | 092003 | 2.8 | 2.8-2.5 |
1212 | 102003 | 2.0 | |
1212 | 012004 | 3.1 | |
1213 | 052005 | 4.0 | |
1213 | 062005 | 5.0 | |
1214 | 022008 | 2.3 | |
1214 | 032008 | 1.2 |
I would like to compute the variable 'Level" which is the difference in returns from T+1 and T . For example the Level for the ID 1212 in 092003 = Returns in 092003 - 082003 (2.8% - 2.5%). This should be for each ID , that is when the ID changes the first observation in Level variable should be '0' and from 2nd observation the difference is computed.
I was wondering whether First.ID and Last.ID method works for this. Could someone send me a sample code.
03-21-2015 02:15 PM
There's also the DIF() function
data want;
set have;
by id;
level=dif(returns);
if first.id then level=0;
run;
03-21-2015 01:32 PM
data want;
set have;
by id;
k=lag(returns);
if not first.id then level=returns-k;
drop k;
run;
03-21-2015 08:04 PM
Thank you , both the codes works fine for me
03-21-2015 02:15 PM
There's also the DIF() function
data want;
set have;
by id;
level=dif(returns);
if first.id then level=0;
run;
Need further help from the community? Please ask a new question.