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. Event Index;
cards;
0001 1  0
0001 0  1
0002 1  1
0002 0  0
0003 1  0
0003 0  1
0003 0  1
0003 0  0
0003 0  1
run;

Each patient has Event = 1 corresponding to the first occurrence (there are dates). The dataset is sorted by Event. So, first.ID = 1. Then there is another column, i.e., Index, corresponding to hospitalizations at different dates (not shown). It doesn't matter if a patient has more than one hospitalization I would like to put index "1" if the patient hast at least one hospitalization. The index should be placed where Event = 1 because subsequent filters will be applied. Can anyone help me please? The desired output should be: 

 


data DB1;
  input ID :$20. Event Index;
cards;
0001 1  1
0001 0  0
0002 1  1
0002 0  0
0003 1  1
0003 0  0
0003 0  0
0003 0  0
0003 0  0
run;

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

PROC SUMMARY can help here

 

proc summary data=db nway;
    class id;
    var index;
    output out=db_max(drop=_:) max=max_index;
run;
data want;
	merge db db_max;
	by id;
	if event=1 and max_index=1 then new_index=1;
	else new_index=0;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

PROC SUMMARY can help here

 

proc summary data=db nway;
    class id;
    var index;
    output out=db_max(drop=_:) max=max_index;
run;
data want;
	merge db db_max;
	by id;
	if event=1 and max_index=1 then new_index=1;
	else new_index=0;
run;
--
Paige Miller
mkeintz
PROC Star

If the data are sorted by ID, then there is a single-step solution:

 

data want;
  merge db (where=(index=1) in=found_an_index)   db;
  by id;
  index= (event=1 and found_an_index=1);
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

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

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
  • 542 views
  • 4 likes
  • 3 in conversation