BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mcook
Quartz | Level 8

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));

When working with a macro variable name, you want %NRBQUOTE and not %nrstr 

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
%LET VNum=%SYSFUNC(CountW(%NRBQUOTE(&VarNames.),|));

When working with a macro variable name, you want %NRBQUOTE and not %nrstr 

--
Paige Miller
mcook
Quartz | Level 8

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.  

 

ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 705 views
  • 1 like
  • 4 in conversation