BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lruan1
Calcite | Level 5

Hi all, I am brand new to DS2 and I wonder if you can give any suggestions on comparing dates. The logic is pretty straightforward. I convert the string to the date and then convert to the double for comparing purpose. Everything is fine till I change the hard coded '2018-06-04' with a variable 'date_String'. Then the date conversion step fails. Do I miss anything? Thanks very much! Here is my code:

 

data _null_;
     method run();
         dcl date dt1 dt2 dt3;
         dcl double ddt1 ddt2 ddt3 we;
          dcl varchar(10) date_String;

 

          date_String = '2018-06-04'

           Dt1 = date date_String;
           Dt2 = date '2018-06-05';
           Dt3 = date '2019-01-04';


              ddt1 = to_double(dt1);
              ddt2 = to_double(dt2);
             ddt3 = to_double(dt3);

 

             put ddt1= ddt2= ddt3=;

 

             if ddt1 < ddt3 AND ddt2 < ddt3 then we = 5.3;
             put we=;
end;
enddata;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@lruan1

You need to use the to_date() function.

      dcl varchar(10) date_String;
      date_String = '2018-06-04';
      Dt1 = to_date(date_String);

 

I don't understand why you believe you have to convert the Date type to Double for comparison. Using Date should be more than fine.

 

May be this link will be helpful to you: http://support.sas.com/kb/51/769.html 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

@lruan1

You need to use the to_date() function.

      dcl varchar(10) date_String;
      date_String = '2018-06-04';
      Dt1 = to_date(date_String);

 

I don't understand why you believe you have to convert the Date type to Double for comparison. Using Date should be more than fine.

 

May be this link will be helpful to you: http://support.sas.com/kb/51/769.html 

lruan1
Calcite | Level 5

Thanks very much, Patrick! As you said, to_data() function is sufficient to compare the dates without any more conversion. It works great!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 827 views
  • 2 likes
  • 2 in conversation