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

Hi community,

 

Here I have a sample data which I want to fetch from the table only the min and max value of grade.

data have;
input ID Comments:$10. Grade;
cards;
1 abcdefg 2
1 lkjhgff 3
1 poiuytr 1
10 mnbvcxa 1
10 mbdfrte 4
10 qwertyu 2
;
run;
proc print;
run;

 

Output should be like:

 

IDComments1Grade1Comments2Grade2
1poiuytr                1lkjhgff 3
10mnbvcxa 1mbdfrte 4

 

Please help me resolve the case.

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input ID Comments:$10. Grade;
cards;
1 abcdefg 2
1 lkjhgff 3
1 poiuytr 1
10 mnbvcxa 1
10 mbdfrte 4
10 qwertyu 2
;
run;

proc sql;
   create table want as 
   select ID
          ,(select Comments from have where a.ID=ID group by ID having Grade=min(Grade)) as Comment1
          ,min(Grade) as Grade1
          ,(select Comments from have where a.ID=ID group by ID having Grade=max(Grade)) as Comment2
          ,max(Grade) as Grade2
   from have as a
   group by ID;
quit;

proc print data=want;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input ID Comments:$10. Grade;
cards;
1 abcdefg 2
1 lkjhgff 3
1 poiuytr 1
10 mnbvcxa 1
10 mbdfrte 4
10 qwertyu 2
;
run;

proc sql;
   create table want as 
   select ID
          ,(select Comments from have where a.ID=ID group by ID having Grade=min(Grade)) as Comment1
          ,min(Grade) as Grade1
          ,(select Comments from have where a.ID=ID group by ID having Grade=max(Grade)) as Comment2
          ,max(Grade) as Grade2
   from have as a
   group by ID;
quit;

proc print data=want;
run;
ballardw
Super User

And an alternate approach using Proc Means:

Proc means data=have nway;
  class id;
  var grade;
  id comments;
  output out=want (drop= _type_ _freq_)
      minid=comments1 min=Grade1 maxid=Comments2 max=grade2
  ;
run;

The NWAY is so the result only includes values with the actual combinations of CLASS variables present. If you leave that off you get an additional row with the overall (regardless of ID) min, max and the identifiers. The Drop removes variables, the variable _type_ can be used to indentify which combination of CLASS is represented. This could be of interest if you had students (?) from different topics or schools. Include those on the class, remove the NWAY (good idea to leave the _type_ in ) and you would have an overall max min across schools, topic and Id, plus a summary with school only, topic only, school and topic, school and id, topic and id as well as the combination of school, topic and id.

 

If you were to want all of the above the SQL solution gets quite convoluted, with Proc Means (or summary) drop 2 keywords (nway _type_) and add 1 2 3 or as many grouping variables as you might have an interest in.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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