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%
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.
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.
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.
@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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.