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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.