%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?
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?
If you're going down the conversion route, then yes.
@mhodge20 wrote:
I would need a %sysfunc and inputn?
@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;
use sysevalf:
%let x = '01JAN2018'd;
%macro loop;
%if %sysevalf(&x. ge '01Jan2017'd) %then %put leave;
%mend;
%loop
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.
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.
@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
@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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.