I have the following code.
data Testing1;
input A $ B $ C $ D $;
datalines;
a*$"B, $%\- D_D( A&5`
;
run;
proc transpose data=testing1 out=testing1;
var A B C D;
run;
proc sql noprint;
select Col1 into :VarNames
separated by '|'
from Testing1;
quit;
%PUT VarNames=%NRBQUOTE(&VarNames.);
%LET VNum=%SYSFUNC(CountW(%NRSTR(&VarNames.),|));
%PUT VNum=&VNum.;
I am unable to get VNum to = 4.
VarNames=a*$"B,|$%\-|D_D(|A&5`
with | as a delimiter, there should be 4 words.
but VNum keeps coming out as 1
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));
When working with a macro variable name, you want %NRBQUOTE and not %nrstr
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));
When working with a macro variable name, you want %NRBQUOTE and not %nrstr
Bah. I'd already tried NRBQUOTE and it didn't work. It gave me a recursion error. so i changed it to NRSTR.
But i just tried it again, copying what you wrote, and it worked perfectly. Thank you.
I must have had a syntax error I didn't notice.
Part of the issue is the unbalanced quote.
From the documentation of %STR and %NRSTR
They also mask the following characters when they occur in pairs and when they are not matched and are marked by a preceding%
' " ( )
So since your example single quote does not appear in a pair you are getting the | characters after the " as one word.
Personally by the time I need to use NRSTR or similar I start looking very closely at the whole process. Macro quoting is pretty dangerous when things go unexpected.
Personally I find it much easier to just use %SUPERQ() to add quoting to a macro variable.
50 51 %let varnames=%superq(varnames); 52 %LET VNum=%SYSFUNC(CountW(&VarNames,|)); 53 %PUT VNum=&VNum.; VNum=4
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.