Question

In this data step I do not understand what if last.y do... Could you tell me ?

data stop2;
    set stop2;
    by x y z t;

if last.y; /*WHAT DOES THIS DO ??*/

    if t ne 999999 then
        t=t+1;
    else do;
            t=0;
        z=z+1;
    end;
run;
Was it helpful?

Solution

LAST.Y refers to the row immediately before a change in the value of Y. So, in the following dataset:

data have;
input x y z;
datalines
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 3 1
2 3 2
2 3 3
;;;;
run;

LAST.Y would occur on the third, sixth, ninth, and twelfth rows in that dataset (on each row where Z=3). The first two times are when Y is about to change from 1 to 2, and when it is about to change from 2 to 3. The third time is when X is about to change - LAST.Y triggers when Y is about to change or when any variable before it in the BY list changes. Finally, the last row in the dataset is always LAST.(whatever).

In the specific dataset above, the subsetting if means you only take the last row for each group of Ys. In this code:

data want;
set have;
by x y z;
if last.y;
run;

You would end up with the following dataset:

data want;
input x y z;
datalines;
1 1 3
1 2 3
1 3 3
2 3 3
;;;;
run;

at the end.

One thing you can do if you want to see how FIRST and LAST operate is to use PUT _ALL_;. For example:

data want;
set have;
by x y z;
put _all_;
if last.y;
run;

It will show you all of the variables, including FIRST.(whatever) and LAST.(whatever) on the dataset. (FIRST.Y and LAST.Y are actually variables.)

OTHER TIPS

In SAS, first. and last. are variables created implicitly within a data step. Each variable will have a first. and a last. corresponding to each record in the DATA step. These values will be wither 0 or 1. last.y is same as saying if last.y = 1.

Please refer here for further info.

That is an example of subsetting IF statement. Which is different than an IF/THEN statement. It basically means that if the condition is not true then stop this iteration of the data step right now.
So

if last.y;

is equivalent to

if not last.y then delete;

or

if not last.y then return;

or

if last.y then do;
  ... rest of the data step before the run ...
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top