0
votes

I often have data that has a date1 and a date2. Date1 is the date we guess will have the event and date2 an event. I usually need to make 2 dummy variables where I increment date1 forwards a week and backwards then compare with the other 2. However I keep thinking there must be a better way to create a date range and then compare with a second date!

Is there a way to do this in sas? Basically I want to take date1 and date2 and make this dataset and am wondering if I MUST create 2 additional variables (date1-7 days and date1+7days) Input datset:

DATE1         DATE2     



10/23/2014  2/12/2015     


2/12/2015   2/10/2015     

Current output:

DATE1_wk_before Date1_wk_after Date2 In_range_indicator

10/16/2014               10/30/2014       2/12/2015       0 


2/05/2015                 2/19/2015       2/10/2015       1

Where In_range_indicator = 1 if date is in the range and 0 if not in the range

I want to know if I can do it just where I do something like In_range_indicator= 1 where Date2 is in range(week before date1 , week after date1) without creating 2 extra sets of data. It seems a waste of time.

I am LITERALLY adding 7 days and subtracting 7 days before and after and it seems a bad way to do this.

3
You don't provide enough information for what you're doing here - this doesn't really make sense. Please provide example initial datasets, then what you're trying to determine, and the final desired dataset, plus the code you now use to get there. - Joe

3 Answers

0
votes

You seems to just want to set the value of a variable based on a condition. No need to get too clever with it, just if and else in your data step:

if date2 ge date1-7 and date2 le date1+7 then ind=1;
else ind=0;
0
votes

Agree with @DWal, simple if and else statement can help. You can also use IFN function.

data mydates;
infile datalines missover;
input (date1-date2) (:mmddyy10.);
In_range_indicator=ifn( date1-7 <= date2 <= date1+7 , 1,0);
format date1-date2 yymmdd10.;
datalines4;
10/23/2014 2/12/2015
2/12/2015 2/10/2015
;;;;
run;
proc print data=mydates;run;
0
votes
if abs(date2-date1)<7 then ind=1; else ind=0;