BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
aokolo
Fluorite | Level 6

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:

IDDATEPLACENEWVAR
19/10/2020ED1
19/10/2020IN1
210/10/2020IN.
34/3/2020ED.
35/3/2020ED.
35/29/2020IN.
410/10/2020IN1
410/10/2020ED1
411/3/2020ED.
412/29/2020IN.

 

I would really appreciate any insight. Thank you!

 

Amanda

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

proc sql;
   create table want as
   select *
        , (count (distinct place) > 1) as newvar
   from test
   group by ID, date
   ;
quit;

View solution in original post

4 REPLIES 4
aokolo
Fluorite | Level 6

Hello,

My expected output would be:

IDDATEPLACENEWVAR
19/10/2020ED1
19/10/2020IN1
210/10/2020IN.
34/3/2020ED.
35/3/2020ED.
35/29/2020IN.
410/10/2020IN1
410/10/2020ED1
411/3/2020ED.
412/29/2020IN.

 

Thanks!

PeterClemmensen
Tourmaline | Level 20

Try this

 

proc sql;
   create table want as
   select *
        , (count (distinct place) > 1) as newvar
   from test
   group by ID, date
   ;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 442 views
  • 2 likes
  • 3 in conversation