Hi,
I am wondering how to edit macro list to add suffix to all of its items.
Let's say I have
%let vars=one_ two_ three_;
I would like to make a macro that loops several times (10x) and in each loop it adds different suffix to &vars.
I came accross SAS list string utils from Jiangtang Hu - List Processing with SAS: A Comprehensive Survey
and I used edited version of his %add_string macro:
%macro add_string(words, str, delim=%str( ), location=suffix); %local outstr i word num_words; * Build the outstr by looping through the words list and adding the * requested string onto each word. ; %let outstr = ; %let num_words = %sysfunc(countw(&words)); %do i=1 %to &num_words; %let word = %scan(&words, &i, &delim); %if (&i eq 1) %then %do; %if (%upcase(&location) eq PREFIX) %then %do; %let outstr = &str&word; %end; %else %do; %let outstr = &word&str; %end; %end; %else %do; %if (%upcase(&location) eq PREFIX) %then %do; %let outstr = &outstr&delim&str&word; %end; %else %do; %let outstr = &outstr&delim&word&str; %end; %end; %end; * Output the new list of words. ; &outstr %mend add_string;
The problem is how to handle the output from such a macro? When I call it I get an Error : Statement is no valid:
%let vars=one_ two_ three_; %let vars1r=%add_string(&vars, 1r);
"ERROR 180-322: STATEMENT IS NOT VALID OR IT IS USED OUT OF PROPER ORDER."
the problem is causing
&outstr
in macro definition, but I don't know how to output from macro in other way. (I am working with SAS enterprise guide 9.4)
Any suggestions? Thanks...
Nice problem! Try changing all the comment statements to macro comment statements, such as:
%* Output the new list of words;
As it stands now, the comment statements become generated text, part of the value being assigned to VARS1R.
Use Base SAS - this is the underlying code which is executed, macro only creates this Base SAS code. It is used for all data processing. Also, without knowing anything about your process its hard to say so this is just guessing. Say I want to create a dataset for each word in the given list, with a second suffix:
%let vars=one_ two_ three_; %macro Create_DS (in_name=,suff=); data &in_name.&suff.; set sashelp.class; run; %mend Create_DS; data _null_; do curr_suffix="abc","def","ghi"; do i=1 to countw("&vars."," "); call execute(cats('%Create_DS (in_name=',scan(&vars.,i,' '),',suff=',curr_suffix,');')); end; end; run;
Basically this will create a macro call for each word in the macro variable, and each suffix in the do loop, so you would end up with:
one_abc
one_def
one_ghi
two_abc
two_def
...
Nice problem! Try changing all the comment statements to macro comment statements, such as:
%* Output the new list of words;
As it stands now, the comment statements become generated text, part of the value being assigned to VARS1R.
Thanks! That did the trick ...
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.