DATA Step, Macro, Functions and more

Comparing Macro Variable date to date literal

Reply
Occasional Contributor
Posts: 10

Comparing Macro Variable date to date literal

%let x = 01/01/2018;

 

%macro loop;

 

%if &x. ge '01Jan2017'd %then leave;

 

%mend;

 

 

How do I accurately compare the date that is the macro variable and the date literal? 

Super User
Posts: 23,321

Re: Comparing Macro Variable date to date literal

1. Either define the macro variable as a data literal like you did in the code

2. Use INPUT to convert the macro variable to a  date literal to allow the comparison. 

 


mhodge20 wrote:

%let x = 01/01/2018;

 

%macro loop;

 

%if &x. ge '01Jan2017'd %then leave;

 

%mend;

 

 

How do I accurately compare the date that is the macro variable and the date literal? 


 

Occasional Contributor
Posts: 10

Re: Comparing Macro Variable date to date literal

I would need a %sysfunc and inputn?
Super User
Posts: 23,321

Re: Comparing Macro Variable date to date literal

If you're going down the conversion route, then yes. 

 


mhodge20 wrote:
I would need a %sysfunc and inputn?

 

Respected Advisor
Posts: 2,833

Re: Comparing Macro Variable date to date literal


mhodge20 wrote:

%let x = 01/01/2018;

 

%macro loop;

 

%if &x. ge '01Jan2017'd %then leave;

 

%mend;

 

 

How do I accurately compare the date that is the macro variable and the date literal? 


%let x = '01JAN2018'd;
%macro loop;
%if &x. ge '01Jan2017'd %then leave;
%mend;
--
Paige Miller
Occasional Contributor
Posts: 10

Re: Comparing Macro Variable date to date literal

Posted in reply to PaigeMiller
the %let is actually a loop im spitting out not me writing a date literal.

%do II = 1 %to 12
%let x = %sysfunc(intnx(month,&termdate.,&ii,B),Date9.);



%if &x. ge '01FEB2018'd %then leave;
%else;
PROC Star
Posts: 1,592

Re: Comparing Macro Variable date to date literal

use sysevalf:

%let x = '01JAN2018'd;
%macro loop;
%if %sysevalf(&x. ge '01Jan2017'd) %then %put leave;
%mend;

%loop
Super User
Posts: 6,637

Re: Comparing Macro Variable date to date literal

You do have more work ahead of you. 

 

In macro language, 01/01/2018 means 1, divided by 1, divided by 2018.  That's why you need the conversion.  (Yes, you are mentioning the correct tools.)

 

However, macro language will not interpret date literals as is.  You will need to wrap the date literal with:

 

%sysevalf('01Jan2017'd)

 

Finally, I don't think LEAVE is a macro language statement.  I could be wrong here.  But I believe that %LOOP is saying to make the comparison, and if the comparison is true add the word LEAVE to your SAS program.  To correct that, you would have to show more of what is inside the definition of %LOOP. 

Occasional Contributor
Posts: 10

Re: Comparing Macro Variable date to date literal

Posted in reply to Astounding
If the comparison is true then leave the loop.
Super User
Posts: 23,321

Re: Comparing Macro Variable date to date literal

You probably need a %LEAVE

And if you're using INTNX it will automatically return a SAS date so you don't need to worry about the format/appearance.

Occasional Contributor
Posts: 10

Re: Comparing Macro Variable date to date literal

I thought the macrovariable would be saved as a character string and I would have to convert the string to date in order to compare it with a date literal?
PROC Star
Posts: 1,592

Re: Comparing Macro Variable date to date literal

[ Edited ]

@Reeza There is no %leave like leave in datastep.  I wonder why the OP is so much into macro in the first place. In most cases, perhaps there is no need for maco. 

 

Anyway to exit the loop, if the OP is using a iterative %do loop. Once the %if condition is satisfied, OP has to reset the the do index variable to stop value or use %do %until(conditon=true) . My 2 cents

Super User
Posts: 23,321

Re: Comparing Macro Variable date to date literal

Posted in reply to novinosrin

@novinosrin You're correct.

You can also use a standard %IF/%THEN/%ELSE to control the loop, but we'd need more of the code or the requirements to understand what the best approach is.  For the OP, there's an example of looping with dates in the SAS macro appendix.

 

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&docset...

Occasional Contributor
Posts: 10

Re: Comparing Macro Variable date to date literal

Posted in reply to novinosrin
Spoiler
 
Spoiler
 

I had the loop spit out a Date9. and then wrapped it in %str(%') to produce a date literal.

 

%let x = %sysfunc(intnx(month,&termdate.,&ii,B),Date9.);
%let t = %str(%')&x.%str(%')d;

%put &t. 

'01FEB2015'd

 

%if &t. ge '01FEB2018'd %then leave;

 

 

Super User
Posts: 6,637

Re: Comparing Macro Variable date to date literal

Spoiler:  You are still a long way from having useful code.

 

The word LEAVE is still not going to be useful.

 

The comparison is still incorrect even if there are no syntax errors.  (Try using '01SEP2015'd vs. '01FEB2018'd for example.)

 

Rather than fix those issues, you would be better advised to show how %LOOP is supposed to be used.

Ask a Question
Discussion stats
  • 18 replies
  • 254 views
  • 1 like
  • 5 in conversation