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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.