DATA Step, Macro, Functions and more

Creating .txt file without a lastline.

Reply
Occasional Contributor
Posts: 6

Creating .txt file without a lastline.

Hello,

I'm creating a. txt file via SAS, but always at the end of generated file I own a blank line.

I searched many forums and found no solution other than one created after the lastline "put" request.

Anyone know if you can not created this line?

CODE Example:

data _NULL_;

file "\\srvtbsas06\SASDATA\ma\context\dbm\export\carta\testes.txt";

put

@1 'TESTE'

@6 "-1";

put

@1 'TESTE'

@6 "-2";

put

@1 'TESTE'

@6 "-3";

run;

Attached the result and the expected.

Tnks!!!


Teste_Expected.bmpTeste_Result.bmp
Super User
Super User
Posts: 6,502

Creating .txt file without a lastline.

That code will NOT write a blank line to the file.  You should probably check the settings of your text editor.

Now if you want to write a file where the last line does NOT end with the end-of-line string then you can add an @ to the end of your last PUT statement.  Try that and see if your editor likes it better.

Super User
Posts: 10,538

Creating .txt file without a lastline.

It might be that you are actually seeing the end of file marker and is dependent on the program.

When I delete all of the text in a file and close the program, Wordpad will show a "character" if highlighting the start of the file but Notepad does not after reopening.

Occasional Contributor
Posts: 6

Re: Creating .txt file without a lastline.

Thanks for the answers, but adding @ to the and of last put, i have the same result.

Is not the editor... The desired is the last line does NOT end with the end-of-line string, the well-known "0D 0A" in hexa...

Tom, i add this @ in this place, its correct?

data _NULL_;

file "\\srvtbsas06\SASDATA\ma\context\dbm\export\carta\testes.txt";

put

@1 'TESTE'

@6 "-1";

put

@1 'TESTE'

@6 "-2";

put

@1 'TESTE'

@6 "-3" @;

run;


Exemplo.bmp
Super User
Super User
Posts: 6,502

Re: Creating .txt file without a lastline.

What I suggested does not work. SAS still writes the end of line characters even if you hold the line with the trailing @;

You can see the LF ('0A'x) that Unix SAS has written in the sample below.

15   filename tmpfile1 temp;

16

17   data _NULL_;

18     file tmpfile1;

19     put @1 'TESTE' @6 "-1";

20     put @1 'TESTE' @6 "-2";

21     put @1 'TESTE' @6 "-3" @;

22   run;

NOTE: 3 records were written to the file TMPFILE1.

      The minimum record length was 7.

      The maximum record length was 7.

23

24   data _null_;

25     infile tmpfile1 recfm=f ;

26     input;

27     list;

28   run;

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

1   CHAR  TESTE-1.TESTE-2.TESTE-3. 24

    ZONE  545542305455423054554230

    NUMR  45345D1A45345D2A45345D3A

NOTE: 1 record was read from the infile TMPFILE1.

You can play around the record format options on the FILE statement.

Here is something that works for this example because the record length is fixed.

Although it did add a space at the end in the place I reserved for the LF in the fixed length records.

72   filename tmpfile1 temp;

73

74   data _NULL_;

75     file tmpfile1 recfm=f lrecl=8;

76     lf='0a'x;

77     put @1 'TESTE' @6 "-1" @7 lf $1.;

78     put @1 'TESTE' @6 "-2" @7 lf $1.;

79     put @1 'TESTE' @6 "-3";

80   run;

NOTE: 3 records were written to the file TMPFILE1.

81

82   data _null_;

83     infile tmpfile1 recfm=f ;

84     input;

85     list;

86   run;

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

1   CHAR  TESTE-. TESTE-. TESTE-3  24

    ZONE  545542025455420254554232

    NUMR  45345DA045345DA045345D30

NOTE: 1 record was read from the infile TMPFILE1.

Super User
Super User
Posts: 6,502

Re: Creating .txt file without a lastline.

Why would you want a file in that non standard format?

What happens in your editor if you open the file generated by SAS and then save it into a new file?  Does the editor add a blank line to the end of the file?  If not then what is the issue with using the file as created by SAS?

Occasional Contributor
Posts: 6

Re: Creating .txt file without a lastline.

Hi Tom!

I need this file in that non standard format becose one supplier of my company... The file generated is available directly to the supplier...

If i save into a new file the editor does not add a blank line...

