DATA Step, Macro, Functions and more

Merging TWO different variables into ONE Variable

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Merging TWO different variables into ONE Variable

[ Edited ]

Hello,

 

I am having a difficult time trying to figure out how to merge the two different variables into one variable.

 

My data set looks like the following:

 

ID=> Patient ID

Chemo_Start_Date=> Date when chemotherapy started

Death=> 1=Death 0=Survived

Death_Date=> Date when patient died

Observation_Finish_date=> Date when follow-up is finished

FU_DTH

 

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

ID           Chemo_Start_Date    Death     Death_Date     Observation_Finish_Date       

10001           2011-08-22            1            2012-03-01                2015-05-30                                             

10002           2010-05-15            0                      .                        2015-05-30                                   

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

 

The follow-up duration (FU) for patients who died (Death=1) should be the difference in dates between the Chemo_Start_Date and

Death_Date.

 

The follow-up duration (FU) for patients who survived (Death=0) should be the difference in dates between the Chemo_Start_Date and

Observation_Finish_Date.

 

So I tried the following:

 

data w.cancer_DT; set w.cancer;

FU = DATDIF(Chemo_Start_Date, Death_Date, 'ACT/ACT'); where death=1;

run;

 

data w.cancer_SUR; set w.cancer;

FU = DATDIF(Chemo_Start_Date, Observation_Finish_Date, 'ACT/ACT'); where deat=0;

run;

 

data w.cancer_All; set w.cancer_DT w.cancer_SUR;

run;

 

 

My result looks like the following, which is what I needed:

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

ID           Chemo_Start_Date    Death     Death_Date     Observation_Finish_Date     FU_Survival     

10001           2011-08-22            1            2012-03-01                2015-05-30                        192                       

10002           2010-05-15            0                      .                        2015-05-30                         841.                      

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

 

However, is there a way to do this in a simple way without seperating files?

 

Any help or advice will be greatly appreciated.

 

Thank you.


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

Re: Merging TWO different variables into ONE Variable

data have;
input ID   Chemo_Start_Date : yymmdd10.  Death     Death_Date  : yymmdd10.   Observation_Finish_Date   : yymmdd10.;
format Chemo_Start_Date    Death_Date  Observation_Finish_Date   yymmdd10.;
cards;    
10001           2011-08-22            1            2012-03-01                2015-05-30                                             
10002           2010-05-15            0                      .               2015-05-30  
;
run; 


data want;
 set have;
 FU_Survival=coalesce(Death_Date,Observation_Finish_Date)-Chemo_Start_Date;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,419

Re: Merging TWO different variables into ONE Variable

Just put an if around it:

data w.cancer; 
  set w.cancer;
  if death=1 then fu=datdif(chemo_start_date,death_date,'act/act');
  else fu=datdiff(chemo_start_date,observation_finish_date,'act/act');
run;

Do also note, consitent casing, indentation, and the use of the code window (which is {i} above the post).

 

Super User
Posts: 17,912

Re: Merging TWO different variables into ONE Variable

Use an IF condition. 

 

If you'll only ever have a death date OR last follow up date use the coalesce function to use whichever variable is not missing. 

I included that as FU2 to demonstrate. 

 

Data w.cancer_fu;
Set w.cancer;

If death=1 the FU = death_date - chemo_start_date;
Else if death=0 then FU = observation_finish_date - start_date;

FU2 = coalesce(death_date, observation_finish_date) - start _date;

Run;

PS please don't include please help or urgent in your subject line. It doesn't add anything of value to your question. Preferably, take the time to acknowledge answers and mark questions as solved. 

Super User
Posts: 17,912

Re: Merging TWO different variables into ONE Variable

Also for dates, I personally find it easier to just subtract them rather than use datediff(). It's shorter for one. 

Solution
‎07-20-2016 08:20 PM
Super User
Posts: 9,687

Re: Merging TWO different variables into ONE Variable

data have;
input ID   Chemo_Start_Date : yymmdd10.  Death     Death_Date  : yymmdd10.   Observation_Finish_Date   : yymmdd10.;
format Chemo_Start_Date    Death_Date  Observation_Finish_Date   yymmdd10.;
cards;    
10001           2011-08-22            1            2012-03-01                2015-05-30                                             
10002           2010-05-15            0                      .               2015-05-30  
;
run; 


data want;
 set have;
 FU_Survival=coalesce(Death_Date,Observation_Finish_Date)-Chemo_Start_Date;
run;
Contributor
Posts: 23

Re: Merging TWO different variables into ONE Variable

Thank You all for your advice and Help.

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 320 views
  • 1 like
  • 4 in conversation