create a new variable using an ID variable

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

create a new variable using an ID variable

Hey all, I have a database with ID numbers and a variable called Type_Of_Visit. If any patient (denoted by variable ID) has a Type_Of_Visit as "inpatient" then I need to tag that ID as 1 and the remaining IDs without any inpatient as 0.

 

The issue is each ID can have multiple types of visits like Emergency, Outpatient, inpatient, multiple outpatient, multiple Emergency and multiple inpatient, and any combination of these, etc. All I want to do is to create a new variable Inpatient_Count if that ID has an inpatient visit at any time (it does not matter if the same ID has other types of visits). If that ID has at least one inpatient encounter, I want to tag all entries for that ID as 1 (in the new variable), and rest of the IDs without inpatient encounter as 0. Hope I clarified it! Please let me know if need further clarification.

 

Thanks for the help in advance!

 

Here is what I want:

ID            Type_Of_Visit     Inpatient_Count

1              inpatient                 1

1              Emergency             1

1              Outpatient              1

2              inpatient                  1

2             inpatient                   1

2             inpatient                   1

3           outpatient                   0

3           outpatient                    0

3           Emergency                   0

4          Emergency                  0

5          Outpatient                    0

6           inpatient                     1

6            Emergency                 1

7           Outpatient                     0

7              Outpatient                  0

7           Emergency                   0

7           Emergency                   0


Accepted Solutions
Solution
‎03-29-2018 04:28 PM
Respected Advisor
Posts: 3,054

Re: create a new variable using an ID variable

[ Edited ]
/* UNTESTED CODE */

data count_inpatient;
    set have;
    if upcase(type_of_visit)='INPATIENT' then new_inpatient_variable=1;
    else new_inpatient_variable=0;
run;

proc summary nway data=count_inpatient;
    class id;
    var new_inpatient_variable;
    output out=count_inpatient1 max=;
run;

data count_inpatient2;
    merge count_inpatient(drop=new_inpatient_variable) count_inpatient1;
    by id;
run;
--
Paige Miller

View solution in original post


All Replies
Solution
‎03-29-2018 04:28 PM
Respected Advisor
Posts: 3,054

Re: create a new variable using an ID variable

[ Edited ]
/* UNTESTED CODE */

data count_inpatient;
    set have;
    if upcase(type_of_visit)='INPATIENT' then new_inpatient_variable=1;
    else new_inpatient_variable=0;
run;

proc summary nway data=count_inpatient;
    class id;
    var new_inpatient_variable;
    output out=count_inpatient1 max=;
run;

data count_inpatient2;
    merge count_inpatient(drop=new_inpatient_variable) count_inpatient1;
    by id;
run;
--
Paige Miller
Contributor
Posts: 22

Re: create a new variable using an ID variable

Posted in reply to PaigeMiller

Thank you!

Learner
Posts: 1

Re: create a new variable using an ID variable

Posted in reply to PaigeMiller

 

 

proc sort data=have out=inp nodupkey;

by ID Type_of_visit;

where upcase(TYPE_OF_VISIT)='INPATIENT';

run;

 

data get;

merge have inp;

by ID;

run;

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 118 views
  • 0 likes
  • 3 in conversation