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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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