Hello,
I'm a novice SAS user (SAS 9.4) and I'm working with a healthcare dataset where I need to count the number of observations that occurred on the same date at different places of service. Example, if ID 1 was seen at the ED on 01/01/2020 and Inpatient on 01/01/2020 then the new variable would count that as 1 service but if ID 1 was seen at the ED on 01/01/2020 and Inpatient on 01/31/2020 then the new variable wouldn't count that. How would I create a variable that would count that?
data test;
input id $ date yymmdd10. place $3. ;
format date yymmdd10.;
datalines;
1 2020/09/10 ED
1 2020/09/10 IN
2 2020/10/10 IN
3 2020/04/03 ED
3 2020/05/03 ED
3 2020/05/29 IN
4 2020/10/10 IN
4 2020/10/10 ED
4 2020/11/03 ED
4 2020/12/29 IN
;
run;
My expected output would be something like this:
ID | DATE | PLACE | NEWVAR |
1 | 9/10/2020 | ED | 1 |
1 | 9/10/2020 | IN | 1 |
2 | 10/10/2020 | IN | . |
3 | 4/3/2020 | ED | . |
3 | 5/3/2020 | ED | . |
3 | 5/29/2020 | IN | . |
4 | 10/10/2020 | IN | 1 |
4 | 10/10/2020 | ED | 1 |
4 | 11/3/2020 | ED | . |
4 | 12/29/2020 | IN | . |
I would really appreciate any insight. Thank you!
Amanda
Try this
proc sql;
create table want as
select *
, (count (distinct place) > 1) as newvar
from test
group by ID, date
;
quit;
Hello,
My expected output would be:
ID | DATE | PLACE | NEWVAR |
1 | 9/10/2020 | ED | 1 |
1 | 9/10/2020 | IN | 1 |
2 | 10/10/2020 | IN | . |
3 | 4/3/2020 | ED | . |
3 | 5/3/2020 | ED | . |
3 | 5/29/2020 | IN | . |
4 | 10/10/2020 | IN | 1 |
4 | 10/10/2020 | ED | 1 |
4 | 11/3/2020 | ED | . |
4 | 12/29/2020 | IN | . |
Thanks!
Try this
proc sql;
create table want as
select *
, (count (distinct place) > 1) as newvar
from test
group by ID, date
;
quit;
Thank you! This worked.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.