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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 457 views
  • 0 likes
  • 2 in conversation