## how to use first. last.?

Solved
Frequent Contributor
Posts: 89

# how to use first. last.?

Hello,

Suppose I have following dataset (" test"):

idvar_1
var_2
111
121
130
140
151
261
270
280
291
2101

I run follwoing codes:

proc sort data=test;

by id var_1 var_2;

run;

data test;

set test;

by id var_1 var_2;

if last.var_2 then var_3=1;

else var_3=0;

run;

What I expect is:

idvar_1var_2var_3
1110
1211
1300
1401
1510
2611
2700
2801
2910
21011

However, what I get is:

idvar_1var_2var_3
1111
1211
1301
1401
1511
2611
2701
2801
2911
21011

What I did is wrong?

Accepted Solutions
Solution
‎01-31-2012 05:53 PM
Super User
Posts: 8,089

## Re: how to use first. last.?

To get your requested output use BY VAR_2 NOTSORTED.

Not sure that actually makes any sense as it is crossing boundry between values of ID.

`159  data want ;160    set have ;161    by var_2 notsorted;162    var_3=last.var_2;163    put (id var_1 var_2 var_3) (3.);164  run;`

`  1  1  1  0  1  2  1  1  1  3  0  0  1  4  0  1  1  5  1  0  2  6  1  1  2  7  0  0  2  8  0  1  2  9  1  0  2 10  1  1`

All Replies
Super User
Posts: 8,089

## how to use first. last.?

The problem is the VAR_1 is different on every observation. So within the set of constant values for ID and VAR_1 every value of VAR_2 is unique.

data want ;

set test;

by id var_2 notsorted;

var_3 = last.var_2;

run;

PROC Star
Posts: 8,164

## how to use first. last.?

I think you are trying to do something like:

data test;

set test;

by id var_2 notsorted;

if last.var_2 then var_3=1;

else var_3=0;

run;

However, that would meet all of your expectations except for the 5th record which, if I correctly understand what you are trying to do, was incorrect in your example.

Frequent Contributor
Posts: 89

## how to use first. last.?

Thank you all very much for help. I never know there is a "notsorted" option.

By the way, could you please elaborate why 5th record is incorrect? I couldn't figure it out myself...

Solution
‎01-31-2012 05:53 PM
Super User
Posts: 8,089

## Re: how to use first. last.?

To get your requested output use BY VAR_2 NOTSORTED.

Not sure that actually makes any sense as it is crossing boundry between values of ID.

`159  data want ;160    set have ;161    by var_2 notsorted;162    var_3=last.var_2;163    put (id var_1 var_2 var_3) (3.);164  run;`

`  1  1  1  0  1  2  1  1  1  3  0  0  1  4  0  1  1  5  1  0  2  6  1  1  2  7  0  0  2  8  0  1  2  9  1  0  2 10  1  1`
🔒 This topic is solved and locked.