BookmarkSubscribeRSS Feed
Samuel_Rocha
Calcite | Level 5

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
12 REPLIES 12
Tom
Super User Tom
Super User

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.

ballardw
Super User

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.

Samuel_Rocha
Calcite | Level 5

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
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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?

Samuel_Rocha
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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.

art297
Opal | Level 21

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;

Samuel_Rocha
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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

Samuel_Rocha
Calcite | Level 5

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;

art297
Opal | Level 21

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 4096 views
  • 0 likes
  • 4 in conversation