Better way to make/compare date ranges? - date

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.

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;

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;

if abs(date2-date1)<7 then ind=1; else ind=0;

Related

intck() giving negative value

I am new to SAS and I am having trouble with finding the difference between 2 dates.
I have 2 columns: checkin_date and checkout_date
the dates are in mmddyy10. format (mm/dd/yyyy).
I have used the following code:
stay_days= intck('day', checkin_day, checkout_day);
I am getting the right values for dates in the same month but wrong values for days that are across 2 months. For example, the difference between 02/06/2014 and 02/11/2014 is 5. But the difference between 1/31/2014 and 2/13/2014 is -18 which is incorrect.
I have also simply tried to subtract them both:
stay_day = checkout_day - checkin_day;
I am getting the same result for that too.
My entire code:
data hotel;
infile "XXXX\Hotel.dat";
input room_no num_guests checkin_month checkin_day checkin_year checkout_month checkout_day checkout_year internet_used $ days_used room_type $16. room_rate;
checkin_date = mdy(checkin_month,checkin_day,checkin_year);
informat checkin_date mmddyy.;
format checkin_date mmddyy10.;
checkout_date = mdy(checkout_month,checkout_day,checkout_year);
informat checkout_date mmddyy.;
format checkout_date mmddyy10.;
stay_day= intck('day', checkin_day, checkout_day);
Your problem is a typo - using wrong variables in intck() function. You are using variables "xxx_DAY" which is the DAY of month instead of the full DATE. Change to stay_day= intck('day', checkin_date, checkout_date);
Your data probably has the date values in the wrong variables. When using subtraction the order should be ENDDATE - STARTDATE. When using INTNX() function the order should be from STARTDATE to ENDDATE. In either case if the value in the STARTDATE variable is AFTER the value in the ENDDATE variable then the difference will be a negative number.
Perhaps you need to clean the data?
The only way to get -18 comparing 2014-01-31 and 2014-02-13 would be if you extracted the day of the month and subtracted them.
diff3 = day(end) - day(start);
which would be the same as subtracting 31 from 13.
Example using your dates:
data check;
input start end ;
informat start end mmddyy.;
format start end yymmdd10.;
diff1=intck('day',start,end);
diff2=end-start;
cards;
02/06/2014 02/11/2014
1/31/2014 2/13/2014
;
Results:
Obs start end diff1 diff2
1 2014-02-06 2014-02-11 5 5
2 2014-01-31 2014-02-13 13 13

SAS date swap year and day

I am working with a dataset containing a date variable with the format MMDDYY10..
The problem is, that the day, month and the year have been swapped.
The data set as it looks now:
Obs Date
1 11/01/1931
2 11/06/1930
3 12/02/2003
4 12/07/2018
What I would like is a date variable with the format DDMMYY10., or a similar:
Obs Date
1 31/01/2011
2 30/06/2011
3 03/02/2012
4 18/07/2012
Observation 1 is hence written as the 1st of November 1931, but really it is the 31st of January 2011.
Does anyone know how I can change this?
Looks like you read the original raw data using the wrong INFORMAT. Most likely you had data in YYMMDD format and you read it as MMDDYY. You can use the PUT() and INPUT() functions to attempt to reverse it.
data have ;
input date mmddyy10.;
newdate = input(put(date,mmddyy6.),yymmdd6.);
format date newdate yymmdd10. ;
put (date newdate) (=);
cards;
11/01/1931
11/06/1930
12/02/2003
12/07/2018
;;;;
Results:
date=1931-11-01 newdate=2011-01-31
date=1930-11-06 newdate=2011-06-30
date=2003-12-02 newdate=2012-02-03
date=2018-12-07 newdate=2012-07-18

Finding last Sunday and going 4 weeks backward every week in SAS

