BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sarahsasuser
Quartz | Level 8
 
Posts: 97
 
Creating a counter for hospitalizations
[ New ]
 

Hi All,

 

I have a large claims dataset where patients had a number of procedures on the same and different hospitalizations. I want to create a counter that counts the number of hospitalizations for each patient. This is easy to do if each row represents a different hospitalization, but many patients have several rows for the same hospitalization. How do I create this type of counter? The counter varaible Hospital_count is what I want to create in the data set below:

 

Want:

Patient      Hospital_admission_date   Procedure_code     Hospital_count

1               01/01/1999                         1.1                          1        

1               01/01/1999                         2.5                          1

1               02/01/1999                         2.3                          2

1               02/01/1999                         1.5                          2

2               01/01/1999                         2.2                          1

2               01/03/1999                         1.5                          2

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

I think this may get you started if I understand

 

data want;
   set have;
   by patient hospital_admission_date;
   retain count;
   If first.patient then count=1;
   else if first.hospital_admission_date then count+1;
run;

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the question is how do you know to increment the counter at row 3, I see nothing that would indicate the counter should change there.  You need to identify the logical check which will trigger a check.

ballardw
Super User

I think this may get you started if I understand

 

data want;
   set have;
   by patient hospital_admission_date;
   retain count;
   If first.patient then count=1;
   else if first.hospital_admission_date then count+1;
run;
sarahsasuser
Quartz | Level 8

Thank you! such a simple and elegant solution that eluded me!

sarahsasuser
Quartz | Level 8

I have a follow up question. I now want to know how many procedures each patient had during their hospitalization. If the patient has two rows for the same hospitalization it means they had two different surgeries during the same hospitalization. Can you show me how to add procedure_count to the dataset below?

 

Want:

Patient      Hospital_admission_date   Procedure_code     Hospital_count      Procedure_count

1               01/01/1999                         1.1                          1                           2

1               01/01/1999                         2.5                          1                           2

1               02/01/1999                         2.3                          2                           2

1               02/01/1999                         1.5                          2                           2

2               01/01/1999                         2.2                          1                           1

2               01/03/1999                         1.5                          2                           1

Ujjawal
Quartz | Level 8

 

proc sql noprint;
create table tt as
select * from temp a inner join
(select patient, Hospital_admission_date ,count(*) as N
from temp
group by patient, Hospital_admission_date ) b
on a.patient=b.patient and a.Hospital_admission_date = b.Hospital_admission_date;
quit;

 

sarahsasuser
Quartz | Level 8

Is there a way I can do this in the data step instead of using sql? I have a number of data cleaning items and am outputting several datasets and would like to create the procedure counter within that data step.

Jim_G
Pyrite | Level 9

try this:

 

data want;   set have;   by patient hospital_admission_date;   retain count procedures;  

    If first.patient then do; count=0; procedures=0; end;

     Procedures+1;

     If first hospital_admission_date then count+1;

     If last.patient then output; drop hospital_admission_date;

 

Jim

 

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1431 views
  • 2 likes
  • 5 in conversation