BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AThomalla
Obsidian | Level 7

Hi,

 

I would like to write code which executes some module, but in the same time automatically prints all variables/matrices or stores them in an excel sheet. For doing that, first I would need some functionality like "SHOW" which gives the local variables within the module, so that I can make a loop accross the variables and print and export them to excel. So in my opinion the key difficulty here is to get and store a list of all variables within a module. Is that possible with some IML functionalities?

 

Example:

 

proc iml;

start example(input_var);

var1 = input_var #2;
var2 = input_var + 2;

measure = var1 || var2;

return(measure)
finish;

M = {1,2};
test = example(M);

quit;

 

In that example I would like to define a module or a macro with which I can execute the module "example" with additional requirement that while executing all local variables (so input, var1, var2, measure) are printed automatically.

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I guess I'd use ODS OUTPUT to store the results of the SHOW command in a data set. Then read in the results and parse the file to obtain the variable name. Maybe this will give you are start (only partially tested):

 

%macro PrintLocalVars();
   ods exclude iml.show; 
   ods output iml.show=showvars;   /* store SHOW table */
      show names;
   ods output close;
   ods exclude none;
   use showvars; read all var {batch}; close;
   call delete(showvars);
   _ndx = loc(batch^=' ');          /* delete any blank lines */
   if ncol(_ndx)>0 then batch=batch[_ndx];
   _b = batch[3:(nrow(batch)-1)];   /* get rid of  headers and end message */
   _LocalVars = scan(_b, 1, ' ');
   print _LocalVars;
   free batch _ndx _b _LocalVars;
%mend;

proc iml;

x = 1; y = 2; z = 3;
%PrintLocalVars();

free x y z varnames;
a = 1; b = 2; c = 3;
%PrintLocalVars();

/* get local vars inside module */
start example(input_var);
   var1 = input_var #2;
   var2 = input_var + 2;
   measure = var1 || var2;
   call execute("%PrintLocalVars()"); 
   return(measure);
finish;

M = {1,2};
test = example(M);

quit;

View solution in original post

2 REPLIES 2
AThomalla
Obsidian | Level 7

I think, the answer is the 

 

STORAGE Function.

 

So first I would have to store all Matrices in a specific library (store _all_) and then apply the storage() function the get the list.

 

Rick_SAS
SAS Super FREQ

I guess I'd use ODS OUTPUT to store the results of the SHOW command in a data set. Then read in the results and parse the file to obtain the variable name. Maybe this will give you are start (only partially tested):

 

%macro PrintLocalVars();
   ods exclude iml.show; 
   ods output iml.show=showvars;   /* store SHOW table */
      show names;
   ods output close;
   ods exclude none;
   use showvars; read all var {batch}; close;
   call delete(showvars);
   _ndx = loc(batch^=' ');          /* delete any blank lines */
   if ncol(_ndx)>0 then batch=batch[_ndx];
   _b = batch[3:(nrow(batch)-1)];   /* get rid of  headers and end message */
   _LocalVars = scan(_b, 1, ' ');
   print _LocalVars;
   free batch _ndx _b _LocalVars;
%mend;

proc iml;

x = 1; y = 2; z = 3;
%PrintLocalVars();

free x y z varnames;
a = 1; b = 2; c = 3;
%PrintLocalVars();

/* get local vars inside module */
start example(input_var);
   var1 = input_var #2;
   var2 = input_var + 2;
   measure = var1 || var2;
   call execute("%PrintLocalVars()"); 
   return(measure);
finish;

M = {1,2};
test = example(M);

quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 687 views
  • 1 like
  • 2 in conversation