BookmarkSubscribeRSS Feed
mhodge20
Calcite | Level 5

%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? 

18 REPLIES 18
Reeza
Super User

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? 


 

mhodge20
Calcite | Level 5
I would need a %sysfunc and inputn?
Reeza
Super User

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

 


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

 

PaigeMiller
Diamond | Level 26

@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
mhodge20
Calcite | Level 5
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;
novinosrin
Tourmaline | Level 20
use sysevalf:

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

%loop
Astounding
PROC Star

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. 

mhodge20
Calcite | Level 5
If the comparison is true then leave the loop.
Reeza
Super User

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.

mhodge20
Calcite | Level 5
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?
novinosrin
Tourmaline | Level 20

@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

Reeza
Super User

@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...

mhodge20
Calcite | Level 5
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;

 

 

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 4119 views
  • 1 like
  • 5 in conversation