Writing to .txt file; spacing between output lines

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 120
Accepted Solution

Writing to .txt file; spacing between output lines

I have the attached code (Code.txt file) that I apply to the attached .csv file. Once I run the code and get the output files that I need, I use the code below to write one of the output files (filename: NCDebt7_1) into a .txt file and export it from SAS; I attach the sample output (.txt file). While it's not the cleanest output file, it serves my purpose as long as the following two conditions are met: a) there is a carriage return after the end of each field (see code below in bold) and b) there is only one line between the 'end' fields and the ensuing 'Instrument' fields (see attached NCDebt1.txt file). While I have been able to accomplish the former (see code below in bold), I have been struggling with the latter and I find myself having to resort to achieving my purpose manually. Does anyone have any ideas about SAS code that could automate the process?

    data _null_;

    %let _EFIERR_ = 0; /* set the ERROR detection macro variable */

    %let _EFIREC_ = 0;     /* clear export record count macro variable */

    file '/fmacdata/utility/fin/KBenchmarks/SAS Data/NCDebt1.txt' delimiter='09'x DSD DROPOVER lrecl=32767;

   set  NCDEBT7_1   end=EFIEOD;

       format var1 $21. ;

       format COL1 $80. ;

       format COL2 $80. ;

       format COL3 $80. ;

       format COL4 $80. ;

       format COL5 $80. ;

       format COL6 $80. ;

       format COL7 $80. ;

       format COL8 $80. ;

       format COL9 $80. ;

       format COL10 $80. ;

       format COL11 $80. ;

       format COL12 $80. ;

       format COL13 $80. ;

       format COL14 $80. ;

       format COL15 $80. ;

       format COL16 $80. ;

       format COL17 $80. ;

       format COL18 $80. ;

       format COL19 $80. ;

       format COL20 $80. ;

       format COL21 $80. ;

       format COL22 $80. ;

       format COL23 $80. ;

       format COL24 $80. ;

       format COL25 $80. ;

       format COL26 $80. ;

       format COL27 $80. ;

       format COL28 $80. ;

       format COL29 $80. ;

       format COL30 $80. ;

       format COL31 $80. ;

       format COL32 $80. ;

       format COL33 $80. ;

       format COL34 $80. ;

       format COL35 $80. ;

       format COL36 $80. ;

       format COL37 $80. ;

       format COL38 $80. ;

       format COL39 $80. ;

       format COL40 $80. ;

       format COL41 $80. ;

       format COL42 $80. ;

       format COL43 $80. ;

       format COL44 $80. ;

       format COL45 $80. ;

       format COL46 $80. ;

       format COL47 $80. ;

       format COL48 $80. ;

       format COL49 $80. ;

       format COL50 $80. ;

       format COL51 $80. ;

       format COL52 $80. ;

       format COL53 $80. ;

       format COL54 $80. ;

       format COL55 $80. ;

       format COL56 $80. ;

       format COL57 $80. ;

       format COL58 $80. ;

       format COL59 $80. ;

       format COL60 $80. ;

     do;

       EFIOUT + 1;

       put var1 $ @;

       put COL1 $ @;

       put COL2 $ @;

       put COL3 $ @;

       put COL4 $ @;

       put COL5 $ @;

       put COL6 $ @;

       put COL7 $ @;

       put COL8 $ @;

       put COL9 $ @;

       put COL10 $ @;

       put COL11 $ @;

       put COL12 $ @;

       put COL13 $ @;

       put COL14 $ @;

       put COL15 $ @;

       put COL16 $ @;

       put COL17 $ @;

       put COL18 $ @;

       put COL19 $ @;

       put COL20 $ @;

       put COL21 $ @;

       put COL22 $ @;

       put COL23 $ @;

       put COL24 $ @;

       put COL25 $ @;

       put COL26 $ @;

       put COL27 $ @;

       put COL28 $ @;

       put COL29 $ @;

       put COL30 $ @;

       put COL31 $ @;

       put COL32 $ @;

       put COL33 $ @;

       put COL34 $ @;

       put COL35 $ @;

       put COL36 $ @;

       put COL37 $ @;

       put COL38 $ @;

       put COL39 $ @;

       put COL40 $ @;

       put COL41 $ @;

       put COL42 $ @;

       put COL43 $ @;

       put COL44 $ @;

       put COL45 $ @;

       put COL46 $ @;

       put COL47 $ @;

       put COL48 $ @;

       put COL49 $ @;

       put COL50 $ @;

       put COL51 $ @;

       put COL52 $ @;

       put COL53 $ @;

       put COL54 $ @;

       put COL55 $ @;

       put COL56 $ @;

       put COL57 $ @;

       put COL58 $ @;

       put COL59 $ @;

       put COL60 $ ;

     put '0D'x @;  -----> Carriage return code;  it is imperative that I keep this piece

       ;

     end;

    if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */

    if EFIEOD then call symputx('_EFIREC_',EFIOUT);

    run;

