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
Diamond | Level 26

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
Diamond | Level 26

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1957 views
  • 0 likes
  • 5 in conversation