BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
data test1;
input apple lemon;
datalines;
4 3
7 80
22 500
;
run;
proc print data=test1;
run;

data fruit;
set test1;
by apple lemon;
if last.apple then output;
run;
proc print data=fruit;
run;

I tried this but cannot see what last. is doing I was expecting only the last entry where apple=22, lemon=500 in dataset fruit, but I am getting fruit exactly same as test1, so what does last. and output do here?

1 REPLY 1
PaigeMiller
Diamond | Level 26

When you use last.apple, SAS assumes that your data is sorted by APPLE, and so the last record of each value of APPLE will be output. In your trivial example, there is only one record for each value of APPLE, this one record for each value of APPLE is also the last record for each value of APPLE, and so this record is output.

 

If we modify the data

 

data test1;
input apple lemon;
datalines;
4 3
4 7
7 12
7 22
7 80
22 500
;

data fruit;
set test1;
by apple lemon;
if last.apple then output;
run;

 

You can see now that there are multiple records with each value of APPLE, and the last record of each value of APPLE is output.

--
Paige Miller

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!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 96 views
  • 0 likes
  • 2 in conversation