Desktop productivity for business analysts and programmers

Editing macro list

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Editing macro list

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


Accepted Solutions
Solution
‎11-08-2017 10:00 AM
Super User
Posts: 6,542

Re: Editing macro list

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


All Replies
Super User
Super User
Posts: 9,211

Re: Editing macro list

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

...

Solution
‎11-08-2017 10:00 AM
Super User
Posts: 6,542

Re: Editing macro list

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.

New Contributor
Posts: 2

Re: Editing macro list

Posted in reply to Astounding

Thanks! That did the trick ...

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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