Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
menu_book Course Curriculum (SAS Complete Course) expand_more
SAS Complete Course / Row Processing

FIRST. and LAST. variables

Learn FIRST. and LAST. variables in SAS through the exact construct, a dedicated example, a boundary case, and a mastery check.

10 min read • Beginner

FIRST. and LAST. variables is an atomic SAS lesson inside Row Processing. The example uses the specific syntax, command, API, instruction, or language rule named by this lesson.

Study the construct itself, then change one relevant input and predict the result before running it. That keeps this lesson focused on FIRST. and LAST. variables rather than on generic SAS syntax.

Topic-specific example

sas-02-09.sas
sas Try It Live
proc sort data=work.sales; by customer_id; run; data work.summary; set work.sales; by customer_id; if first.customer_id then total=0; total+amount; if last.customer_id then output; retain total; run;
Edge case / engineering note

For FIRST. and LAST. variables, inspect the SAS log as well as the output data set; type conversion, missing values, and BY-group ordering can change results without stopping execution.

Content source & attribution
Original Syntaxis Content · Original Syntaxis Content