DATA Step, Macro, Functions and more

Masking % but allow &

Reply
New Contributor
Posts: 2

Masking % but allow &

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%

 

 

Super User
Super User
Posts: 9,384

Re: Masking % but allow &

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.

Super User
Posts: 6,622

Re: Masking % but allow &

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.

Super User
Posts: 9,866

Re: Masking % but allow &

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,845

Re: Masking % but allow &

[ Edited ]

@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       

 

Ask a Question
Discussion stats
  • 4 replies
  • 104 views
  • 0 likes
  • 5 in conversation