BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

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.

 

 

 

 

 

 

 

 

 

 

 

 

2 REPLIES 2
FreelanceReinh
Jade | Level 19

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;
PaigeMiller
Diamond | Level 26

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;

 

 

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 8327 views
  • 1 like
  • 3 in conversation