BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12
I want to add few steps at the end of all programs which is located in particular folder. One way to do this is adding INCLUDE statement in all the programs.

But I don't want to touch all the programs to do this task as there are 200+ program. Is there any other way to achieve this objective without touching all the programs?

13 REPLIES 13
SASKiwi
PROC Star

One way would be to create a controlling SAS program whose job is to run any of your 200 plus programs using a %INCLUDE, then does the extra steps following it.

 

You could even pass a parameter into this controlling program which specifies which of your programs to run so you don't need to keep changing it all the time. This functionality is particularly useful when running your programs in batch mode. 

Babloo
Rhodochrosite | Level 12
May I request you to guide me with one small example?
SASKiwi
PROC Star

@Babloo  - the controlling program approach is really very simple:

 

%include "<MySASProgram.sas>"; * Put your program name here;

* Now add the code you want to run afterwards for each of your programs here;

<Common program code>

Then you could enhance it using SYSPARM: https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n1ug7uhp2d4vpmn149431l4pcucf.htm...

 

Start SAS like so: sas MyControlProgram.sas -sysparm = "MySASProgram"

data _null;
  * This reads the value of sysparm;
  MyProgramName = sysparm(); 

  * This creates a %include statement then runs it;
  call execute('%include "' !! strip(MyProgramName) !! '.sas";');
run;
  

Note, change the sysparm to change the program you want to run. The above program does not change at all.

Babloo
Rhodochrosite | Level 12
Well, assume SAS programs which I have in the folder is program1.sas,
program2.sas and program3.sas.

And the common program which I want to execute at the end of each program
is common_program.sas.

Then how will you fit these program(s) in your proposed solution?
Patrick
Opal | Level 21

@Babloo 

Assuming this is still about writing job status information to some table and that you will be running your code in batch:

I've faced in the past a similar challenge where I had to add such processing at the beginning and the end of each job after everything had already been built and unit tested. The approach I've taken there: 

1. Create an autocall macro for writing the status information

2. Add the call to this autocall macro via system options -initstmt and -termstmt as part of the batch string.

 

This approach allowed me to implement such loggin without having to touch the DIS jobs.

Babloo
Rhodochrosite | Level 12
Could you please point me to the document to implement the approach which
you suggested?
Kurt_Bremser
Super User

@Babloo wrote:
I want to add few steps at the end of all programs which is located in particular folder. One way to do this is adding INCLUDE statement in all the programs.

But I don't want to touch all the programs to do this task as there are 200+ program. Is there any other way to achieve this objective without touching all the programs?


Do it in the script that runs the programs. That way you do not have to touch any existing SAS codes.

Babloo
Rhodochrosite | Level 12
We're executing the program via scheduling tool called Stone Branch. So you
are suggesting me to add and call my program which I want to add in after
the completion of each program?
Kurt_Bremser
Super User

At one point, your scheduler has to run SAS. Most likely this is done by running a shell script like sasbatch.sh which is found in the SASApp/BatchServer directory of your BI server configuration. Find that script (your StoneBranch admins can help you in that), make a copy of it, expand that copy so that a small SAS program which records information is run before and after the main call, and use that copy.

 


@Babloo wrote:
We're executing the program via scheduling tool called Stone Branch. So you
are suggesting me to add and call my program which I want to add in after
the completion of each program?

 

Patrick
Opal | Level 21

@Babloo wrote:
We're executing the program via scheduling tool called Stone Branch. So you
are suggesting me to add and call my program which I want to add in after
the completion of each program?

 

The scheduler will call SAS programs issuing a command like:

.../sasbatch.sh -sysin <.../your sas program.sas>  .....

 

So here you can add the options -initstmt and -termstmt

 

I.e. if you want to execute your login macro right after the DIS code (passed in via -sysin) then it would be something like 

.../sasbatch.sh -sysin <.../your sas program.sas>  .....-termstmt '%mymacro()'

 

The macro itself will execute in the same session and though you can use in this macro any of the DIS generated global macro variables. 

Patrick
Opal | Level 21

@Babloo 

And what @Kurt_Bremser proposes is also a viable approach and something I've done successfully in the past. 

....or you can also as a combination of both approaches add the -....stmt options to your copy of the sasbatch.sh - or eventually even better to a copy of sasbatch_usermods.sh

Babloo
Rhodochrosite | Level 12

I don't understand this part 'you can also as a combination of both approaches add the -....stmt options to your copy of the sasbatch.sh - or eventually even better to a copy of sasbatch_usermods.sh'

Patrick
Opal | Level 21

@Babloo wrote:

I don't understand this part 'you can also as a combination of both approaches add the -....stmt options to your copy of the sasbatch.sh - or eventually even better to a copy of sasbatch_usermods.sh'


sasbatch.sh is just a shell script which takes input parameters and constructs a batch command line which it then executes. Because it's just a shell script you can amend it to do whatever else you need. If you look into the script then - if I remember right - there is already logic built which adds some of the required options if they haven't been passed in as parameters. You could extend this logic and also add -initstmt and -termstmt in the same manner.

The script is cascaded in the same way the config files and autoexecs are. 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 13 replies
  • 1628 views
  • 1 like
  • 4 in conversation