As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Parse sysparm

From sasCommunity
Jump to: navigation, search

Q: How can I pass multiple named values to a program?

A: use command-line or sas statement option sysparm with comma-separated values.

  • batch: command-line option: -sysparm 'value'
  • program: option statement: options sysparm = 'value';

program parse-sysparm

MWSUG-2016, Tools of the Trade, paper TT04, A Sysparm Companion

%put trace: p-sysprm=parse-sysparm beginning;
%put echo parameter: &=sysparm;
 /*    name: <UNC>\SAS-site\includes\p-sysprm.sas
                                     parse-sysparm
description: convert sysparm to set of macro variables
purpose    : subroutine for pararameterized includes
note       : multiple values are named parameters
             and delimited by comma: a=1,b=2
usage:
autoexec: filename site_inc '...\SAS-site\includes\';
options sysparm = 'a=1,b=2,d=4';
%include site_inc(p-sysprm);
**** ******/
DATA _null_;
     attrib stmnt length = $%length(*let_&sysparm!);
 
if length(sysparm()) then do;
   ** upper bound is n(equal signs);
   do i = 1 to countc(sysparm(),'=');
      ** assemble macro variable assignment statement;
      stmnt= catx(' ','%let ',scan(sysparm(),i,','),';');
      putlog 'echo: ' stmnt=;
      call execute(cat('%nrstr(',stmnt,')'));
      end;
   end;
stop;
run;
%put trace: p-sysprm=parse-sysparm ending;

Concept Demonstration

write matrix to csv

 /*    name: C:\temp\sas\write-matrix-to-csv.sas
description: write a matrix of real numbers to a *.csv file
    purpose: demo of parse sysparm
     author: Ronald J. Fehd 2016
http://www.sascommunity.org/wiki/Parse_sysparm
command line call: place new values in comma-separated list
sas write-matrix-to-csv -sysparm 'columns=10,rows=10,outcsv=matrix-10-10'
***************/
* set defaults;
%let function = rancau;*in rancau ranexp rannor ranuni;
%let format   = best.;*: w. or w.d;
%let f_of_x   = ;*log(x);
%let seed     = 0;*0: use systime of day;
%let columns  = 10;
%let rows     = 10;
%let outcsv   = z-matrix-w-random-numbers;
 
* replace default values with those in sysparm;
DATA _null_;
     attrib item length=$%length(&sysparm);
     retain sysparm "&sysparm";
     putlog 'echo ' sysparm=;
 
*for each parameter in non-blank(sysparm), reassign value;
if length(sysparm) then do;
   do i = 1 to countc(sysparm,',')+1;
      item = scan(sysparm,i,',');
      *note: item is <parameter>=<value>;
      *submit macro variable assignment: *let mvar=value;
      call execute(catx(' ','%let',trim(item),';'));
      end;
   end;
stop;
run;
 
* echo list of parameters;
%put _user_;
 
* write matrix to file;
DATA _null_;
     file "&outcsv..csv";
     retain seed &seed;
 
* write header in row=1;
do columns = 1 to &columns-1;
   * format leading zeros z<n>. : z3. = 001:100;
   put 'c' columns z%length(&columns). ',' @;
   end;
put    'c' columns z%length(&columns). ;
 
* write matrix;
do rows = 1 to &rows;
   do columns = 1 to &columns-1;
      call &function(seed,x);
      *if not blank(f-of-x);
      %sysfunc(ifc(%length(&f_of_x)
         ,%nrstr(x = &f_of_x;)
         ,))
      put x &format ',' @;
      * @ means hold line:: no cr/lf;
      end;
   put    x &format;*end line: write cr/lf;
   end;
stop;
run;

write matrix to data set

 /*    name:C:\temp\sas\write-matrix-to-data.sas
description: write a data set of real numbers
    purpose: demo of parse sysparm
     author: Ronald J. Fehd 2016
http://www.sascommunity.org/wiki/Parse_sysparm
command line call: place new values in comma-separated list
sas write-matrix-to-data -sysparm 'columns=10,rows=10,outdata=x'
***************/
* set defaults;
%let function = rancau;*in rancau ranexp rannor ranuni;
%let format   = best.;*: w. or w.d;
%let f_of_x   = ;*log(abs(c(_j)));
%let seed     = 0;*0: use systime of day;
%let columns  = 10;
%let rows     = 10;
%let prefix   = c;*of variable names;
%let outdata  = _null_;
 
* replace default values with those in sysparm;
DATA _null_;
     attrib item length=$%length(&sysparm);
     retain sysparm "&sysparm";
     putlog 'echo ' sysparm=;
 
*for each parm=value in non-blank(sysparm), reassign value;
if length(sysparm) then do;
   *upper bound is       n(commas)+1;
   do i = 1 to countc(sysparm,',')+1;
      item = scan(sysparm,i,',');
      *note: item is <parameter>=<value>;
      *submit macro variable assignment: *let mvar=value;
      call execute(catx(' ','%let',trim(item),';'));
      end;
   end;
stop;
run;
 
* echo list of parameters;
%put _user_;
 
* write matrix to data set;
DATA &outdata;
     array &prefix(&columns);
     drop _i _j _seed;
     retain _seed &seed;
 
* write matrix;
do _i = 1 to &rows;
   do _j = 1 to &columns;
      call &function(_seed,&prefix(_j));
      *if not blank(f-of-x);
      %sysfunc(ifc(%length(&f_of_x)
         ,%nrstr(&prefix(_j) = &f_of_x;)
         ,))
      end;
   output;
   end;
stop;
run;
/**********************************
PROC sql; describe table &syslast;
          quit;
PROC print data = &syslast(obs=20);
 **********************************/

References

see also How to pass parameters to a SAS Program

-- created by User:Rjf2 10:22, 14 June 2007 (EDT)

--Ronald_J._Fehd macro.maven == the radical programmer