The SAS Output Delivery System and reporting techniques

tranwrd() replace comma with linebreak

Accepted Solution Solved
Reply
Regular Contributor
Regular Contributor
Posts: 156
Accepted Solution

tranwrd() replace comma with linebreak

Dear all,

I have a macro variable with a comma separated list of numbers: %let a = 1,2,3,4,5. I'd like to create a sas data set now with one var called id that has 5 rows of which one row contains one number from the comma separated list. Just like in:

data myoutput;

     input id 1;

     datalines;

1

2

3

4

5

;

run;

I though I could mybe do this:

data myoutput;

     input id 1;

     datalines;

&linebreaklist;

run;

And create the Makrovariable &linebreaklist like this:

%let linebreaklist = %sysfunc(tranwrd(%str(1,2,3,4,5),%str(,),\n));

Of course with the above statement I get

1\n2\n3\n4\n5

But I'd like to have

1

2

3

4

5

Does anybody know how I'd have to specify the linebreak in the tranwrd() function?

Best wishes

Eva


Accepted Solutions
Solution
‎01-25-2013 06:07 PM
Super User
Super User
Posts: 6,323

Re: tranwrd() replace comma with linebreak

%let a=1,2,3,4,5;

filename list temp;

data _null_;

   file list;

   put "&a";

run;

data want ;

  infile list dsd ;

  input id @@;

run;

View solution in original post


All Replies
Contributor
Posts: 44

Re: tranwrd() replace comma with linebreak


I have used scan function for cases like this:

%let a = 1,2,3,4,5;

data myoutput(drop=count);

retain count 0;

b='.';

do while (b ne '');

  count+1;

  b=scan("&a",count,',');

  if b ne '' then output;

end ;

run;

Asko

Regular Contributor
Regular Contributor
Posts: 156

Re: tranwrd() replace comma with linebreak

Dear Asko,

yes, that's what I use now. I thought maybe there's a solution without a loop.

Respected Advisor
Posts: 3,775

Re: tranwrd() replace comma with linebreak

You can't do much computer programming withOUT loops.  This one has a loop too. Can you find it?

%let a = 1,2,3,4,5;

data a;
   retain a "&a";
  
infile cards eof=eof dsd;
  
if _n_ eq 1 then do;
     
input @@;
      _infile_ = a;
     
end;
  
input n @@;
   return;
eof:
stop;
  
cards;
!necessary evil!
;;;;;
   run;
Regular Contributor
Regular Contributor
Posts: 156

Re: tranwrd() replace comma with linebreak

;-) Of course I know where the loop is.

I just wanted to have something easier to look at without do while.

I found a hint about @@ in some other forum as well. But haven't found much about what it does. Do you know where I find that in the documentation or could you explain it?

Respected Advisor
Posts: 3,775

Re: tranwrd() replace comma with linebreak

Super Contributor
Posts: 578

Re: tranwrd() replace comma with linebreak

I think you could do it if you wanted a text file output (just replace the comma with the ascii crlf (which i think is '0a0d'x).  I don't think sas datasets work like that as you need to output a new row instead of just replacing the text.

Super Contributor
Posts: 265

Re: tranwrd() replace comma with linebreak

if u just want result then try it,, if macro variable is nt necessary for u..

data test;

x='1,2,3,4,5';

do i= 0 to length(x);

y=substr(x,i+1,1);

i=i+1;

output;

end;

drop i x;

run;

proc print;

run;

Regular Contributor
Regular Contributor
Posts: 156

Re: tranwrd() replace comma with linebreak

Mmh, ok I see. So I'll have a look at @@ in the input statement and then decide whether I use this or the loop.

Thanx a lot for all your helpful answers - I learned a lot!

Respected Advisor
Posts: 4,972

Re: tranwrd() replace comma with linebreak

Another factor may influence your decision here ... where do you plan to put this code?  The CARDS statement is not permitted inside a macro definition.

DN, I found that this small simplification is permissible:

_infile_="&a";

Solution
‎01-25-2013 06:07 PM
Super User
Super User
Posts: 6,323

Re: tranwrd() replace comma with linebreak

%let a=1,2,3,4,5;

filename list temp;

data _null_;

   file list;

   put "&a";

run;

data want ;

  infile list dsd ;

  input id @@;

run;

Super Contributor
Posts: 265

Re: tranwrd() replace comma with linebreak

data test;

input month $ sale;

datalines;

jan 2000

feb 1400

mar 2300

apr 3400

may 2700

jun 3200

july 800

aug 2900

sep 1700

oct 2100

nov 2600

dec 3200

;

run;

i m looking to convert month in num as 1 2 3 ,

for that i m tring it.

data test1;

set test;

mon_num=input(month,monname3.);

run;

proc print; run;

but its showing error. i need to short data by month. can we do it in short or sud i use if condition on each month????

Super Contributor
Posts: 578

Re: tranwrd() replace comma with linebreak

data have;

input month $ sale;

datalines;

jan 2000

feb 1400

mar 2300

apr 3400

may 2700

jun 3200

july 800

aug 2900

sep 1700

oct 2100

nov 2600

dec 3200

;

run;

data want;

format   

    Month_Num 8.;

set have;

month_num=month(input(month||' 01, 2000',anydtdte20.0));

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 1484 views
  • 2 likes
  • 7 in conversation