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.


Difference between revisions of "Parse sysparm"

From sasCommunity
Jump to: navigation, search
m (added cat:Sysparm (sp) and ref to blogs)
(added Concept demo programs page-reads:2016-03-25: 15,383)
Line 41: Line 41:
 
run;    *calls execute here;
 
run;    *calls execute here;
 
run;
 
run;
 +
</source>
 +
 +
== Concept Demonstration ==
 +
=== write matrix to csv ===
 +
<source lang="sas">
 +
/*    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;
 +
</source>
 +
 +
=== write matrix to data set ===
 +
<source lang="sas">
 +
/*    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);
 +
**********************************/
 
</source>
 
</source>
  

Revision as of 08:30, 25 March 2016

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';
 /*    name: parse-sysparm.sas
description: parse the text in option sysparm into macro variables
    purpose: support batch processing command-line parameter passing
;/* testing in program ****** **
options source2;%* for put _global_;
options sysparm = 'a=1;b=2;d=4';
;/***** ********************* */
 
DATA _Null_;
     attrib Stmnt   length = $132
            Testing length =    4;
     retain Testing %eval(0
            or "%sysfunc(getoption(Source2))"
                               eq "SOURCE2"
            or "%sysfunc(getoption(Verbose))"
                               eq "VERBOSE"  );
if "&SysParm." ne "" then do;
   putlog "SysParm: &SysParm.";
   do I = 1 to %*number of equal-signs;
          length(compress("&SysParm",'=','k'));
      Stmnt= catx(' ' %*separated by space;
                 ,'%let '
                 ,scan("&SysParm.",I,';')
                 ,';');
      if   Testing then putlog Stmnt=;
      call execute(cat('%nrstr(',Stmnt,')'));
      end; %*do I;
   if Testing then
      call execute('%nrstr(%put _global_;)');
   end; %*if "&SysParm." ne "";
stop;
run;    *calls execute here;
run;

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