Help using Base SAS procedures

creating new categorical variable using Date variables

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

creating new categorical variable using Date variables

Hi group,

I have a data with Emergency Room visits with Room start date (datetime) and Room end dates. I also have labs for the patients captured as collection_date (datetime). I have to identify labs which were taken at ER using Room_Start_Date (datetime) and Room_End_Date (datetime). If labs were taken between Room_Start_Date and Room_End_Date, then they are considered labs_at_ER, which need to create a new categorical variable (yes/no). Can anyone please help me with the SAS codes to create this new variable? Note: all my date variables are in datetime format.

 

Thanks a lot,

Satish


Accepted Solutions
Solution
‎02-15-2016 04:40 PM
Trusted Advisor
Posts: 1,115

Re: creating new categorical variable using Date variables

[ Edited ]

Hi Satish,

 

Basically, the new categorical variable could be defined as follows (in a data step; similarly in PROC SQL):

labs_at_ER=(Room_Start_Date<=collection_date<=Room_End_Date);

(Assumption: Your datetime variables are SAS datetime values or at least in a format that preserves chronological order.)

 

As created above, labs_at_ER would be a numeric variable, to which you could assign a numeric format that maps 1 to "yes" and 0 to "no." This has several advantages over a character variable with values "yes" and "no."

 

It's up to you to decide if "<=" or "<" is more appropriate in either place in the definition. You should also add code (e.g. an IF condition) to handle missing values of the three variables involved in the inequality.

 

Edit: Correction: The numeric format should, of course, map 0 to "no", not 2.

View solution in original post


All Replies
Solution
‎02-15-2016 04:40 PM
Trusted Advisor
Posts: 1,115

Re: creating new categorical variable using Date variables

[ Edited ]

Hi Satish,

 

Basically, the new categorical variable could be defined as follows (in a data step; similarly in PROC SQL):

labs_at_ER=(Room_Start_Date<=collection_date<=Room_End_Date);

(Assumption: Your datetime variables are SAS datetime values or at least in a format that preserves chronological order.)

 

As created above, labs_at_ER would be a numeric variable, to which you could assign a numeric format that maps 1 to "yes" and 0 to "no." This has several advantages over a character variable with values "yes" and "no."

 

It's up to you to decide if "<=" or "<" is more appropriate in either place in the definition. You should also add code (e.g. an IF condition) to handle missing values of the three variables involved in the inequality.

 

Edit: Correction: The numeric format should, of course, map 0 to "no", not 2.

Valued Guide
Posts: 2,175

Re: creating new categorical variable using Date variables

One handy feature that would support this kind of connection is the USER FORMAT
This can supply a value where nunbers fall within ranges.
See PROC FORMAT
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 575 views
  • 3 likes
  • 3 in conversation