Hi,
Does anyone know if "countw" can skip the space between single quoted words?
Here is my code:
%macro colnames1(colnames1);
%put "number of names in columns is " %sysfunc(countw(&colnames1));
%mend colnames1;
%colnames1(Contracts age 'young boys'n 'young girls'n);
run;
The result is:
%macro colnames1(colnames1);
%put "number of names in columns is " %sysfunc(countw(&colnames1));
%mend colnames1;
%colnames1(Contracts age 'young boys'n 'young girls'n);
"number of names in columns is " 6
run;
There should only be 4 words ('contracts', 'age', 'young boys' and 'young girls'). The program is counting the spaces in between the single quotes.
I further tried to following but still didn't get the results I wanted (still gave me 6 words).
%put "number of names in columns is " %sysfunc(countw(&colnames2%str("")));
THANKS!
Try
%macro colnames1(colnames1); %put "number of names in columns is " %sysfunc(countw(&colnames1," " ,Q )); %mend colnames1;
The Q says to ignore delimiters within quotes,
One reason you shouldn't use named literals but oh well.
An easy fix would be to add a delimiter to the words and count that instead, ie
%colnames1(Contracts | age | 'young boys'n | 'young girls'n);
Use COUNTC to count the number of | and then add one to determine the number of 'variables'.
@LuLuVolcano73 wrote:
Hi,
Does anyone know if "countw" can skip the space between single quoted words?
Here is my code:
%macro colnames1(colnames1);
%put "number of names in columns is " %sysfunc(countw(&colnames1));
%mend colnames1;
%colnames1(Contracts age 'young boys'n 'young girls'n);
run;
The result is:
%macro colnames1(colnames1);
%put "number of names in columns is " %sysfunc(countw(&colnames1));
%mend colnames1;
%colnames1(Contracts age 'young boys'n 'young girls'n);
"number of names in columns is " 6
run;
There should only be 4 words ('contracts', 'age', 'young boys' and 'young girls'). The program is counting the spaces in between the single quotes.
I further tried to following but still didn't get the results I wanted (still gave me 6 words).
%put "number of names in columns is " %sysfunc(countw(&colnames2%str("")));
THANKS!
That run; statement doesn't belong ...
however, COUNTW is doing the proper thing here, it just sees colnames as a text string, it doesn't understand that you think 'young boys'n is one word. it is simply looking for the space to delimit the words, and so 'young boys'n is two words, the first being 'young and the second being boys'n.
If you really want this to be 4 words, you would need some other approach, I don't think COUNTW can get there. Perhaps using regular expressions can turn 'young boys'n into young_boys which then is counted properly by COUNTW.
Or, you can put the anti-delimiters in manually, replacing 'young boys'n with young_boys and 'young girls'n with young_girls. And also in this case COUNTW will work as expected.
Thanks.
Try
%macro colnames1(colnames1); %put "number of names in columns is " %sysfunc(countw(&colnames1," " ,Q )); %mend colnames1;
The Q says to ignore delimiters within quotes,
Thank you! You are AWESOME!
Wow, @ballardw, I did not know that. That is a very easy solution!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.