I have a SAS job that runs every Thursday, but sometime it need to run on Wednesday, and maybe Tuesday evening. The job collects some data in 4 week intervals up until the closest Sunday. For example, today we have 19Mar2015, and I need data until 15Mar2015.
data get_some_data;
set all_the_data;
where date >= '16Feb2015' and date <= '15Mar2015';
run;
Next week I have to manually change the date parameters too
data get_some_data;
set all_the_data;
where date >= '23Feb2015' and date <= '22Mar2015';
run;
Anyway I can automate this?
I'll expand on the suggestion from #user667489 as it could take you a while to work it out. The key is to use the week time interval, which by default starts on a Sunday (you can change this with a shift index, read this for further details)
So your query just needs to be :
where intnx('week',today(),-4)<date<=intnx('week',today(),0);
Use the INTNX function to regress the date back to last Sunday:
data get_some_data;
set all_the_data;
lastsun=intnx('week',today(),0);
/*where date >= '23Feb2015' and date <= '22Mar2015';*/
where date between lastsun-27 and lastsun;
run;
You can try getting the last sunday date using weekday function and then using INTNX get the 4 week back date from that sunday date. Check the below ref code :
data mydata;
input input_date YYMMDD10.;
/* Loop to get the last sunday date, do the processing
and get out of loop */
do i =0 to 7 until(last_sunday_date>0);
/* Weekday Sunday=1 */
if weekday(sum(input_date,-i))=1 then do;
last_sunday_date=sum(input_date,-i);
/* INTNX to get the 4 week back date */
my_4_week_start=intnx('week',last_sunday_date,-4);
end;
end;
format input_date last_sunday_date my_4_week_start yymmdd10.;
datalines4;
2015-03-01
2015-03-07
2015-03-14
2015-03-21
2015-03-28
2015-04-05
2015-04-13
2015-04-20
;;;;
run;
proc print data=mydata;run;
let me know if this helps!

Check if the difference between dates is exactly 'n' months in expression SSRS

In my quarterly report Im trying to validate the two parameters StartDate and EndDate.
I first check if the difference between the dates is 2 months:
Switch(DateDiff(
DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2,
"Error message")
Then I try to add whether the StartDate is the first day of month AND EndDate is last day of month:
And (Day(Parameters!StartDate.Value) <> 1
And Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value)))
So the whole expression looks like this:
Switch(DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2
And
Parameters!IsQuarterly.Value = true
And
Day(Parameters!StartDate.Value) <> 1
And
Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value))<>1),
"Error: Quarterly report must include 3 months")
But It works wrong when the difference between dates is still 2 months, but StartDate and EndDate are not first and last day of the whole period.
I'd appreciate any help :)
I would say just change the implementation Add another two Parameter With Quarter and Year
Quarter like Q1,Q2,Q3 & Q4 with Value 1,2,3 & 4 respectively and year 2012,2013,2014 & so on
Now based on the parameter selected Qtr & Year set Default value of start & End Date
=DateSerial(Parameters!Year.Value), (3*Parameters!Qtr.Value)-2, 1) --First day of Quarter
=DateAdd("d",-1,DateAdd("q",1,Parameters!Year.Value, (3*Parameters!Qtr.Value)-2, 1))) --Last day of quarter
Doing this no need to do any validation bcz its always get the correct Date Difference.
Other Reference
First day of current quarter
=DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)
Last day of current quarter
=DateAdd("d",-1,DateAdd("q",1,DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)))

How can I compare two dates in vbscript/ASP?

Using ASP classic, I need to somehow compare two dates with each other. How can I do this?
Date1 = #rs["date"]#
Date2 = #12/1/2009#
If DateDiff("d", Date1, Date2) > 1 Then
response.write "This date is before 12/1/2009"
Else
response.write "This date is after 12/1/2009"
End If
If Date1 > Date2 Then
' Date1 occurred after Date 2
End If
Use >, < and = like comparing numbers (and >=, <= and <> too). Smaller dates are more historic.
This of course assumes that Date1 and Date2 are actually Date or DateTime objects. If they aren't, you'll need to convert them to Date objects first using CDate().