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

I'm hoping for some help in understanding the results I'm getting in the following code.

%let test =;

%put test has a word count of %sysfunc(countw(test));

In the case of an empty macro variable, countw returns a value of 1. I'd like the result to be 0 in this case, which seems like it should be the result, but I'm clearly missing something in the documentation. Is there a way to have this return 0 by using some combination of optional arguments? If so, what would that be?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It is problem with the way that %SYSFUNC() parses and passes the arguments to the function.

You could either test first or just append a space (or whatever you delimiter is) to the end of the argument.

%put %sysfunc(countw(&test%str( )));

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

I think you might want to use

%put test has a word count of %sysfunc(countw(&test));

And furthermore, you might want to perform a test of &test to make sure it isn't an empty string, which it is in your example. If its an empty string, you can return a value of 0 and branch around the actual use of countw

--
Paige Miller
emikea
Calcite | Level 5

My apologies. Leaving out the ampersand was a typo.

I'm actually trying to have countw return 0 when the string is empty. It seems like that should be the result.

Tom
Super User Tom
Super User

It is problem with the way that %SYSFUNC() parses and passes the arguments to the function.

You could either test first or just append a space (or whatever you delimiter is) to the end of the argument.

%put %sysfunc(countw(&test%str( )));

emikea
Calcite | Level 5

Thank you Tom.

Ksharp
Super User

That is weird. I got ERROR info running your code .

%let test =;

%put test has a word count of %sysfunc(countw(%bquote(&test)));

test has a word count of 0

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 15413 views
  • 6 likes
  • 4 in conversation