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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.