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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1036 views
  • 6 likes
  • 4 in conversation