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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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