Have:
List=*
1 2 3 4
*
Want:
LIST=*
1
2
3
4
*
When I run the example code, in which I was trying to use TRANWRD to convert spaces to line returns, I get what I thought was the line return code instead of the line returns:
1'0D0A'x2'0D0A'x3'0D0A'x4
Example Code:
%macro test(list);
%Let List2 = %sysfunc(tranwrd(&List,%str( ),'0D0A'x));
data _null_;
put
"LIST=*" /
"&List2" /
"*"
;
run;
%mend test;
%test(1 2 3 4);
If it's not a problem you can do it with the call symputX() routine,
%macro test(list);
data _null_;
call symputx("List2", tranwrd(symget('List')," ",'0D0A'x),"L");
run;
data _null_;
x = "&list2.";
put
"LIST=*" /
"&List2" /
"*" /
x= $hex32.
;
run;
%mend test;
%test(1 2 3 4);
but don't expect to see "new lines" in the log, SAS ignores them in the log. You have to write it down to external file:
%macro test(list);
data _null_;
call symputx("List2", tranwrd(symget('List')," ",'0D0A'x),"L");
run;
filename f "%sysfunc(pathname(work))/text.txt";
filename f list;
data _null_;
file f;
x = "&list2.";
put
"LIST=*" /
"&List2" /
"*" /
x= $hex32.
;
run;
data _null_;
infile f;
input;
put _N_= "**" _infile_;
run;
%mend test;
%test(1 2 3 4);
[EDIT:]
This technical note my also help:
https://support.sas.com/kb/36/916.html
All the best
Bart
Hi edwolfe,
You can achieve this by the following code:
%macro test(list);
%put LIST=*;
%do i=1 %to %sysfunc(countw(&list));
%put %scan(&list,&i);
%end;
%put *;
%mend test;
%test(1 2 3 4);
This code should produce
LIST=* 1 2 3 4 *
Or, if you need to output it to an external file, you can use the following code modification:
%macro test(list);
data _null_;
file 'c:\temp\test.txt';
put 'LIST=*' /
%do i=1 %to %sysfunc(countw(&list));
"%scan(&list,&i)" /
%end;
'*';
run;
%mend test;
%test(1 2 3 4);
Hope this helps.
I would strongly advise against adding printing controls in to macro parameters. You are adding complexity to your processing logic which isn't useful and could make it difficult to maintain. @LeonidBatkhan's approach is way better.
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.