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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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