DATA Step, Macro, Functions and more

replace the characters

Occasional Contributor
Posts: 11

replace the characters

I need to replace the characters with others, but when write this, code don't work



%macro example();

%let decodedata=f"eif";
%put decodedata=&decodedata; 
%let decodedata1=%sysfunc(tranwrd(&decodedata,",&quot));
%put decodedata1=&decodedata1;
%mend example;



 and when write

%let decodedata1=%sysfunc(tranwrd(&decodedata,' " ',&quot));


get the original string in response. What's wrong?

Valued Guide
Posts: 580

Re: replace the characters

Posting log with options symbolgen mprint and mlogic activated will show what happened.


From the code you posted:

  • the variable &quot is undefined
  • in macro-code an empty string is %str( )
Super User
Posts: 10,279

Re: replace the characters

Look at the log, the macro fails because of the unbalanced double quote, and the single %let fails because of the undefined symbol quot. Note that " is something that works in HTML, but not as some kind of tag in SAS.


What are you trying to achieve? With complicated replacements involving quotes, I'd do it in a data step with symget() and call symputx(). Using data step functions in data steps is always easier and more transparent.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: replace the characters

Why are you doing data manipulation in SAS Macro when there is a fully fledged language for such a purpose called base SAS?  You are setting yourself up for bad unstable coding.

Super User
Posts: 6,785

Re: replace the characters

This is untested but should work.  Change this:




Use this instead:




If it's possible, I do agree with the others that claim you would be well-served by switching to SAS language instead of macro language.

Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation