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

Here is the code. It doesn't seem to be removing (replacing with blank) the strings that I am providing as the last parameter in the macro call. Anyone have any ideas. I've tried slight variations but with no luck.

 

%macro word_removal(tbl_in, tbl_out, first_field, second_field, str_list);
   data &tbl_out;
    set &tbl_in;
      %do i = 1 %to %sysfunc(countw(&str_list.));
        %let current_str = %scan(&str_list., &i.);
		&second_field = tranwrd(&first_field, "&current_str.", '');
      %end;
   run;    
%mend word_removal;

%word_removal(input_table, output_table, field_one, field_two, ('hello','world','beta','alpha'));

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@JediApprentice wrote:

Here is the code. It doesn't seem to be removing (replacing with blank) the strings that I am providing as the last parameter in the macro call. Anyone have any ideas. I've tried slight variations but with no luck.

 

%macro word_removal(tbl_in, tbl_out, first_field, second_field, str_list);
   data &tbl_out;
    set &tbl_in;
      %do i = 1 %to %sysfunc(countw(&str_list.));
        %let current_str = %scan(&str_list., &i.);
		&second_field = tranwrd(&first_field, "&current_str.", '');
      %end;
   run;    
%mend word_removal;

%word_removal(input_table, output_table, field_one, field_two, ('hello','world','beta','alpha'));

Thanks


You have two issues likely. First is the macro call is introducing problems with the way that you pass the list. Remove the (), the ' and commas.

Second if you want to remove ALL of the words then your logic is incorrect. You keep replacing the string from the original value. If you pass more than one word then you are currently only actually replacing the last one in the list.

%macro word_removal(tbl_in, tbl_out, first_field, second_field, str_list);
   data &tbl_out;
    set &tbl_in;
      %do i = 1 %to %sysfunc(countw(&str_list.));
        %let current_str = %scan(&str_list., &i.);
         %if &i=1 %then %do;
      		&second_field = tranwrd(&first_field, "&current_str.", '');
          %end;
          %else %do;
      		&second_field = tranwrd(&second_field, "&current_str.", '');
          %end;
     %end;
   run;    
%mend word_removal;

%word_removal(Sashelp.cars,work.junk, make, newmake,Acura Audi);



Quotes and commas in a macro parameter are very often either not needed with careful planning and often a cause of problems.

 

View solution in original post

1 REPLY 1
ballardw
Super User

@JediApprentice wrote:

Here is the code. It doesn't seem to be removing (replacing with blank) the strings that I am providing as the last parameter in the macro call. Anyone have any ideas. I've tried slight variations but with no luck.

 

%macro word_removal(tbl_in, tbl_out, first_field, second_field, str_list);
   data &tbl_out;
    set &tbl_in;
      %do i = 1 %to %sysfunc(countw(&str_list.));
        %let current_str = %scan(&str_list., &i.);
		&second_field = tranwrd(&first_field, "&current_str.", '');
      %end;
   run;    
%mend word_removal;

%word_removal(input_table, output_table, field_one, field_two, ('hello','world','beta','alpha'));

Thanks


You have two issues likely. First is the macro call is introducing problems with the way that you pass the list. Remove the (), the ' and commas.

Second if you want to remove ALL of the words then your logic is incorrect. You keep replacing the string from the original value. If you pass more than one word then you are currently only actually replacing the last one in the list.

%macro word_removal(tbl_in, tbl_out, first_field, second_field, str_list);
   data &tbl_out;
    set &tbl_in;
      %do i = 1 %to %sysfunc(countw(&str_list.));
        %let current_str = %scan(&str_list., &i.);
         %if &i=1 %then %do;
      		&second_field = tranwrd(&first_field, "&current_str.", '');
          %end;
          %else %do;
      		&second_field = tranwrd(&second_field, "&current_str.", '');
          %end;
     %end;
   run;    
%mend word_removal;

%word_removal(Sashelp.cars,work.junk, make, newmake,Acura Audi);



Quotes and commas in a macro parameter are very often either not needed with careful planning and often a cause of problems.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 6568 views
  • 0 likes
  • 2 in conversation