BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ari2495
Obsidian | Level 7

data Db_1;
set Db;
by variable_order;
if first.variable=1 then amount=0;
amount+balance;
if last.variable=1 then output;
run;

Hi, on the code as above i can't understand the use of first and last. Are they simply libraries or have they a specific function?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The FIRST. and LAST. are automatic variables, created for every variable used in the BY statement. They indicate a change in the values of a variable. See this:

data have;
input x;
datalines;
1
1
1
2
2
3
;

data want;
set have;
by x;
f = first.x;
l = last.x;
run;

BTW, your data step will not work, as you use FIRST. and LAST. for a variable not included in the BY.

 

Also note that, for BY to work, the dataset must be sorted by the variable(s), unless the NOTSORTED option is used.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

The FIRST. and LAST. are automatic variables, created for every variable used in the BY statement. They indicate a change in the values of a variable. See this:

data have;
input x;
datalines;
1
1
1
2
2
3
;

data want;
set have;
by x;
f = first.x;
l = last.x;
run;

BTW, your data step will not work, as you use FIRST. and LAST. for a variable not included in the BY.

 

Also note that, for BY to work, the dataset must be sorted by the variable(s), unless the NOTSORTED option is used.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 209 views
  • 1 like
  • 2 in conversation