Your SAS programs, embedded in web apps and elsewhere

How to get a list of macro variables from an existing macro variable

Reply
N/A
Posts: 0

How to get a list of macro variables from an existing macro variable

I have a macro variable, which contains a list of character strings. I am wondering if there is any way to get a series of new macro variables from that. To illustrate with an example:

I have: %let old_list = a b c d ; * a, b, c, d are the variable names in my model;
I want following 3 new macro variables so that I can refit the model with new list:
%let new_list1 = b c d;
%let new_list2 = a c d;
%let new_list3 = a b c;


I really appreciate any help on getting this. Thanks.
SAS Super FREQ
Posts: 8,864

Re: How to get a list of macro variables from an existing macro variable

Posted in reply to deleted_user
Hi:
This is more of a macro usage question than a stored process question. Your best bet for a solution might be to contact Tech Support for help with a more automated approach or possibly using a multiple value parameter for your stored process.

To "break down" the value in the old_list macro variable, you could use the SCAN function invoked with %SYSFUNC, as shown below:
[pre]
%let old_list = a b c d ;

%let v1 = %sysfunc(scan(&old_list,1));
%let v2 = %sysfunc(scan(&old_list,2));
%let v3 = %sysfunc(scan(&old_list,3));
%let v4 = %sysfunc(scan(&old_list,4));
%let v5 = %sysfunc(scan(&old_list,5));

%put --------> pieces of old_list &old_list;
%put --------> v1 = &v1;
%put --------> v2 = &v2;
%put --------> v3 = &v3;
%put --------> v4 = &v4;
%put --------> v5 = &v5;
%put ----> Note how v5 is "null" when you have reached;
%put ----> the end of the string contained in old_list;
[/pre]

To see how the SCAN function works, submit the above code from an EG code node or in a SAS Display Manager session. The results from the %PUT statements will appear in the SAS log. When you run this code snippet, you will see that I have created 5 separate macro variables -- &v1 thru &v4 have the values that were parsed out of &old_list and v5 is "null" because there was no 5th value in the string.

To really code a robust solution for your stored process so you can build multiple model statements, you will probably need a macro program and a macro %do loop (at the very least). Tech Support can help you with those. To contact Tech Support, refer to:
http://support.sas.com/techsup/contact/index.html

Good luck,
cynthia
N/A
Posts: 0

Re: How to get a list of macro variables from an existing macro variable

Posted in reply to Cynthia_sas
Thanks a lot Cynthia!
Ask a Question
Discussion stats
  • 2 replies
  • 199 views
  • 0 likes
  • 2 in conversation