I want to replace space between variables passed in a macro variable with double quotes with space, i.e. " ". Please find below the code I am using for the same and the result I am getting.
%let str7=ABC DEF UVW XYZ;
%let str8 = %qsysfunc(quote(%qsysfunc(tranwrd(%qsysfunc(compbl(%qsysfunc(strip(%upcase(&str7.))))),%str( ),%nrstr(%")))));
%put ^^^^&str8;
The Log Displays:
18 %put ^^^^&str8;
^^^^"ABC""DEF""UVW""XYZ"
I need this to be "ABC" "DEF" "UVW" "XYZ".
I have also tried the following...
proc fcmp outlib=work.funcs.funcs;
function delimit_words(iString $) $;
result = '"' || tranwrd(cats(compbl(iString))," ", '" ,"') || '"';
return (result );
endsub;
run;
%let x = A B C ;
%let y = %sysfunc(delimit_words(&x));
%put &y;
but it gives me following error...
ERROR: The DELIMIT_WORDS function referenced in the %SYSFUNC or %QSYSFUNC macro function is not found.
I have used the following approach given by Jeff.
data test;
id =1; _var="ABC"; output;
id =1; _var="DEF"; output;
id =1; _var="UVW"; output;
id =2; _var="UVW"; output;
id =3; _var="ABC"; output;
id =3; _var="UVW"; output;
id =3; _var="XYZ"; output;
id =4; _var="ABC"; output;
id =4; _var="XYZ"; output;
run;
%macro __test1(_byvar=, _qnam=, _id=);
proc sort data= test out=_test;
by &_byvar.;
%if %superq(_qnam) ne %then
%do;
%let __tmpmv_qnam = %qsysfunc(prxchange(%bquote(s/\b/"/),-1,%bquote(&_qnam)));
*";
%put ^^^^^&__tmpmv_qnam.;
where upcase(&_id) in (&__tmpmv_qnam);
%end;
run;
%mend;
%__test1 (_byvar=id ,_qnam = ABC UVW, _id=_var);
The log diaplayed following error:
SYMBOLGEN: Macro variable _QNAM resolves to ABC UVW
ERROR: Literal contains unmatched quote.
Please help on this one.
Without debugging either of your approaches above, I can offer a third:
%let str8 = %sysfunc(prxchange(%bquote(s/\b/"/),-1,%quote(&str7.)));
*";
The prxchange()
function tells SAS to replace every word boundary (\b
) with a "
. As far as when to use bquote()
, vs quote()
, it's all documented online, but honestly I usually resort to trial and error.
The *";
does nothing, but it makes things look nicer if your editor's syntax highlighting gets confused by the single quote (like mine does).