DATA Step, Macro, Functions and more

Count the number of pipes within Macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 95
Accepted Solution

Count the number of pipes within Macro

Hi SAS Users,

 

I wanted some help with getting the count of pipes '|' of a macro variable.

 

 %LET DLM_COUNT = COUNTC(&PLATFORM,') +1;  

 

&PLATFORM has value like 'ABC|DEF|GHI|QWE'

 

So i am expecting to 4 in DLM_COUNT.

 

Thanks,

Ana

 

 


Accepted Solutions
Solution
‎12-06-2016 10:21 PM
Trusted Advisor
Posts: 1,022

Re: Count the number of pipes within Macro

Countc is a regulare sas function, not a macro function.  So it has to be called via  %sysfunc;

 

Also, you seem to want to count WORDs, where | is the separator:

 

%let PLATFORM =ABC|DEF|GHI|QWE;

%LET DLM_COUNT = %sysfunc(COUNTW(&PLATFORM,|));

%put &=dlm_count;

View solution in original post


All Replies
Solution
‎12-06-2016 10:21 PM
Trusted Advisor
Posts: 1,022

Re: Count the number of pipes within Macro

Countc is a regulare sas function, not a macro function.  So it has to be called via  %sysfunc;

 

Also, you seem to want to count WORDs, where | is the separator:

 

%let PLATFORM =ABC|DEF|GHI|QWE;

%LET DLM_COUNT = %sysfunc(COUNTW(&PLATFORM,|));

%put &=dlm_count;

Frequent Contributor
Posts: 95

Re: Count the number of pipes within Macro

Thanks much for the quick reply. It worked very well.
Super User
Posts: 5,516

Re: Count the number of pipes within Macro

[ Edited ]

Modifying your attempt slightly:

 

 

%let dlm_count = %sysfunc(countc(&platform, |));

 

I can't test it right now, but it looks like it should work.

 

In retrospect, COUNTW counts the number of words using pipes as delimiters.  COUNTC counts number of pipes.  Either is possible, just pick the one that does what you need.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 216 views
  • 0 likes
  • 3 in conversation