BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

Hi there,

I am trying to generate record of each person having maximum and minimum bp with respective dates. Sample of dataset is as follows:

data test;
input id bp date $ ;
cards;
1 100 20160110
2 110 20160111
3 150 20160112
1 110 20160112
2 130 20160113
3 130 20160114
1 140 20160113
2 150 20160114
3 120 20160115
1 120 20160114
2 120 20160115
3 160 20160116
1 150 20160115
2 140 20160116
3 130 20160117
;
run;

Wanted:

id    max_bp           date_having_max_bp               min_bp             date_having_min_bp

1     150                   20160115                                  100                  20160110

 

Can somebody help me to do that. Thank you in advance for your kind reply. 

Swain
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi:

proc sort data=test;
  by id bp;
run;

data want (drop=bp date);
  set test;
  by id;
  retain min_bp min_date_bp max_bp max_date_bp;
  if first.id then do;
    min_bp=bp;
    min_date_bp=date;
  end;
  if last.id then do;
    max_bp=bp;
    max_date_bp=date;
    output;
  end;
run;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi:

proc sort data=test;
  by id bp;
run;

data want (drop=bp date);
  set test;
  by id;
  retain min_bp min_date_bp max_bp max_date_bp;
  if first.id then do;
    min_bp=bp;
    min_date_bp=date;
  end;
  if last.id then do;
    max_bp=bp;
    max_date_bp=date;
    output;
  end;
run;
DeepakSwain
Pyrite | Level 9

Thank you for your quick reply as well as for providing solution in very simple language. 

Swain
Kurt_Bremser
Super User

Data step solution:

proc sort data=test;
by id;
run;

data want;
set test;
by id;
retain max_bp date_having_max_bp min_bp date_having_min_bp;
format date_having_max_bp date_having_min_bp yymmddn8.;
if first.id
then do;
  max_bp = 0;
  min_bp = 1000;
end;
if bp > max_bp
then do;
  max_bp = bp;
  date_having_max_bp = date;
end;
if bp < min_bp
then do;
  min_bp = bp;
  date_having_min_bp = date;
end;
if last.id then output;
keep id max_bp date_having_max_bp min_bp date_having_min_bp;
run;

 

PS: @RW9's solution beats mine. Much neater.

ballardw
Super User

One way:

proc means data=test noprint nway;
   class id;
   var bp;
   output out=want (drop= _:)
     max= idgroup(max(bp) out[1] (date)=DateBpMax)
     min= idgroup(Min(bp) out[1] (date)=DateBPMin)
     /autolabel autoname;
run;
proc print noobs label;run;

 

The Nway suppresses an over max min across the ID values.

I didn't force to your variable names. There some pretty good reasons to use the statistic as a suffix depending on later processing needs.

DeepakSwain
Pyrite | Level 9

Thank you for providing me an alternative way to address my reporting process. 

Swain

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
  • 5 replies
  • 1310 views
  • 6 likes
  • 4 in conversation