BookmarkSubscribeRSS Feed
Specter
Calcite | Level 5

Hi,

 

i have a Problem with my Code.

 

%macro print2;

%let x = %test% %test2%;

%let myvar=%str('%');

  

%local i next_name;

%do i=1 %to %sysfunc(countw(&x));

%let next_name = %scan(&x, &i);

%put &next_name;

%end;

  

%mend print2;

 

%print2;

 

 

the ouput is :

 

test

test2

 

but i Need this Output

 

%test%

%test2%

 

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The question is why, can yoou give a working example of what you want.  As soon as I see lots of %'s &'s %str() masking and such like, I can guarentee you there is an easier way of doing what you want with simple coding.

Astounding
PROC Star

By default, %SCAN uses percent signs as delimiters:

 

https://v8doc.sas.com/sashtml/macro/z514scan.htm

 

To change that, specify that blank is the only delimiter:

 

%let next_name = %scan(&x, &i, %str( ) ) ;

 

Now generating %test% might cause its own set of problems.  You might want to switch from %scan to %qscan if there are problems.

Kurt_Bremser
Super User

Once again, the futility of lists in macro variables.

data list;
input string $20.;
put string;
cards;
%test%
%test2%
;
run;

Anything you want to do can be done off that dataset with call execute.

data_null__
Jade | Level 19

@Specter wrote:

Hi,

 

i have a Problem with my Code.

 

%macro print2;

%let x = %test% %test2%;

%let myvar=%str('%');

  

%local i next_name;

%do i=1 %to %sysfunc(countw(&x));

%let next_name = %scan(&x, &i);

%put &next_name;

%end;

  

%mend print2;

 

%print2;

 

 

the ouput is :

 

test

test2

 

but i Need this Output

 

%test%

%test2%

 

 


You need to quote the value of X.  This will suppress the first group of invocation notes in the log.

WARNING: Apparent invocation of macro TEST not resolved. Then as mentioned by @Astounding use QSCAN with blank as delimiter, this will suppress the other invocation notes and return the proper values.

 

 55         
 56         %macro print2;
 57            %let x = %nrstr(%%test%% %%test2%%);
 58         
 59            %local i next_name;
 60            %do i=1 %to %sysfunc(countw(&x));
 61               %let next_name = %qscan(&x,&i,%str( ));
 62               %put &next_name;
 63               %end;
 64            %mend print2;
 65         
 66         %print2;
 %test%
 %test2%
 67         
 68       

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 4 replies
  • 995 views
  • 0 likes
  • 5 in conversation