Write and run SAS programs in your web browser

Conditional Start

Reply
Contributor
Posts: 60

Conditional Start

Hi there,

 

What I want to do is run a job based on the date of the month which can vary.

So I was thinking of using conditional start.

 

If I had a table with a list of dates, and that date matched "&sysdate"d - 1 I would want the job to run, but if not stop the job and dont process the job.

 

Has anyone any ideas on how to achieve this, I have looked for examples but am struggling on it.

 

Thanks,

Aidan 

Super User
Posts: 7,407

Re: Conditional Start

Do you mean that you have a dataset with dates and jobnames?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Conditional Start

I would have 1 data set with multiple different dates and then if a date within that data set matches today-1, i will run  ajob, if not, do nothing

Super User
Posts: 7,407

Re: Conditional Start


Aidan wrote:

I would have 1 data set with multiple different dates and then if a date within that data set matches today-1, i will run  ajob, if not, do nothing


Ok, so I suggest:

%macro run_job_cond;

%let flag=no;

data _null_;
set have;
if date = today() - 1 then call symput('flag','yes');
run;

%if &flag=yes
%then %do;
/* run the job here */
%end;

%mend;

%run_job_cond

How you run the job depends on what the "job" is. If it is a SAS code suitable for %include, then you could do that. If it is something that should be run in a clean batch job, use one of the methods to call external commands and use SAS from the commandline.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,360

Re: Conditional Start

Here's one way to proceed.  Take the job you might want to run, and add this to the beginning of it.  Assuming you have your list of dates stored in a SAS data set with one date value per observation:

 

data _null_;

call symputx('date_found', 'endsas;');

set list_of_dates;

if date = "&sysdate9"d - 1 then do;

   call symputx('date_found', ' ');

   stop;

end;

run;

 

&date_found

 

When there is no matching date, &DATE_FOUND generates ENDSAS; and the program stops.  Just run the program every day.

 

There are other non-SAS approaches that use job schedulers, depending on your operating system.  But this would be an easy SAS-based approach.

 

 

Contributor
Posts: 60

Re: Conditional Start

Thank you both, I have both implemented so hopefully it will work ,  I will know on the 20th so will accpet solution for whichever works.

 

Thanks again for the quick reply 

 

 

Super User
Posts: 7,407

Re: Conditional Start

And as for "running a job every day": if you have the luxury of running SAS on UNIX, a simple crontab entry will do the job.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Conditional Start

If in node 1 the dates match, will it progress onto node 2 as per attachment?

See code in node 1 below

 

data _null_;
call symputx('date_found', 'endsas;');
set &_INPUT1;
if date = "&sysdate9"d - 1 then do;
call symputx('date_found', ' ');
stop;
end;
run;

&date_found

Attachment
Super User
Posts: 7,407

Re: Conditional Start

I guess you're mixing things here. If you work with a process flow, you should use the controls available in process flows. So I'd add a node that checks your dataset and sets a macro variable, which you can then use in conditions in your process flow.

What @Astounding and I have given you are options to use with stand-alone SAS code.

You could try to wrap my solution around the code of your nodes, but you would lose the process-flow functionality on the way, as you'll end up with a single monolithic piece of code.

The endsas statement @Astounding uses immediately terminates the SAS workspace server session, so EG would have to start a new workspace server to continue (which would lose all macro variables etc)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Conditional Start

startement the next node has code like the below;

proc sql;
create view work.W2LB59M as
select
(datepart(BILLING_RUN_DT)) as BILLING_RUN_DT length = 8
format = date11.
informat = DATETIME20.
label = 'BILLING_RUN_DT'
from &SYSLAST
where "&Sysdate"d-1>=datepart(BILLING_RUN_DT)
;
quit;

%let SYSLAST = work.W2LB59M;

 

 

 

So I need to put that here as highlitged with "CODE TO PROCESS"

 

 

data _null_;
call symputx('date_found', 'endsas;');
set &_INPUT1;
if date = "&sysdate9"d - 1 then do  "CODE TO PROCESS";
call symputx('date_found', ' ');
stop;
end;
run;

&date_found

Super User
Posts: 7,407

Re: Conditional Start

From the screenshot in your rtf I guess you use EG and process flows?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Conditional Start

Yeah I use DI more often than not, but have used some user written in the past

Super User
Posts: 7,407

Re: Conditional Start

Since I do not work with DI Studio, you need to take my suggestions from now on with a grain of salt.

If DI Studio works like EG, you can set conditions for each node in the flow. One of the options (in EG, mind) for such a condition is to query the value of a macro variable.

If you now enter the data step code that checks the control dataset into a node at the beginning like this:

%let continue=no;
data _null_;
set control;
if date = today() - 2
then do;
  call symput('continue','yes');
  stop;
end;
run;

You can then add a condition on the following nodes that queries for &continue=yes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Conditional Start

Ok this is where I am at now.

 

I have a table with the last run date on its own, being the 15th of Feb 2017.

 

This is node 1.

Node 2 contains the following, so I want to run a test if the criteria matches which is should.

 


%let continue=no;
data temp1;
set &_INPUT1;
if BILLING_RUN_DT = '15Feb2017'd
then do;
call symput('continue','yes');
stop;
end;
run;

 

Then node 3 does the processing of the rest of the DI job which has a where clause of &continue=yes

 

This still doens't seem to be executing from node 3 with any data even though I have data that matches to the 15th of Feb so the condition should be yes.

 

Has anyone any ideas at this point?

 

Thanks

Aidan 

Attachment
Ask a Question
Discussion stats
  • 13 replies
  • 279 views
  • 3 likes
  • 3 in conversation