DATA Step, Macro, Functions and more

Conditionally running sas code

New Contributor
Posts: 4

Conditionally running sas code

I have reports that are run daily, however I do not want to run it if the office was close on the previous day. I have compiled a dataset that has 1 record for every day of the year with a binary for whether the office is open or not.

1. How can I check this at the beginning of a program versus "Yesterday"

2. How can I skip a whole lot of code, send an email to myself saying.. the program ran but no reports were necessary and then terminate the sas session. I am running this on batch in a windows nt environment on sas 9.1

Frequent Contributor
Posts: 83

Re: Conditionally running sas code

Ok so the way that I understand this is that you are going to have a dataset which has dates and a flag 1/0 if the office was open and therefore the program should run.

So this is just an example of what the dates would look like if they were read in for explanation purposes.

%macro opendt;

data open;

     input  date open ;

    informat date mmddyy10. open best2.;

    format date mmddyy10.;


010113    0

010213    1

010313    1



proc sql noprint;

select date

into open_list separated by '*'

from open

where open =1;


%let open_cntlist = &sqlobs;

%let print_date = %sysfunc(today(), MMDDYY10.);

%let yesterday = %sysfunc(intnx(day, %sysfunc(InputN(&print_date,MMDDYY10.)),-1),MMDDYY10.);

%do I = 1 %to &open_cntlist.;

%Let opendt = %scan(&open_list.,&I.,'*');

%if &opendt=&yesterday %then %do;

/*Insert code to run report*/

/*Insert email code for report*/


%else %do;

/*insert email code for not open yesterday*/



The report will be skipped and only an email saying that the office wasn't open will be sent if yesterday was not a day that was open.

Respected Advisor
Posts: 4,736

Re: Conditionally running sas code

As I understand your requirement you're trying to mimic what scheduling software like LSF normally would do for you. You will need some wrapper code which checks if programs should be run or not - and then do "stuff" accordingly.

If you want to take things a bit further you probably would also maintain a control table where you log job execution (started, finished, finished with errors and so on). In the wrapper you could also check the return codes of the batch submitted jobs and then send an email notification if a job fell over - or an email once all jobs finished executing. You might also consider to use a control table to only batch submit a job once a day - so when something falls over then you fix the job(s) and restart the scheduler which then only runs jobs not already successfully run on the day. ...but in the end: that's scheduling software functionality so when the requirements increase then using such software is may-be the better option than hand-coding.

Below a code example which might serve you as starting point.

data calendar;
  attrib Date format=date9. Run_flg length=$1;
  do date=today()-1 to today()+1;
    if date=today() then run_flg='1';
    else run_flg='0';

data programs;
  length path $300 prgm $40;
  infile datalines truncover dlm='|';
  input path prgm;

%macro check_rundate(calendar);
  data _null_;
    set &calendar (where=(date=today()));
    call symputx('run_flg',run_flg);
  %if &run_flg ne 1 %then

      data _null_;
        set programs;
        retain cmd1 '"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -CONFIG "C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg" nosplash noicon';
        length cmd2 cmd3 cmd4 $1000 cmd $4000;
        cmd2='-sysin "'||strip(path)||'\'||strip(prgm)||'"';
        cmd3='-log "'||strip(path)||'\'||scan(prgm,1,'.')||'_'||strip(put(datetime(),B8601DT.))||'.log"';
        cmd4='-print "'||strip(path)||'\'||scan(prgm,1,'.')||'_'||strip(put(datetime(),B8601DT.))||'.lst"';

        cmd=strip(cmd1)||' '||strip(cmd2)||' '||strip(cmd3)||' '||strip(cmd4)||' ';
        put cmd;

        call system(cmd);



Super User
Super User
Posts: 8,073

Re: Conditionally running sas code

To conditionally run code you normally need to use a macro. If you are not already using a macro in your program you can just wrap your existing program into a macro.

Once you are running a macro you can use %DO/%END blocks to control which parts run.

I would structure the program in this order:

- Test if reports need to run and set macro variable.

- If report need to run then run it.

- Produce email using the same macro variable to conditionally generate the content depending on whether reports were run or not.

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation