Hello everyone,
I want to send an email if a file is in a directory or not on the 10th day of the month.
I have a macro (rc) that is equal to 1 if the file exists, or equal to 0 if the file doesn't exist.
I have a problem when the file doesn't exist and if we are the 10th day of the month.
I did a test with today's date.
Here is my code :
%let _date=%sysfunc(today(),DDMMYY10.); /* equals to 09/01/2020 in my case */
%let my=%sysfunc(today(),MMYYS.); /* equals to 01/2020 in my case */
%Macro date_success(rc=);
%if &rc.=0 and &_date.=09/&my. %then %do; /*Here we have normally rc=0 AND _date = 09/&my. = 09/01/2020*/
%put "====== SUCCESS =============";
%end;
%else %do;
%put "=========== FAILED =========";
%put "10/&my.";
%end;
%mend;
%date_success(rc=1) /*Return FAILED as normally planed*/
%date_success(rc=0) /*Doesn't return success, it seems that &_date.=09/&my. doesnt work */
Thank you in advance for your help !
Best regards,
Hi @Mick_lb
It is never a good idea to format your macro variables.
Here is an attempt to avoid this issue:
%let _date=%sysfunc(today());
%Macro date_success(rc=);
%if &rc.=0 and %sysfunc(day(&_date.)) ne 10 %then %do;
%put "====== SUCCESS =============";
%end;
%else %do;
%put "=========== FAILED =========";
%put %sysfunc(putn(&_date.,date9.));
%end;
%mend;
%date_success(rc=1)
%date_success(rc=0)
What is that it is NOT doing? Seems to work for me since today is 09JAN2020.
1051 %let _date=%sysfunc(today(),DDMMYY10.); /* equals to 09/01/2020 in my case */ 1052 %let my=%sysfunc(today(),MMYYS.); /* equals to 01/2020 in my case */ 1053 %Macro date_success(rc=); 1054 %put &=rc &=_date &=my ; 1055 %if &rc.=0 and &_date.=09/&my. %then %do; /*Here we have normally rc=0 AND _date = 09/&my. = 1055! 09/01/2020*/ 1056 %put "====== SUCCESS ============="; 1057 %end; 1058 %else %do; 1059 %put "=========== FAILED ========="; 1060 %put "10/&my."; 1061 %end; 1062 %mend; 1063 1064 %date_success(rc=1) RC=1 _DATE=09/01/2020 MY=01/2020 "=========== FAILED =========" "10/01/2020" 1065 %date_success(rc=0) RC=0 _DATE=09/01/2020 MY=01/2020 "====== SUCCESS ============="
Where do the real values of _DATE and MY come from? Is 09 really in the code? Note that to SAS the string '9/01/2020' is not the same as the string '09/01/2020' so you need to generate those leading zeros consistently.
You are right, this comparison will not work properly:
&_date.=09/&my.
SAS interprets the slash as division, so it computes 9 divided by &my.
Because macro language performs integer arithmetic (dropping any remainders), it is quite likely that 09/&my. yields the exact same result as 10/&my.
If your macro variables have no leading or trailing blanks, you can get around that by adding double quotes:
"&_date."="09/&my."
Right. The implicit %EVAL() function call that %IF statement uses will evaluate strings that look like integer arithmetic.
For your DD/MM/YYYY pattern that is big problem as whatever value day/month gives once you divide it by the much larger year value and you will end up with zero on both sides of the comparison operator.
1160 %put 09/01/2019 -> %eval(09/01/2019) ; 09/01/2019 -> 0
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.