Attachment
Attachment
Attachment

Accepted Solutions
Solution
‎09-17-2014 07:39 PM
Super User
Super User
Posts: 6,368

Re: Writing to .txt file; spacing between output lines

If your file is getting generated with just LineFeed at the end of the lines instead of CarriageReturn and LineFeed then you are probably running on Unix system.

You can add the TERMSTR=CRLF option to the FILE statement and SAS will generate CR+LF at the end of each line even on Unix.

View solution in original post


All Replies
Solution
‎09-17-2014 07:39 PM
Super User
Super User
Posts: 6,368

Re: Writing to .txt file; spacing between output lines

If your file is getting generated with just LineFeed at the end of the lines instead of CarriageReturn and LineFeed then you are probably running on Unix system.

You can add the TERMSTR=CRLF option to the FILE statement and SAS will generate CR+LF at the end of each line even on Unix.

Valued Guide
Posts: 3,206

Re: Writing to .txt file; spacing between output lines

Please review your SAS syntax. File statement SAS(R) 9.4 Statements: Reference, Third Edition and formats SAS(R) 9.4 Formats and Informats: Reference
Leave the Termstr as it is on the OS.  (LF for Unix) CRLF for Windows when you go for a filetranfer approach that includes that translation (yep sFTP is doing that).
When going for deleiver a text file think about the encoding, it is option with the file statement. Today often utf8 is used, most people are brain limited by latin1. 

More....

- You are writing a lot of variables, a list usage would be making more sense.

- Having that 60 variables of $80 you are having 4800 bytes that is more than the 32767 (2byte counter).   

- That dropover will recognize the overflow, that is correct, but I am not seeing the retain.

  You could better use the sum statement SAS(R) 9.4 Statements: Reference, Third Edition  I see you used already that one for the number of observations in an own construct. There are better options for that.
- there are options to work at the file variable _file_ but having 60 fields of 80 long you are having a maximum of 4800 sizing. no problem.

For testing purposes the following.

libname  test "/folders/myfolders/test";
Filename test "/folders/myfolders/test";

  data NCDEBT7_1 ;
    infile datalines dsd dlm="~" missover ;
       Length  var1 $21 strng col1-col60 $80 ;
    input var1 col1-col60  ;
    array coldf col1-col60;
    IF ( _N_ = 3 ) then do;  /* long chars */
    strng='abcdefghijklmnopqrstuvwxyz 0123456789 - ABCDEFGHIJKLMNOPQERSTUVWXYZ !@#$% &*()';
    strngl=length(Strng);
    put ""ength " strngl) ;
    do i=1 to dim(coldf) ;
      coldf{i}=strng ;
    end;
    end; 
  datalines ;
    A~c11~c12~c13~c14~c15~
    B~c21~c22~c23~c24~c25~
    C~c31~c32~c33~c34~c35~
    D~c41~c42~c43~c44~c45~
  run;

  data _null_;
    file test('NCDebt1.txt') delimiter='09'x DSD DROPOVER lrecl=32767;
   set  NCDEBT7_1   end=EFIEOD nobs=_efirec ;
    format var1 $21. col1-col60 $80. ;
    put var1 col1-col60 ;
    if _ERROR_ then _EFIERR+1;  /* set ERROR detection macro variable */
    if EFIEOD then do;
      call symputx('_EFIREC',_EFIREC);
      call symputx('_EFIERR',_EFIERR);
    end; 
    run;
   
%macro status ;
   %put ". data step NCDept1.txt procesing status:&sysrc &syserr " ;
   %put ".   Number of records &_efirec   dropped over: &_efierr " ;
%mend;
%status;

---->-- ja karman --<-----
☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 573 views
  • 3 likes
  • 3 in conversation