Learning SAS? Welcome to the exclusive online community for all SAS learners.

I want to write a text file with a line feed at the end of every line

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 83
Accepted Solution

I want to write a text file with a line feed at the end of every line

I want to write a text file with a line feed or carriage return at the end of every line.

I don't know how to code that in the put statement or the file statement;

I can write the file OK it just dosn't have the line feeds in it.

Thanks for the help.     Jim


Accepted Solutions
Solution
‎03-07-2015 10:25 AM
Respected Advisor
Posts: 3,777

Re: I want to write a text file with a line feed at the end of every line

That's not how you want to do it in a SAS program.  If you want to create a file that has DOS record termination use the TERMSTR=CRLF file statement option.  Notice below the output from the LIST statement that each line ends with '0d0a'x;

filename FT61F001 temp;

data _null_;
  
file FT61F001 TERMSTR=crlf;
   do j=1 to 2;
     
put j=;
      end;
  
run;
data _null_;
  
infile FT61F001 recfm=f;
   input;
  
list;
  
run;


32         filename FT61F001 temp;
33        
34         data _null_;
35            file FT61F001 TERMSTR=crlf;
36            do j=1 to 2;
37               put j=;
38               end;
39            run;

NOTE:
The file FT61F001 is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE:
2 records were written to the file (system-specific pathname).
      The minimum record length was
3.
      The maximum record length was
3.
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds
     

40         data _null_;
41            infile FT61F001 recfm=f;
42            input;
43            list;
44            run;

NOTE:
The infile FT61F001 is:
      (system-specific pathname),
      (system-specific file attributes)

RULE:     ----+----
1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                     

1   CHAR  j=1..j=2.. 10
    ZONE 
6330063300
    NUMR  AD1DAAD2DA
NOTE:
1 record was read from the infile (system-specific pathname).
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds

View solution in original post


All Replies
Super User
Super User
Posts: 7,400

Re: I want to write a text file with a line feed at the end of every line

Can you post your code and some sample data as:

data _null_;

  set sashelp.bmt;

  file "s:\temp\rob\temp.txt";

  put group t status;

run;

Should work just fine.

Frequent Contributor
Posts: 83

Re: I want to write a text file with a line feed at the end of every line

I'm using SAS University Edition on a windows PC.  My SAS code writes the file OK but when I look at it with Notepad all the observations are in 1 record.    I think there is a hex character that needs put at the end of each PUT statement or some key word in the FILE statement that says the file has line feeds.

Super User
Super User
Posts: 7,400

Re: I want to write a text file with a line feed at the end of every line

Sorry, I can't be of further help here as I don't use the UE, however the above code I placed works fine in base SAS, so I would assume its a limitation of the UE (others would probably comment further on that).

Frequent Contributor
Posts: 83

Re: I want to write a text file with a line feed at the end of every line

I found one answer. (In SAS there are many ways to do things) .

The line feed on a TXT file is character    HEX '0D'.

I set up a hex constant  CR=input('0D',$hex2.);

data _null_;  set;  

   file '/folders/myfolders/rawfiles/lionel_price01.txt';

  

  cr=input('0D',$hex2.);

  put @1 era  @10 cat @20 v7 @30 '.   '      cr;     Run;

Solution
‎03-07-2015 10:25 AM
Respected Advisor
Posts: 3,777

Re: I want to write a text file with a line feed at the end of every line

That's not how you want to do it in a SAS program.  If you want to create a file that has DOS record termination use the TERMSTR=CRLF file statement option.  Notice below the output from the LIST statement that each line ends with '0d0a'x;

filename FT61F001 temp;

data _null_;
  
file FT61F001 TERMSTR=crlf;
   do j=1 to 2;
     
put j=;
      end;
  
run;
data _null_;
  
infile FT61F001 recfm=f;
   input;
  
list;
  
run;


32         filename FT61F001 temp;
33        
34         data _null_;
35            file FT61F001 TERMSTR=crlf;
36            do j=1 to 2;
37               put j=;
38               end;
39            run;

NOTE:
The file FT61F001 is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE:
2 records were written to the file (system-specific pathname).
      The minimum record length was
3.
      The maximum record length was
3.
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds
     

40         data _null_;
41            infile FT61F001 recfm=f;
42            input;
43            list;
44            run;

NOTE:
The infile FT61F001 is:
      (system-specific pathname),
      (system-specific file attributes)

RULE:     ----+----
1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                     

1   CHAR  j=1..j=2.. 10
    ZONE 
6330063300
    NUMR  AD1DAAD2DA
NOTE:
1 record was read from the infile (system-specific pathname).
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds
Super User
Super User
Posts: 6,499

Re: I want to write a text file with a line feed at the end of every line

Use the TERMSTR=CRLF option on the FILE statement as data_null_ has posted.

What is happening is that SAS UE edition is running under Unix and Notepad is a Windows (DOS) program.  The convention of text files on a Unix machine is to end the lines with just a LineFeed ('0A'x) and the convention on a DOS or Windows machine is to use CarriageReturn and LineFeed ('0D0A'x) to end the lines.  Since your SAS code is running on Unix it will follow the Unix convention by default.  Notepad is too dumb to recognize what is going on. You could also use a smarter editor such as Notepad+ that can adapt to unix style text files.

New User
Posts: 1

Re: I want to write a text file with a line feed at the end of every line

[ Edited ]

The TERMSTR=crlf solution didn't work for me.  Actually, in the online documentation for 9.3, it doesn't look like SAS still supports the TERMSTR option on the FILE statement.

 

I'm using PC SAS on Windows 7, here's my LOG notes:

NOTE: Copyright (c) 2002-2010 by SAS Institute Inc., Cary, NC, USA.
NOTE: SAS (r) Proprietary Software 9.3 (TS1M2)
NOTE: This session is executing on the W32_7PRO platform.

 

I found that I had to be very specific about LS and LRECL for the text file to look right -- i.e. for the data to actually start in column 1.  I'm using RECFM=F[ixed], and the data itself takes up exactly 780 bytes, but I set LS and LRECL to 782, because the CR+LF characters imbedded into the text file are 2 bytes long.  

 

If I set LS and LRECL to *less* than the exact length, then the data on lines 2 onward start in the column before the length I set. (e.g. when I set it to 780, lines 2 onward started in column 779)

 

If I set LS and LRECL to *more* than the exact length, then the data on lines 2 onward start in column "X", where "X" is the number of bytes over the exact length required.  (e.g. when I set it to 784, lines 2 onward started in column 2)

 

*--- write to FLATFILE;
data _null_; set &indata;
  file FLATFILE recfm=F ls=782 lrecl=782 ;

  length zzz_crlf_tempfield $2.;
  zzz_crlf_tempfield = byte(13)||byte(10);

  put
  @   1  SEQU                             8.
  @   9  PRODCODU                         $25.
  ...
  @ 772 IMPORTER                          $7.
  @ 779  BRANCH                           $2.
  zzz_crlf_tempfield
  ;
run;

*--- read from FLATFILE;
data &outdata;
  infile FLATFILE recfm=F ls=782 lrecl=782 ;

  length zzz_crlf_tempfield $2.;

  input
  @   1  SEQU                             8.
  @   9  PRODCODU                         $25.
  ...
  @ 772 IMPORTER                          $7.
  @ 779  BRANCH                           $2.
  zzz_crlf_tempfield
  ;
run;

 

 

Frequent Contributor
Posts: 83

Re: I want to write a text file with a line feed at the end of every line

Thank you  data_null_ and Tom.   

My answer  worked for me but you have suggested the correct answer.

  Jim

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 5221 views
  • 2 likes
  • 5 in conversation