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