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?
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;
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;
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?
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;
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.