In SAS, outside of a data step, what is the best way to replace a character in a macro variable with a blank?

StackOverflow https://stackoverflow.com/questions/521207

  •  22-08-2019
  •  | 
  •  

Question

In SAS, outside of a data step, what is the best way to replace a character in a macro variable with a blank?

It seems that TRANSLATE would be a good function to use. However when using %SYSFUNC with this function, the parameters are not surrounded with quotes. How do you indicate a blank should be used as replacement?

Was it helpful?

Solution

There are no quotes in macro language. The only quotes characters that are in use are the & , % etc. to indicate that the text should be interpreted as a macro "operator". A blank is represented by %str( ) as indicated above in Carolina's post.

OTHER TIPS

The %str( ) (with a blank between the parens) can be used to indicate a blank for this parameter. Also be careful with TRANSLATE...the 2nd param is the replacement char...however in TRANWRD it is reversed.

    %macro test ;
     %let original= translate_this_var ;
     %let replaceWithThis= %str( ) ;
     %let findThis= _ ;
     %let translated= %sysfunc(translate(&original, &replaceWithThis, &findThis)) ;
     %put Original: &original ***** TRANSLATEd: &translated ;
    %mend ;
    %test;

    %macro test2 ;
     %let original= translate_this_var ;
     %let replaceWithThis= %str( ) ;
     %let findThis= _ ;
     %let tranwrded= %sysfunc(tranwrd(&original, &findThis, &replaceWithThis)) ;
     %put Original: &original ***** TRANWRDed: &tranwrded ;
    %mend ;
    %test2

you can use perl reg ex instead, like:

%put ***%sysfunc(prxchange(s/x/ /, -1, abxcdxxf))***;
/* on log
***ab cd  f***
*/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top