The idea almost worked, but is added a space (hex "20") at the end of the last record :-(.

Can i remove this space manipulating the file into SAS?

file "\\srvtbsas06\SASDATA\ma\context\dbm\export\carta\testes.txt" recfm=F lrecl=8;

lf='0a'x;

put @1 'TESTE' @6 "-1" @8 lf ;

put @1 'TESTE' @6 "-2" @8 lf ;

put @1 'TESTE' @6 "-3";

run;

Super User
Super User
Posts: 6,502

Re: Creating .txt file without a lastline.

Try RECFM=N.

This seems to work for your example.

255  filename tmpfile1 temp;

256

257  data _NULL_;

258    file tmpfile1 recfm=n ;

259    lf='0d0a'x;

260    do i=-1 to -3 by -1;

261      put 'TESTE' i 2. @;

262      if i ^=-3 then put lf $2. @;

263    end;

264  run;

NOTE: UNBUFFERED is the default with RECFM=N.

265

266  data _null_;

267    infile tmpfile1 recfm=f ;

268    input;

269    list;

270  run;

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

1   CHAR  TESTE-1..TESTE-2..TESTE-3 25

    ZONE  5455423005455423005455423

    NUMR  45345D1DA45345D2DA45345D3

NOTE: 1 record was read from the infile TMPFILE1.

PROC Star
Posts: 7,364

Re: Creating .txt file without a lastline.

If you know how many records you have you could also always just to a bit of post processing.  e.g.:

data _NULL_;

  file "c:\art\testes.txt";

  put @1 'TESTE'

      @6 "-1";

  put @1 'TESTE'

      @6 "-2";

  put @1 'TESTE'

      @6 "-3";

run;

data _NULL_ ;

  FILE "c:\art\testes_want.txt" RECFM=N ;

  infile "c:\art\testes.txt" RECFM=N end=eof;

  input VAR1 $CHAR1. @;

  if var1 eq '0D'x then counter+1;

  if counter lt 3 then put VAR1 $CHAR1. ;

run;

Occasional Contributor
Posts: 6

Creating .txt file without a lastline.

Art! thats work perfect!!!!

But after that I had another problem... oh my god...!

When i upload the file using a ftp i have again de 0D 0A in the file, becose in ftp file i can´t insert a option RECFM=N.

         filename MeuArq ftp "&carta_cd._&dt_gr_arq..txt" cd="&path_destino_carta_FTP."

             host="&FTPservidor."

             user="&FTPusuario." pass="&FTPsenha." debug mprompt lrecl=503;

        

           data _null_;

            infile "&path_saida.\&carta_cd._&dt_gr_arq..txt" lrecl=503;

              input;

              file MeuArq;

             put _infile_;

           run;

-----------------------------------------

But i resolved this problem, was ugly i know...

I write the file and after this i append 1 row in the end... i do the same for upload by ftp...

like this:

data _NULL_;

file "testes.txt";

put

@1 'TESTE'

@6 "-1";

put

@1 'TESTE'

@6 "-2";

put

@1 'TESTE'

@6 "-3";

run;

 

data _NULL_;

 

file "testes.txt" mod recfm=F lrecl=5;

  

put

@1 'TESTE';

run;

Super User
Super User
Posts: 6,502

Re: Creating .txt file without a lastline.

You should move the file as BINARY with FTP to prevent it from adjusting the end-of-line characters.

Occasional Contributor
Posts: 6

Creating .txt file without a lastline.

Hi Tom,

I try use the option Binary into FTP, but no sucess...

I belive it is becose de "put _infile_" :-(

Code:

         filename MeuArq ftp "&carta_cd._&dt_gr_arq..txt" cd="&path_destino_carta_FTP."

             host="&FTPservidor."

                                         rcmd='binary'

             user="&FTPusuario." pass="&FTPsenha." debug mprompt lrecl=503;

        

           data _null_;

            infile "&path_saida.\&carta_cd._&dt_gr_arq..txt" lrecl=503;

              input;

              file MeuArq;

              put _infile_;

           run;

PROC Star
Posts: 7,364

Creating .txt file without a lastline.

Why bother reading and writing the file?  You could use the same method to just send the created file.  Take a look at the later example shown in http://dc-sug.org/ftp.pdf

Ask a Question
Discussion stats
  • 12 replies
  • 1366 views
  • 0 likes
  • 4 in conversation