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?
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.
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.
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!
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.