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

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;

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
If you switch from COUNTW to COUNT, it's easy to count the number of commas.

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

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.

 

novinosrin
Tourmaline | Level 20

Thank you @Tom   So no modifier available to exclude the trailing and yet count the consecutive  ?

Tom
Super User Tom
Super User

Why would you want to exclude data?

novinosrin
Tourmaline | Level 20

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. 

Astounding
PROC Star
If you switch from COUNTW to COUNT, it's easy to count the number of commas.
Tom
Super User Tom
Super User

@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: 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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 764 views
  • 3 likes
  • 3 in conversation