SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Parameterized Job

Accepted Solution Solved
Reply
Regular Contributor
Posts: 168
Accepted Solution

Parameterized Job

May I request someone to shed some lights to create a parameterized job which should run start of each month starting from July 01,2015? I was excepting only the tips, with that I can manage to complete the task.

Thanks in advance for any help.


Accepted Solutions
Solution
‎07-02-2015 03:10 AM
Super User
Posts: 6,939

Re: Parameterized Job

2) You must use %sysget to transfer the value of the environment variable.

Environment variables in UNIX (and Windows) are set from the commandline (shell). Therefore you don't put them into a SAS configuration file.

Here's my(our) method:

- a specific set of environment variables has been defined by us (SAS admins) and the data center people; one of those is the jobname (SAS program name without the .sas)

- SAS programs to be scheduled are stored in a defined path

- for scheduled jobs, a UNIX script called sasbatch has been created that calls SAS in batch mode, with a specialized config & autoexec

- that sasbatch makes sure that all specified (see above) environment variables are present; if not, they are created with a dummy value

- each job run is individualized with datetime and process number

- the logfile is specified with program name and individulization, so that every run leaves a distinct log

- the sasbatch runs a special SAS program before and after each job in a separate SAS call. This program records every job in a dataset (before with "running", after with return code) and creates a daily HTML page that displays all job runs for that day

- to make programming of scheduled jobs easier, an include called pre-run.sas has been written that does all the %sysget and writes all values into the log, for control purposes

In order to successfully create and run scheduled jobs, you need to acquire knowledge about the underlying operating system (for shell scripting) and the scheduling tools present

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,939

Re: Parameterized Job

Use macro variables in the SAS code for parametrization.

To set those macro variables, you could use the -sysparm commandline option when running a batch job and parse the &SYSPARM automatic macro variable.

Or you can set your parameters in environment variables and use %sysget to retrieve the values.

Or you can set your macro variables from user-defined prompts if you run the jobs manually from EG.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 168

Re: Parameterized Job

how to set parameters in enviorment variable?

Super User
Posts: 6,939

Re: Parameterized Job

This depends on the operating system.

In UNIX, it is (in a shell script)

export VARNAME=value

(it is common to write environment variables in capitals to easily distinguish them from commands, files etc)

"export" is a keyword that puts the environment variable in the "public" block that is visible to child processes (one of those will be your SAS job).

So, imagine you have a file called /home/user/example.sas:

%let indate=%sysget(INDATE);

proc print data=test;

where date="&indate."d;

run;

now you do (in a shell script):

export INDATE=01jan2015

sas /home/user/example.sas -print /home/user/example.lst

This will create a listing of records with the wanted date in the .lst file

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 168

Re: Parameterized Job

Thanks for the explanation. My understanding is,

1. Create environment variable like, export INDATE=01jan2015 in UNIX box.

2.If I run the SAS code below in UNIX, macro variable will automatically resolved to 01jan2015.

Please advise if I overlook any. Do we need to create a environment variable in SAS configuration file?

Super User
Posts: 5,083

Re: Parameterized Job

Environmental variables are not automatically transferred into macro variables.  You need to explicitly use %SYSGET to make that happen.

Also note, many monthly applications try to retrieve data from a previous time period, such as the previous month.  You can do that in SAS using the INTNX function.  Even if you don't have a macro variable with the beginning date in it, SAS provides the date on which the program runs as a macro variable.  For example, consider:

where (intnx('month', "&sysdate9"d, -1) <= date_variable < intnx('month', "&sysdate9"d, 0));

If your program runs on any date in July 2015, it automatically selects values for DATE_VARIABLE that have a June 2015 date.

Good luck.

Solution
‎07-02-2015 03:10 AM
Super User
Posts: 6,939

Re: Parameterized Job

2) You must use %sysget to transfer the value of the environment variable.

Environment variables in UNIX (and Windows) are set from the commandline (shell). Therefore you don't put them into a SAS configuration file.

Here's my(our) method:

- a specific set of environment variables has been defined by us (SAS admins) and the data center people; one of those is the jobname (SAS program name without the .sas)

- SAS programs to be scheduled are stored in a defined path

- for scheduled jobs, a UNIX script called sasbatch has been created that calls SAS in batch mode, with a specialized config & autoexec

- that sasbatch makes sure that all specified (see above) environment variables are present; if not, they are created with a dummy value

- each job run is individualized with datetime and process number

- the logfile is specified with program name and individulization, so that every run leaves a distinct log

- the sasbatch runs a special SAS program before and after each job in a separate SAS call. This program records every job in a dataset (before with "running", after with return code) and creates a daily HTML page that displays all job runs for that day

- to make programming of scheduled jobs easier, an include called pre-run.sas has been written that does all the %sysget and writes all values into the log, for control purposes

In order to successfully create and run scheduled jobs, you need to acquire knowledge about the underlying operating system (for shell scripting) and the scheduling tools present

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

Re: Parameterized Job

Do you really need it to be parameterized?

What parameter(s) do you need, and how will you use them?

A scheduling tool handles the execution time/date.

For a monthly batch, a lookup table could be sufficient.

Data never sleeps
Regular Contributor
Posts: 168

Re: Parameterized Job

Yes, I would like to parameterized. I guess we can do it via control table but I don't a have full picture.

Super User
Posts: 5,257

Re: Parameterized Job

So, get back when you have the full picture Smiley Happy

Data never sleeps
Super User
Posts: 3,108

Re: Parameterized Job

What scheduler do you plan to use? If you have the SAS LSF scheduler you can set up your scheduling in SAS Management Console, including parameters which LSF then creates environment variables from. You can then use %SYSGET in your SAS code to read these into macro variables, as has been already mentioned.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 669 views
  • 3 likes
  • 5 in conversation