Suppose my macro variable contain three words : "Word One" Word2 "Word Three";
%varlist = "Word One" Word2 "Word Three";
%macro do_all;
%do i=1 %to %sysfunc(countw(&varlist ));
%let term=%scan(&varlist, &i);
%put &term;
%end;
%mend;
My problem that some variable names consist of two words that separated by blank and this code doesn't work.
Thank you
My problem, that some variable names consist of two words that separated by blank.
Hi @AlexeyS,
Use the "q" modifier of both the COUNTW function and the %SCAN function to ignore the blanks inside quotation marks:
%macro do_all;
%do i=1 %to %sysfunc(countw(&varlist, %str( ), q));
%let term=%scan(&varlist, &i, %str( ), q);
%put &term;
%end;
%mend;
I point out that there's almost never a need to have macro variable values with quotes around them, and its just not a good idea. (What would you do with some of these macro variable words that have quotes around them and others that do not?)
A better idea would be to have macro variable values without the quotes around them, and a delimiter of some special character.
%macro do_all;
%let varlist = Word One~Word2~Word Three;
%do i=1 %to %sysfunc(countw(&varlist,~));
%let term=%scan(&varlist,&i,~);
%put &=term;
%end;
%mend;
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.