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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.