Brad
Brad

Reputation: 853

SAS Command Line - Partial Program Execution

I am using runSASJob to execute SAS programs in a UNIX environment.

I was wondering if there was a way to execute a portion of a SAS program from the command line.

For example, say I have a SAS program with 100 lines and there is a data step in that program that takes up lines 5-10.

Can I run lines 5-10 only from the command line?

Thanks

Upvotes: 0

Views: 178

Answers (2)

Leo
Leo

Reputation: 3116

The following macro should address this problem and some similar ones:

%macro runMacroFromEnv
/des='Set the environment value RUNMACRO to run a macro'; 
    %if %sysfunc( envlen( RUNMACRO ) ) > 0 %then %sysget( RUNMACRO );
%mend;

For example: if you have the following file:

%macro foo;
   %put I just ran foo;
%mend;

%macro runMacroFromEnv
/des='Set the environment value RUNMACRO to run a macro'; 
    %if %sysfunc( envlen( RUNMACRO ) ) > 0 %then %sysget( RUNMACRO );
%mend;

%runMacroFromEnv;

and invoke SAS with:

sas runMacro.sas -set RUNMACRO %foo

Then somewhere in your log, you will see:

I just ran foo

A couple of important details:

  • Using -set works (at least) in Unix and Windows
  • There are other mechanisms for setting environment variables, depending on your shell
  • Don't forget the % in -set RUNMACRO %foo
  • See The set system option for more details

Upvotes: 0

user667489
user667489

Reputation: 9569

There are various options available to you:

  1. Split out the 5 lines of interest into a separate file, then replace them with a %include statement in the original 100-line program
  2. As above, but using an autocall macro or stored compiled macro
  3. Write a data step that takes the 100-line .sas file as input, outputs only the 5 lines you want, and %include it - useful if you don't have write access to the file you want to run the 5 lines from, and you don't want to maintain a separate copy.
  4. If you have already executed the whole 100-line program in the same session, option spool is set, and you know which line numbers the 5 desired lines occupied in the log, you can replay those lines via a %include statement.

For option 3, a sample data step might look like this:

%let SHORTFILE = %sysfunc(pathname(work))/shortfile.sas;

data _null_;
  infile "/path/to/100/line/file.sas" obs = 10 firstobs=5 lrecl = 32767;
  file "&SHORTFILE" lrecl= 32767;
  input;
  put _infile_;
run;

%include "&SHORTFILE";

If you're feeling brave and you really don't want to create an extra temp file, you can use call execute to do this instead:

data _null_;
  infile "/path/to/100/line/file.sas" obs = 10 firstobs=5 lrecl = 32767;
  input;
  call execute(_infile_);
run;

Upvotes: 1

Related Questions