Hi all, here's the result I got from SAS:
A 10
B 5
B 6
C 7
C 8
C 9
C 10
D 2
Does anyone know how to output it like this:
A 10
B 6
C 10
D 2
Thank you in advance!
data want;
set have;
by id;
if last.id;
run;
Thanks for your reply it works but can we make it without using proc sort and set statement?
but what is problem with sort and set statement. That is clean and easiest way to say. if you data is always in that order that your max id is last record then you can do
proc sql;
select * from have
group by id
having othercolumn = max(othercolumn);
set statement is totally fine and I understand it is the easiest way but I'm actually doing an assignment and we're told to solve this by using if-then, retain, output statement etc. Thanks tho!
I don't understand this? Do you want to use if-then, retain and an output statement?
If you want to avoid sorting and you know that the data is already grouped, you could do
data have;
input ID $ var;
datalines;
A 10
B 5
B 6
C 7
C 8
C 9
C 10
D 2
;
data want;
set have;
by ID notsorted;
if last.ID;
run;
@ywhui9 wrote:
Thanks for your reply it works but can we make it without using proc sort and set statement?
You have to explain further what you are doing, and WHY you don't want to use PROC SORT and the SET statement. The code is written a certain way for a reason (simplicity, directness); and without a good reason (which you have not specified), you are asking for a lot of unnecessary complications.
If you had to, you could load the data into a hash table (using methods that replace previously found values) and then unload the result into a data set. But why use a sledgehammer to kill a fly?
If you had to, you could load the data into a hash table (using methods that replace previously found values) and then unload the result into a data set. But why use a sledgehammer to kill a fly?
Sorry guys, I don't want to enclose too much detail (e.g. my codes) as I found some of my classmates are asking for the same question too. I know this looks stupid. I'm going to find out the solution myself. Thanks for all replies! Have a nice day!
@ywhui9 wrote:
I found some of my classmates are asking for the same question too. I know this looks stupid.
Indeed, it does.
Your presumed classmates have been posting essentially the same homework again and again during the past 48 hours:
Doesn't it defeat the purpose of homework to have experts do it?
What if your course instructors follow these threads?
@ywhui9: No problem, sorry, wasn't meant to be rude. Now I see, you just needed the last missing piece to the puzzle. That's fine. 🙂
@ywhui9 wrote: I have no friends in this class ...
Sad to read this. Luckily, this forum is full of nice people who spend a lot of their free time helping other members with their SAS (and statistics) questions. It's a great place for learning SAS (which is actually a never-ending task) because you can see how problems of all levels are tackled by experts with various backgrounds.
Good luck with your SAS course and happy learning!
Hi @ywhui9 Interesting that a group of your classmates have found this amazing SAS family(our communities) while my mates are still pondering how to ask a clear question here. Plus, one of your questions(summary in one step that involved Look ahead) was something I personally enjoyed answering.
To your-" we're told to solve this by using if-then, retain, output statement etc" (supposing that's for extra credit?) add some flavor to astounding idea of hash replace the found previous values in this thread
data have;
input ID $ var;
datalines;
A 10
B 5
B 6
C 7
C 8
C 9
C 10
D 2
;
data _null_;
dcl hash H (dataset:'have',ordered: "a",duplicate:'r') ;
h.definekey ("id") ;
h.definedata ("id", "var") ;
h.definedone () ;
h.output(dataset:'want');
stop;
retain _n_ _iorc_ ;
if 0 then do;set have;output;end;
run;
The above may make professors may think we are naughty but works for extra credit :), but I have always been that since childhood. Have fun!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.