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

when comparing date variables in data steps, we have to convert the character date variables to numeric first with input statement. 

But in proc sql, it seems the character date variables can be directly compared.

 

In the following code, without converting to numeric, the character variables can be compared just like numeric:

 

proc sql;
      create table lb as
      select a.*, b.visit, b.visitnum
      from lb_ as a 
            left join sv as b 
                on a.usubjid=b.usubjid and (b.svstdtc <=a.lbdtc <= b.svendtc);
quit;

Does Proc Sql automatically convert the character date variables to numeric for the comparison? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

To expand on @Reeza's comment:

 

10Jan2017 will be before 9Jan2016 in character comparison because 1 < 9, first characters compared.

 

Or Aug 10, 2016 comes before Jan 10, 2017 because A < J

 

IF your character values are in the form of YYYYMMDD and have zeroes as place holders then you would be okay as 20170110 comes after 20170109 in character comparisons. Any other character valued dates are likely to fail and some quite spectacularly.

Also conversion to SAS date values will help you clean up bad dates such as Feb 30 and my favorite from an inherited data project August 67, 1985

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Does compared variables have same type in both datasets ?

If yes - then probably it is compared char to char.

If no - are you sure you get the right results ?

 

Please post sample of both datasets and their data types.

Reeza
Super User
But in proc sql, it seems the character date variables can be directly compared.

You CAN compare character date variables in a data step as well as SQL, but it will be alphabetic order, not numerical/calendar order.

 

EDIT: The variable types must be the same in either method, SQL or data step. 

 

 

ballardw
Super User

To expand on @Reeza's comment:

 

10Jan2017 will be before 9Jan2016 in character comparison because 1 < 9, first characters compared.

 

Or Aug 10, 2016 comes before Jan 10, 2017 because A < J

 

IF your character values are in the form of YYYYMMDD and have zeroes as place holders then you would be okay as 20170110 comes after 20170109 in character comparisons. Any other character valued dates are likely to fail and some quite spectacularly.

Also conversion to SAS date values will help you clean up bad dates such as Feb 30 and my favorite from an inherited data project August 67, 1985

fengyuwuzu
Pyrite | Level 9

Yes, my date is in yyyymmdd format (like '2015-09-03'). Now I understand why it runs okay. 

 

I did compare with converting to numeric values, and there is no difference. The key is the character date has to be in yyyymmdd format. 

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
  • 4 replies
  • 4256 views
  • 0 likes
  • 4 in conversation