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

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

...

Astounding
PROC Star

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.

Maty
Calcite | Level 5

Thanks! That did the trick ...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 995 views
  • 2 likes
  • 3 in conversation