Solved
Contributor
Posts: 24

# First. and Last.

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.

IDMonthend(MM/YYYY)Returns(%)Level (Diff in Returns)
12120820032.5
12120920032.82.8-2.5
12121020032.0
12120120043.1
12130520054.0
12130620055.0
12140220082.3
12140320081.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.

Accepted Solutions
Solution
‎03-21-2015 02:15 PM
Super User
Posts: 23,752

## Re: First. and Last.

There's also the DIF() function

data want;

set have;

by id;

level=dif(returns);

if first.id then level=0;

run;

All Replies
Frequent Contributor
Posts: 115

## Re: First. and Last.

data want;

set have;

by id;

k=lag(returns);

if not first.id then level=returns-k;

drop k;

run;

Contributor
Posts: 24

## Re: First. and Last.

Thank you , both the codes works fine for me

Solution
‎03-21-2015 02:15 PM
Super User
Posts: 23,752

## Re: First. and Last.

There's also the DIF() function

data want;

set have;

by id;

level=dif(returns);

if first.id then level=0;

run;

🔒 This topic is solved and locked.