BookmarkSubscribeRSS Feed
edwolfe
Calcite | Level 5
 
I'm writing a macro that contains a macro parameter with a list of values, and I want to print each of those values on a separate line of the output file. I've tried using TRNWRD to do this, but the spaces in the list are not being replaced with line returns.

 

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);
3 REPLIES 3
yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



LeonidBatkhan
Lapis Lazuli | Level 10

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.

SASKiwi
PROC Star

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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 722 views
  • 7 likes
  • 4 in conversation