Hi,
Patient_ID | Admission_date | Comorbidity |
A | 14/05/2011 | 1 |
B | 21/04/2011 | 0 |
C | 30/06/2011 | 2 |
C | 23/09/2011 | 3 |
C | 22/06/2012 | 1 |
D | 25/04/2011 | 2 |
D | 26/04/2012 | 1 |
E | 01/12/2011 | 1 |
E | 04/12/2011 | 2 |
E | 29/04/2012 | 3 |
E | 30/04/2013 | 2 |
Patient_ID | Admission_date | Comorbidity | Past_Score |
A | 14/05/2011 | 1 | 0 |
B | 21/04/2011 | 0 | 0 |
C | 30/06/2011 | 2 | 0 |
C | 23/09/2011 | 3 | 2 |
C | 22/06/2012 | 1 | 5 |
D | 25/04/2011 | 2 | 0 |
D | 26/04/2011 | 1 | 2 |
E | 01/12/2011 | 1 | 0 |
E | 04/12/2011 | 2 | 1 |
E | 29/04/2012 | 3 | 3 |
E | 30/04/2013 | 2 | 0 |
I am using SAS Enterprise Guide, version 7.1
SQL is a good choice for this type of question.
You join the table to itself, within period of a year.
Untested:
Proc SQL;
create table want as
select a.*, sum(b.comorbidity) as scores
from have as a
left join have as b
on b.date between a.date-1 and a.date-365
group by a.id, a.date, a.comorbidity;
quit;
SQL is a good choice for this type of question.
You join the table to itself, within period of a year.
Untested:
Proc SQL;
create table want as
select a.*, sum(b.comorbidity) as scores
from have as a
left join have as b
on b.date between a.date-1 and a.date-365
group by a.id, a.date, a.comorbidity;
quit;
Using @Reeza's code may be also wrap a coalesce() function around the sum() so that you get 0 and not missing for cases where there is no previous admission.
Proc SQL;
create table want as
select a.*, coalesce(sum(b.comorbidity),0) as scores
from have as a
left join have as b
on b.date between a.date-1 and a.date-365
group by a.id, a.date, a.comorbidity;
quit;
Thank you very much @Reeza and @Patrick. I really appreciate your help.
The code works great, the only problem arises when I keep some other variables in the table - in that case proc sql duplicates some observations.
For example:
data have;
input id date ddmmyy8. comorbidity dummy ;
format date date8.;
cards;
1 11072014 1 1
1 09022016 1 1
1 17032016 1 1
1 11012016 1 1
2 11012016 1 1
2 08022016 1 1
3 19012016 1 1
4 10012016 1 1
;
run;
proc sql;
create table want as
select a.*, coalesce(sum(b.comorbidity),0) as scores
from have as a
left join have as b
on a.id=b.id
and b.date between a.date-1 and a.date-365
group by a.id, a.date, a.comorbidity;
quit;
In this case proc sql duplicates one observation and I end up with 8 instead of original 9.
Any idea how I can solve this?
Many thanks indeed, I am really grateful!
Sorry, I mean I end up with 9 instead of original 8 observations.
Thank you!
Best,
K.
use either a DISTINCT in the select clause or even better add the variable to the Group By clause.
Thanks, it's works perfectly now!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.