Acknowledgement: Thanks KM for supplying this answer.
Question:
I am comparing a date variable and date and time variable and I am not getting the right answer.
Below, I am creating a variable CUT_DATE. I compare it against DateModified, which includes year, month, date, and time. I am not getting the right answer. Please see the table after the syntax. I should get “0” in USE_THIS (the highlighted row). I think one solution may be to create CUT_DATE based on year, month, date, AND TIME. What function do I use for this instead of MDY?
/*REMOVE ALL DATA ENTERED BEFORE A FIXED DATE*/
%let _year=2018;
%let _month=10;
%let _date=01;
/*table 1*//*table 1*//*table 1*//*table 1*/
/*table 1*//*table 1*//*table 1*//*table 1*/
data table1b;set access1.table1;
cut_date=mdy(&_month,&_date,&_year);
FORMAT cut_date date9. ;
USE_THIS=0;
if DateModified > cut_date then USE_THIS=1;
run;
DateModified cut_date USE_THIS
31OCT2018:00:00:00 1-Oct-18 1
31OCT2018:00:00:00 1-Oct-18 1
05MAR2018:00:00:00 1-Oct-18 1
31OCT2018:00:00:00 1-Oct-18 1
31OCT2018:00:00:00 1-Oct-18 1
ANSWER from SAS:
You can use the DHMS function to create a datetime value. Another alternative would be to use the DATEPART function on the DateModified variable.
Example:
data table1b;
set table1;
cut_date=mdy(&_month,&_date,&_year);
cut_date1=dhms(mdy(&_month,&_date,&_year),0,0,0);
FORMAT cut_date date9. cut_date1 datetime18. ;
USE_THIS=0;
use_this1=0;
if DateModified > cut_date1 then USE_THIS=1;
if datepart(datemodified) > cut_date then use_this1=1;
run;