DATA Step, Macro, Functions and more

How to write output to a text file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

How to write output to a text file

I just want an extended version of this output with two more variables. Let's say I have the dataset like the below.

 

data have;

input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;

datalines;

Name1 RD 10 RS 1 2 AS GS

Name2 RD 20 RS 3 4 AS TS

Name2 RD 20 RS 5 6 AS TT

Name2 RD 20 RS 7 8 AS ST

Name3 RD 30 RS 11 12 AS GS

Name3 RD 30 RS 13 14 AS HG

Name4 RD 40 RS 15 16 AS GS

;

 

Is it possible to get an output like the below. There is one more variable which has more than one value for one Name and that the order is also changed.

 

Name1
RD|10
RS|1|2
AS

GS
Name2
RD|20
RS|3|4
RS|5|6
RS|7|8
AS

TS

TT

ST
Name3
RD|30
RS|11|12
RS|13|14
AS

GS

HG
Name4
RD|40
RS|15|16
AS

GS

 

Thanks in advance!


Accepted Solutions
Solution
‎08-30-2017 07:17 AM
Super Contributor
Posts: 345

Re: How to write output to a text file

Posted in reply to BalajiBollu

Well, this is really ugly code.

 

data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;

data _null_;
   set have;
   by F1;
   file "&Benutzer\narf.txt";

   length line buffer3 buffer5 $ 200;
   retain buffer:;

   array buffs[2] buffer3 buffer5;

   if first.F1 then do;
      call missing(of buffer:);
      put F1;

      line = catx('|', F2, F2_1);
      put line;
   end;

   line = catx('|', of F4:);
   put line;

   if not find(buffer3, F3) then do;
      buffer3 = catx(' ', buffer3, F3);
   end;

   if not find(buffer5, F5) then do;
      buffer5 = catx(' ', buffer5, F5);
   end;

   if last.F1 then do;
      do b = 1 to dim(buffs);
         do i = 1 to countw(buffs[b]);
            line = scan(buffs[b], i, ' ');
            put line;
         end;
      end;
   end;

run;

View solution in original post


All Replies
Super Contributor
Posts: 345

Re: How to write output to a text file

Posted in reply to BalajiBollu

Can you describe to transforming logic?

 

Are F2 and F2_1 always the same for one F1?

Occasional Contributor
Posts: 9

Re: How to write output to a text file

Posted in reply to andreas_lds

Yes F2 and F2_1 will have only one value for one F1 Value. Only F4 F4_1 F4_2 and F5 will have more than one value for one F1.

Respected Advisor
Posts: 4,173

Re: How to write output to a text file

Posted in reply to BalajiBollu

@BalajiBollu

Something like below might do:

data have;
  input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
  datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;

data _null_;
  if _n_=1 then
    do;
      length order 8 String $32;
      dcl hash h1(ordered:'y');
      h1.defineKey('F1','order','String');
      h1.defineData('String');
      h1.defineDone();
      call missing(of _all_);
    end;

  set have end=last;

  order=1; String=F1; 
  h1.ref();
  order=2; String=catx('|',of F2:); 
  h1.ref();
  order=3; String=catx('|',of F4:); 
  h1.ref();
  order=4; String=F3; 
  h1.ref();
  order=5; String=F5; 
  h1.ref();

  if last then h1.output(dataset:'want(keep=String)');
run;

data _null_;
  file print;
  set want;
  put String;
run;
Occasional Contributor
Posts: 9

Re: How to write output to a text file

Thanks much!!!

Solution
‎08-30-2017 07:17 AM
Super Contributor
Posts: 345

Re: How to write output to a text file

Posted in reply to BalajiBollu

Well, this is really ugly code.

 

data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;
run;

data _null_;
   set have;
   by F1;
   file "&Benutzer\narf.txt";

   length line buffer3 buffer5 $ 200;
   retain buffer:;

   array buffs[2] buffer3 buffer5;

   if first.F1 then do;
      call missing(of buffer:);
      put F1;

      line = catx('|', F2, F2_1);
      put line;
   end;

   line = catx('|', of F4:);
   put line;

   if not find(buffer3, F3) then do;
      buffer3 = catx(' ', buffer3, F3);
   end;

   if not find(buffer5, F5) then do;
      buffer5 = catx(' ', buffer5, F5);
   end;

   if last.F1 then do;
      do b = 1 to dim(buffs);
         do i = 1 to countw(buffs[b]);
            line = scan(buffs[b], i, ' ');
            put line;
         end;
      end;
   end;

run;
Super User
Posts: 10,035

Re: How to write output to a text file

Posted in reply to BalajiBollu

How about this one ?

 

data have;
input F1$ F2$ F2_1$ F4$ F4_1$ F4_2$ F3$ F5$;
datalines;
Name1 RD 10 RS 1 2 AS GS
Name2 RD 20 RS 3 4 AS TS
Name2 RD 20 RS 5 6 AS TT
Name2 RD 20 RS 7 8 AS ST
Name3 RD 30 RS 11 12 AS GS
Name3 RD 30 RS 13 14 AS HG
Name4 RD 40 RS 15 16 AS GS
;

filename x temp;
data _null_;
 file x;
 length x y $ 20000;
 do i=1 by 1 until(last.f2_1);
   set have;
   by f1 f2 f2_1;
   x= catx(',',x,catx('|',F4,F4_1,F4_2));
   if i=1 then y=catx(',',y,F3,F5);
     else y=catx(',',y,F5);
 end; 
 put F1;
 put F2 +(-1) '|'  F2_1;
 do i=1 to countw(x,',');
  temp=scan(x,i,',');
  put temp; 
 end;
 do i=1 to countw(y,',');
  temp=scan(y,i,',');
  put temp;
 end;
 run;

 data _null_;
  infile x;
  input;
  put _infile_;
run;

 

 

Occasional Contributor
Posts: 9

Re: How to write output to a text file

Thank you!

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 142 views
  • 3 likes
  • 4 in conversation