Countw T vs M modifier or please recommend the appropriate modifier other than B to make it count from left to right for the below scenario? My eyes are failing to spot and catch the modifier
The expected count is 6
data _null_;
count=countw('a, b, c,,d, e, ', ',' );
put count=;/*this is not counting embedded one but is counting the trailing*/
count=countw('a, b, c,,d, e, ', ',' ,'t');
put count=;
count=countw('a, b, c,,d, e, ', ',' ,'m');
put count=;
count=countw('a, b, c,,d, e, ', ',' ,'b');
put count=;
run;
Use the M modifier to allow it to count the adjacent delimiters as indicating null values.
There are 7 words in your string, the last one is all blanks. If you want a result of 6 then subtract 1 from the normal result.
Thank you @Tom So no modifier available to exclude the trailing and yet count the consecutive ?
Why would you want to exclude data?
I agree to count with M and then minus is neat. But I just wanted to see/learn if there is a straight forward way(without having to manipulate ) to count from left to right with options only till the last delim. Hypothetically assuming the requirement is to count the number of words until the last delim.
Slick idea:)
@Astounding wrote:
If you switch from COUNTW to COUNT, it's easy to count the number of commas.
The problem with the COUNT() (or COUNTC()) function is that they do not offer the Q modifier to handle strings where the words contain the delimiter character.
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.