BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vanpeltm1785
Obsidian | Level 7

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')

1 ACCEPTED SOLUTION

Accepted Solutions
vanpeltm1785
Obsidian | Level 7

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

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

Sounds like you want to test if there are two months between ENDDT and TODAY.

if intck('month',"&enddt"d,today()) >= 2 then ...
vanpeltm1785
Obsidian | Level 7

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?

 

 

Tom
Super User Tom
Super User

@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
Obsidian | Level 7
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
Tom
Super User Tom
Super User

@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.;
vanpeltm1785
Obsidian | Level 7

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

vanpeltm1785
Obsidian | Level 7

Thanks, I am hoping to figure out how to code it myself to better understand it, but this might be the eventual solution!

Tom
Super User Tom
Super User
Just play with it. Save the results of the INTNX() function calls into variables and print them.
Why not just use MONTH(TODAY()) and check whether it is 1 (January), 4 (April), 7 (July) or 10 (October)?
vanpeltm1785
Obsidian | Level 7

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1770 views
  • 0 likes
  • 3 in conversation