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

Hi guys, 

suppose to have the following: 

 

 

data DB;
  input ID :$20. Admission :date09. Discharge :date09. Morbidity1  Morbidity2  Morbidity3  Morbidity4;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017   1    0     1    0
0001  13JAN2017 25JAN2017   1    0     1    0
0001  22FEB2017 03MAR2017   0    1     0    0
0001  30JAN2019 04MAR2019   1    0     0    0
0002  01DEC2018 14DEC2018   1    0     1    0
0002  25DEC2018 02JAN2019   0    0     1    0
0002  25NOV2020 03DEC2020   1    1     1    1
0003  09JAN2016 25JAN2016   0    0     1    0
0003  29JAN2018 12FEB2018   0    0     1    1
...;

Is there a way to collapse all occurencies ("1" or "0") for the same ID? 

 

Let say the desired output should be: 

 


data DB1;
  input ID :$20.  Morbidity1  Morbidity2  Morbidity3  Morbidity4;
cards;
0001   1    1     1    0
0002   1    1     1    0
0003   0    0     1    1
...;

In other words regardless the admission-discharge specificity, the occurrencies should be collapsed into one row for each ID. Values in DB for Morbidity* variables are "1" or "0". No other values are present.

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

It looks like you want the maximum value for each of the morbidity variables within a single ID.  This is what PROC SUMMARY is for:

 

proc summary data=db ;
  by id;
  var morbidity1-morbidity4;
  output out=want (drop=_type_ _freq_)  max=;
run;

The above assume DB is sorted by ID.  If not, you could:

 


proc summary data=db nway;
  class id;
  var morbidity1-morbidity4;
  output out=want (drop=_type_ _freq_)  max=;
run;

 

--------------------------
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

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

It looks like you want the maximum value for each of the morbidity variables within a single ID.  This is what PROC SUMMARY is for:

 

proc summary data=db ;
  by id;
  var morbidity1-morbidity4;
  output out=want (drop=_type_ _freq_)  max=;
run;

The above assume DB is sorted by ID.  If not, you could:

 


proc summary data=db nway;
  class id;
  var morbidity1-morbidity4;
  output out=want (drop=_type_ _freq_)  max=;
run;

 

--------------------------
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

--------------------------
ballardw
Super User

One way:

proc summary data=work.db nway;
   class id;
   var Morbidity: ;
   output out=work.db1 (drop=_:) max=;
run;

NWAY means only the combination of all CLASS variables is output. Otherwise there would be a row for an all dataset summary. The Drop in the output removes two variables _type_, indicating combination of CLASS variables and _freq_ number of observations used .

If you haven't seen the : list  creator it mean use all variables whose names start with the characters before the colon.

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 513 views
  • 1 like
  • 3 in conversation