Hello all, I'm hoping someone can help, as I am fairly new with SAS. I couldn't find anywhere on the forum that specifically addressed what I am trying to do. I am trying to make the code run only if my date criteria is met.
I want to run (automate) the previous quarter of data, but only if it is >2 months after the end of the previous quarter, for example, I want to run data for 01JUL2019 - 30SEP2019, but run it no earlier than 01DEC2019 to make sure all the data is caught up.
Here is what I have now:
data _null_;
call symput('startdt',"'"||put(intnx('quarter', today(),-1, 'B'), date9.)||"'D");
call symput('ENDdt',"'"||put(intnx('quarter', today(), -1, 'E'), date9.)||"'D");
run;
%put &startdt.;
%put &ENDdt.;
So for this to run, date of intnx('quarter', today(),-1, 'B') should be equal to intnx('month', today(),-5, 'B')
Thanks, I moved a round a few things, but primarily changed "if intnx('month',end,now) >=2 then do;" to "if intck('month',end,now) = 3 then do;"
Does that seem right? seems to be giving me the output I want... Here is my full adjustment:
%let startdt=NOTE: WILL NOT RUN BECAUSE THE REPORT MUST BE RUN IN THE THIRD MONTH AFTER THE PREVIOUS QUARTER ENDED,EXAMPLE-IF THE QUARTER ENDS SEPT 30, YOU MUST WAIT UNTIL AT LEAST DEC 01 TO RUN;
%let enddt=NOTE: WILL NOT RUN BECAUSE THE REPORT MUST BE RUN IN THE THIRD MONTH AFTER THE PREVIOUS QUARTER ENDED,EXAMPLE-IF THE QUARTER ENDS SEPT 30, YOU MUST WAIT UNTIL AT LEAST DEC 01 TO RUN;
data _null_;
now=today();
start = intnx('quarter',now,-1,'b');
end = intnx('quarter',now,-1,'e');
if intck('month',end,now) = 3 then do;
call symput('startdt',"'"||put(start, date9.)||"'D");
call symput('ENDdt',"'"||put(end, date9.)||"'D");
call symput('MonthCount',"(((("||put(intck('month',end,now),10.)||" Months ))))");
end;
else call symput('MonthCount',"(((("||put(intck('month',end,now),10.)||" Months ))))");
run;
%put &startdt.;
%put &ENDdt.;
%put &MonthCount.;
Sounds like you want to test if there are two months between ENDDT and TODAY.
if intck('month',"&enddt"d,today()) >= 2 then ...
Hmm, that looks like it could work, but where in the code would I put that? because enddt will not yet be defined if the "if" statement with that comes first, correct?
@vanpeltm1785 wrote:
Hmm, that looks like it could work, but where in the code would I put that? because enddt will not yet be defined if the "if" statement with that comes first, correct?
Yes for that test to work you need to first do that calculation that gave you the end date you want to test.
You need to explain more about what you want to happen to get more precise advice. What do you want to do when it is time to run? What do you want to do when it is NOT time to run?
@vanpeltm1785 wrote:
If the date criteria is met, I would like the date outputs to be assigned, if the criteria is not met, just an error that wont assign the dates, or even a message that says the criteria was not met
%let startdt=NONE;
%let enddt=NONE;
data _null_;
now=today();
start = intnx('quarter',now,-1,'b');
end = intnx('quarter',now,-1,'e');
if intnx('month',end,now) >= 2 then do;
call symput('startdt',"'"||put(start, date9.)||"'D");
call symput('ENDdt',"'"||put(end, date9.)||"'D");
end;
else put 'NOTE: Will not run on ' now date9. ' since not 2 or more months after ' end date9. '.';
run;
%put &startdt.;
%put &ENDdt.;
I'm not sure what, but something here is not working, it could be this: "if intnx('month',end,now) >=2 then do;"
what would that resolve to numerically or date-wise?
also, I want the code to error if it is >=2, and only run if =3 months after, is this still the right way to put the if else?
lastly, the else statement does not resolve and output the error message
Have the scheduler run your program on first of March, June, September and December.
Thanks, I am hoping to figure out how to code it myself to better understand it, but this might be the eventual solution!
Thanks, I moved a round a few things, but primarily changed "if intnx('month',end,now) >=2 then do;" to "if intck('month',end,now) = 3 then do;"
Does that seem right? seems to be giving me the output I want... Here is my full adjustment:
%let startdt=NOTE: WILL NOT RUN BECAUSE THE REPORT MUST BE RUN IN THE THIRD MONTH AFTER THE PREVIOUS QUARTER ENDED,EXAMPLE-IF THE QUARTER ENDS SEPT 30, YOU MUST WAIT UNTIL AT LEAST DEC 01 TO RUN;
%let enddt=NOTE: WILL NOT RUN BECAUSE THE REPORT MUST BE RUN IN THE THIRD MONTH AFTER THE PREVIOUS QUARTER ENDED,EXAMPLE-IF THE QUARTER ENDS SEPT 30, YOU MUST WAIT UNTIL AT LEAST DEC 01 TO RUN;
data _null_;
now=today();
start = intnx('quarter',now,-1,'b');
end = intnx('quarter',now,-1,'e');
if intck('month',end,now) = 3 then do;
call symput('startdt',"'"||put(start, date9.)||"'D");
call symput('ENDdt',"'"||put(end, date9.)||"'D");
call symput('MonthCount',"(((("||put(intck('month',end,now),10.)||" Months ))))");
end;
else call symput('MonthCount',"(((("||put(intck('month',end,now),10.)||" Months ))))");
run;
%put &startdt.;
%put &ENDdt.;
%put &MonthCount.;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.