(Author: Roland Rashleigh-Berry
Last updated: 21 Feb 2013)
You should limit shell scripts that call SAS software to those needing to get information from SAS software objects or those required to call SAS software procedures. This is because invoking SAS software in a script creates an overhead. For reading text, the language "gawk" is faster and better. You can read about how to use gawk here. If your script combines information from a SAS software object with other tasks not related to SAS software objects, then you should learn how to split up your task into multiple scripts so that one script just acts on the SAS software object and then outputs this information to other scripts for further processing. Knowing how to split up tasks like this and the best way to do it comes with experience.
The reason you will want to write shell script that call SAS software is to avoid using an interactive SAS session. Spectre favours batch working so if programmers need information about SAS software objects or need to call a SAS software procedures to show them some simple output then it would help them if they could do this from the Unix command line. There are already some shell scripts that call SAS software in Spectre such as contentsl and summary for this but the list is not exhaustive. There will be times when you need to write your own shell script that call SAS software.
Writing shell scripts that call SAS software is very easy and to make it even easier there a utility to help you do this that you will be introduced to.
$ sasunixskeleton
Enter script name: dummy This utility will generate a script and store it in the current directory. Enter purpose of script: Just a demo
|
...and here is the script it created.
#!/bin/bash
# Script : dummy # Version : 1.0 # Author : Roland Rashleigh-Berry # Date : 23-Apr-2006 # Purpose : Just a demo # SubScripts : none # Notes : EDIT # Usage : dummy EDIT # #=============================================================================== # PARAMETERS: #-pos- -------------------------------description------------------------------- # 1 First parameter (if any) description EDIT EDIT EDIT # 2 Second parameter (if any) description EDIT EDIT EDIT # 3 etc. or..... # N/A Do not supply any parameters EDIT EDIT EDIT #=============================================================================== # AMENDMENT HISTORY: # init --date-- mod-id ----------------------description------------------------ # #=============================================================================== # Put out a usage message if not enough parameters supplied
# Feed SAS code into standard input
EDIT EDIT EDIT EDIT Put your code here. Use $1 to resolve the first entered parameter.
$'s get treated like the &'s in front of macro variables by
scripts.
Your final output should be written to normal print output or you
data _null_;
|
The script it created is skeleton script code, hence the name of the utility "sasunixskeleton". You have to edit the script where you see "EDIT" and add your code to the "here document" which is ended by the string "-----FINISH-----" starting in the first column. Note the options used when invoking SAS software. "-stdio" is needed but you may need to change the default options "-noautoexec -sasuser work". With the "-stdio" option in effect, the log gets written to stderr (standard error) which has file handle "2" (standard output has file handle "1") so you see I have routed the log to the Unix dustbin using "2>/dev/null". If you are debugging the script then you will need to route the log to a file in your home directory so you can look at the log and see what the problem is. But don't forget to route the log back to /dev/null again when you have finished debugging.
The important thing you have to remember is what happens to the "$" (dollar) sign in a script. Just like you reference a macro variable with a "&" in front of it, scripts use the "$" sign to reference shell variables. So if you have a format like this "$200." then it will cause an error because it will look for a shell variable named "200". So to get round this in the here document you should put an escape character "\" in front of the "$" so you would write the same format as "\$200.". You need to remember this. However, this does not apply to the script parameter values "$1", "$2" etc..
If your script creates normal print output then it will automatically be written to stdout (standard output) which is what you want, as this will go to the user's terminal. But if you "put" the values then you must use "file stdout" so that the "put" values are written to standard output.
Here are some examples where "file stdout" is used.
contentsl
showlfmt
Use the "Back" button of your browser to return to the previous page
SAS and all other SAS Institute Inc. product or service names are registered
trademarks or trademarks of SAS Institute Inc. in the USA and other countries.
® indicates USA registration.