DATA Step, Macro, Functions and more

Date format in Data Step comparison

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 107
Accepted Solution

Date format in Data Step comparison

Hi,

 

I would like to filter out 'Stay_Date' before today. How could I change the following code in Data step?

 

Data Work.A;

     Set Work.B;

     Where Stay_Date LE 'sysdate'd;

Run;

 

It returns error: Invalid Date / Time. As 'Stay_Date' format is '2015-12-31'. But the following code is fine:

 

Data Work.A;

     Set Work.B;

     Where Stay_Date LE '25APR2017'd;

Run;

 

How could I use System date instead of a particular one? Thank you!


Accepted Solutions
Solution
‎04-25-2017 11:23 AM
Trusted Advisor
Posts: 1,607

Re: Date format in Data Step comparison

[ Edited ]

Crubal wrote:

 

It returns error: Invalid Date / Time. As 'Stay_Date' format is '2015-12-31'


 

It seems you are confusing a few issues here. Stay_Date (without the quotes) is 2015-12-31 without the quotes, and this is a formatted date value, it is not a character string (which is what the quotes would indicate). Thus you need to compare Stay_Date to a SAS date value, for example:

 

 

where stay_date le today();

 

 No need for macro variables, or double-quotes to all the macro variable to be resolved.

View solution in original post


All Replies
Super User
Posts: 5,071

Re: Date format in Data Step comparison

A couple of small changes:

 

     Where Stay_Date LE "&sysdate9"d;

 

It's important to use double quotes, so that the macro variable can resolve.

Frequent Contributor
Posts: 107

Re: Date format in Data Step comparison

Thank you that would be helpful for future Macro procedure!
Solution
‎04-25-2017 11:23 AM
Trusted Advisor
Posts: 1,607

Re: Date format in Data Step comparison

[ Edited ]

Crubal wrote:

 

It returns error: Invalid Date / Time. As 'Stay_Date' format is '2015-12-31'


 

It seems you are confusing a few issues here. Stay_Date (without the quotes) is 2015-12-31 without the quotes, and this is a formatted date value, it is not a character string (which is what the quotes would indicate). Thus you need to compare Stay_Date to a SAS date value, for example:

 

 

where stay_date le today();

 

 No need for macro variables, or double-quotes to all the macro variable to be resolved.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 123 views
  • 2 likes
  • 3 in conversation