BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10

proc sql;
create table cars2 as
select make, type
from sashelp.cars
where make in ('Acura','Audi')
;quit;

data cars3;
set cars2;
by make type;
if last.make and last.type;
run;

 

The query gives the last make and type.  I want to show the last make and type in each category.  For example

Make     Type

Accura   Sedan

Accura   Sports

 

Same scenario for Audi.  Instead I am getting the last observation period.  Is there a way to address in the proc sql statement or must it be done in the datastep?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you want the last of each type for all of the makes then you want:

 

if last.type;

 

proc sort data=sashelp.cars
   out=work.cars;
   by make type;
run;

data want;
   set work.cars (keep=make type);
   by make type;
   if last.type;
run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

It is not clear to me what you want.

Try each of next IF statements:

1) if last type;

2) if last make;

3) if last.type then putlog type=;
    if last.make then putlog make=;

4) if last.make or last.type;

PeterClemmensen
Tourmaline | Level 20

This logic is not clear to me. Why do you want two observations for Accura? If you want to show the last make and type for each category, then what is your overall category?

ballardw
Super User

If you want the last of each type for all of the makes then you want:

 

if last.type;

 

proc sort data=sashelp.cars
   out=work.cars;
   by make type;
run;

data want;
   set work.cars (keep=make type);
   by make type;
   if last.type;
run;
PGStats
Opal | Level 21

It is not clear what you mean by category. Maybe you want this:

 

data cars2;
set sashelp.cars;
where make in ('Acura','Audi');
by make type notsorted;
if last.type;
/* keep make type; */
run;

proc print data=cars2 noobs; var make type; run;

image.png

PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1025 views
  • 1 like
  • 5 in conversation