[This site is not connected with the SAS Institute]
[last updated - 03 September 2003]
Sometimes, doing advanced progranmming, you may want to generate SAS code or macro statements that contain the contents of a character variable or perhaps the label of a variable and you need to put it in quotes. Always bear in mind that a string you are quoting may itself contain quotes. The following short code segment and its log will show you how to code for this for both generated SAS code and macro code.
Firstly, here is the pure code submitted:
data test; str='Larry''s cat''s "meow" sounded more like a ''woof'''; put str=; call symput('str',str); call execute('data _null_;str2='''||trim(tranwrd(str,"'","''"))||''';put str2=;run;'); run; %put >>>> str=%str(%')%sysfunc(tranwrd(&str,%str(%'),%str(%'%')))%str(%');
And here is the log output:
23 data test; 24 str='Larry''s cat''s "meow" sounded more like a ''woof'''; 25 put str=; 26 call symput('str',str); 27 call execute('data _null_;str2='''||trim(tranwrd(str,"'","''"))||''';put str2=;run;'); 28 run; str=Larry's cat's "meow" sounded more like a 'woof' NOTE: The data set WORK.TEST has 1 observations and 1 variables. NOTE: DATA statement used: real time 0.05 seconds NOTE: CALL EXECUTE generated line. 1 + data _null_;str2='Larry''s cat''s "meow" sounded more like a ''woof''';put str2=;run; str2=Larry's cat's "meow" sounded more like a 'woof' NOTE: DATA statement used: real time 0.00 seconds 29 %put >>>> str=%str(%')%sysfunc(tranwrd(&str,%str(%'),%str(%'%')))%str(%'); >>>> str='Larry''s cat''s "meow" sounded more like a ''woof'''
Go back to the home page.
E-mail the macro and web site author.