- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));
When working with a macro variable name, you want %NRBQUOTE and not %nrstr
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));
When working with a macro variable name, you want %NRBQUOTE and not %nrstr
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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