SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
clqa
Calcite | Level 5

is the first. and last. statement temporary even though its in the data step?

5 REPLIES 5
Cynthia_sas
SAS Super FREQ

Hi:
FIRST.byvar and LAST.byvar are automatic variables that exist for the duration of the DATA step program, but they can be used in the program. Since they are never output to the final dataset, you might consider them temporary. I prefer to think of them as automatic, like _N_ and _ERROR_, which are also available for the duration of the program but not output.

So, FIRST. and LAST. are variables, not statements. When you use FIRST. and LAST. in a program, it is typically in some kind of IF statement like this:
if last.company then output;
if first.department then dept_counter=0;

Cynthia

Reeza
Super User

First and Last variables are automatic variables that are not written to the output data set.

 


@clqa wrote:

is the first. and last. statement temporary even though its in the data step?


 

rahuljhaver
Calcite | Level 5
Yes they are temporary variables created by SAS while processing the BY group staement in DATA step. These temporary variables are available for DATA step programming but are not added to the output data set.
data_null__
Jade | Level 19

This is interesting.

 

32   data _null_;
33      first.sex = 30;
34      put _all_;
35      run;

first.sex=30 _ERROR_=0 _N_=1
Cynthia_sas
SAS Super FREQ

Hi:

  Creating or using the variable in an assignment statement is not prohibited. But look what happens when you try to create a dataset:

first_var_not_written.png

There are 2 assignment statements, but only 1 variable (X) is actually written to the output data -- the FIRST.SEX variable is NOT written to the output dataset.

 

Cynthia

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1372 views
  • 0 likes
  • 5 in conversation