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

I have a list such as:  

 

%LET FILELIST= First File Name, Filename Number Two, The Third One, File Number IV, Five Is The Last One;

 

Is there a way to count the number of phrases? More specifically is there a way to get CountW to not register a space as a delimiter, and only register a specified delimiter?  

I currently have a code working properly but only with a / as a delimiter.  Where i manually changed all commas to / and count the number of / and add 1. 

such as:

 

%LET FILELIST= First File Name/ Filename Number Two/ The Third One/ File Number IV/ Five Is The Last One;

%MACRO LOOP_F();
     %DO F=1 %TO %SYSFUNC(Count(&FILELIST.,/))+1;
          %LET FF=%SCAN(&FILELIST.,&F.,/ );

          %IMPORT_TM(&FF.);
     %END;
%MEND LOOP_F;

 

where Import_TM is another macro.  

 

How can i count the number of phrases directly, without resorting to counting delimiters and adding 1?  

1 ACCEPTED SOLUTION

Accepted Solutions
JeffMeyers
Barite | Level 11

You can specify the delimiter in COUNTW.  I do that a lot in examples like:

 

%let x=1,2,3,4;

%do i = 1 %to %sysfunc(countw(%superq(x),%str(,)));
    %put %scan(%superq(x),&i,%str(,));
%end;

%superq above just masks things like commas, quotes etc.  When using commas as the delimiter though in a macro %sysfunc make sure you wrap it with %str to tell SAS it's a macro text string and not separating options.  If in a data step then you would do COUNTW(x,',') if you are using a comma as the delimiter.

View solution in original post

1 REPLY 1
JeffMeyers
Barite | Level 11

You can specify the delimiter in COUNTW.  I do that a lot in examples like:

 

%let x=1,2,3,4;

%do i = 1 %to %sysfunc(countw(%superq(x),%str(,)));
    %put %scan(%superq(x),&i,%str(,));
%end;

%superq above just masks things like commas, quotes etc.  When using commas as the delimiter though in a macro %sysfunc make sure you wrap it with %str to tell SAS it's a macro text string and not separating options.  If in a data step then you would do COUNTW(x,',') if you are using a comma as the delimiter.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1 reply
  • 700 views
  • 0 likes
  • 2 in conversation