BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10
data have1;
infile datalines dsd dlm=",";
	input subj $ treatment $ painlvl bodypart $;
datalines;
001, A, 1, leg
001, A, 2, leg
002, B, 1, head
003, A, 1, arm
004, B, 1, eye
005, B, 2, mouth
; 
run; * choose distinct subjects with highest severity (if more than one instance);

Hello,

information:

* 6 obs

* n=5

* 2 treatment groups (A, B)

Goal: 001 has two reported painful observations in the same area (leg), and the goal is to

output the row where he has the highest pain level, so that my resultant data set will include 5 rows.

 

Could I receive some help? I tried PROC SQL and using the max() paired w/ the group by clause. It worked but I received an error. 

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have1;
infile datalines dsd dlm=",";
	input subj $ treatment $ painlvl bodypart $;
datalines;
001, A, 1, leg
001, A, 2, leg
002, B, 1, head
003, A, 1, arm
004, B, 1, eye
005, B, 2, mouth
; 
run;


proc sql;
create table want as
select * from have1
 group by subj
  having painlvl=max(painlvl);
quit;

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

Help us help you.  Please show the log including the code and the error message it produced.  

Use the "</>" icon to make a text box for that content.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Hello_there
Lapis Lazuli | Level 10
I don't know if it was an error, but it said something to the effect of not liking me using a summary function when trying to group by subject when i was selecting count(distinct subject).

Anyways, i found a data step way to do it which is to sort by subj treatment bodypart desecnding painlvl, and then proc sort again by subj nodupkey.

Thanks anway though
Ksharp
Super User
data have1;
infile datalines dsd dlm=",";
	input subj $ treatment $ painlvl bodypart $;
datalines;
001, A, 1, leg
001, A, 2, leg
002, B, 1, head
003, A, 1, arm
004, B, 1, eye
005, B, 2, mouth
; 
run;


proc sql;
create table want as
select * from have1
 group by subj
  having painlvl=max(painlvl);
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 840 views
  • 2 likes
  • 3 in conversation