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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.