DATA Step, Macro, Functions and more

Creating a New Variable if Death_Date is present

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Creating a New Variable if Death_Date is present

[ Edited ]

Hello,

 

I want to create a dummy variable for the patient data.

 

Patients 10002, 10003, 10004 were dead, and 

Patients 10005, 10006, 10007 have survived so far.  

 

-----------------------------------------------

Patient_ID      Death_Date 

10002            2013-06-04

10003            2011-08-15

10004            2012-12-14

10005 

10006

10007

---------------------------------------------

Depending on if Death_Date is present or not, I want to make  a new variable DEATH (1= Death, 0=Surived) 

 

 

 

 

so I tried the following: 

 

data w.patient;

set w.patient;

Death=.;

if (Death_Date=".") then Death = 1

if (Death_Date=" ") then Death = 0

run; 

 

However, it is marking all the death

 

What have I done wrong?

 

Any advice or help would be appreciated.

 

Thank you. 

 

P.S Ultimately, I want to make my data set as  the following: 

-----------------------------------------------

Patient_ID      Death_Date     Death 

10002            2013-06-04           1 

10003            2011-08-15           1

10004            2012-12-14           1

10005                                         0

10006                                         0

10007                                         0

---------------------------------------------

 

 

 

 

 

 


Accepted Solutions
Solution
‎07-20-2016 08:21 PM
Super User
Posts: 9,662

Re: Please Help: Creating a New Variable if Death_Date is present

data w.patient;

set w.patient;

if missing(Death_Date) then Death = 0 ;

 else Death = 1;

run; 

View solution in original post


All Replies
Solution
‎07-20-2016 08:21 PM
Super User
Posts: 9,662

Re: Please Help: Creating a New Variable if Death_Date is present

data w.patient;

set w.patient;

if missing(Death_Date) then Death = 0 ;

 else Death = 1;

run; 

Super User
Super User
Posts: 7,392

Re: Please Help: Creating a New Variable if Death_Date is present

Just to add to your great solution @Ksharp, this would be a great example for the IFN/IFC functions - i.e. a binary yes or no choice:

data w.patient;
  set w.patient;
  death=ifn(missing(Death_Date),0,1);
run; 
Contributor
Posts: 23

Re: Please Help: Creating a New Variable if Death_Date is present

Thank you so much!!

Super User
Posts: 17,749

Re: Creating a New Variable if Death_Date is present

It depends on your variable type, numeric or character. 

 

In character variables missing is represented as a blank, and this can be denoted with empty quotes ("")

A numeric variable has a . when missing and you can check for this with (.) no quotation marks required. 

 

If date_char = "" then death = 0; else death = 1;
If date_num = . Then death = 1; else death =0;

Or you can bypass that issue entirely and use the MISSING() function as demonstrated in @Ksharp code. Personally I like that option as it's easy to read. 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 269 views
  • 0 likes
  • 4 in conversation