BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SannaSanna
Quartz | Level 8

Hello. 

 

I am trying to export a text file with specific position points for columns.  I am having difficulty with removing the extra blank line after each row of data.   This text file must have line record length of 455 and CRLF return but it seems like my file is producing CR after each row of data.   This is the code I am using.  Can anyone see what needs to be modified.  Any help would be greatly appreciated.  Thank you. 

 

 

DATA _NULL_;

file 'D:\EF/VACHLINVARMv1.TXT' Ls= 445;

   IF _N_ = 1 THEN DO;

   SET E.tatehdr;

   PUT

      @1    FILE_TYPE

      @51 TOTAL_RECORDS_IN_FILE

      @61 FILE_NAME

      @86 FILE_IDREC

      @118 FILE_RPT_PERIOD ;

END;

      SET E.slevel;    

PUT @1 RECORD_NUM @11 FIPS @13 AGNCY_NUM  @49 TABLE_NAME @69 WEA

    @119 TYPE @169 FinalWea  @184 TYPE2 @234 TOTAL_INDICATOR @435 COUNT @445 '0D'x;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Peter_C
Rhodochrosite | Level 12
SAS adds the TERMSTR (depending on platform, LF or CRLF)  to the results of PUT statements, unless you set RECFM=F 
(the default for RECFM is V).

Of course, you can use FILE statement options PAD and TERMSTR= CRLF (assuming default is still RECFM=V)


That is why you might get 446 or larger, instead of the 445.

( by adding that TERMSTR)


Avoid that problem by using RECFM=F and LRECL=445. Then the record length should be truncated to 445.


Just because the last item on the PUT statement places a 0Dx at 445 doesn't mean that is the total line width:
Beware using the implied format for variable COUNT in 
  @435 COUNT 

A default numeric format might use character width 12 which results in 446 characters before the TERMSTR is added, when RECFM=V

Also, earlier variables might be big enough to overflow that 445


Full clarity needs something like a PROC Contents report of the input dataset and the log of the session including the NOTEs following this export step

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
Have a look at FILE statement options
RECFM= F
with that you would probably use
LRECL= 455
Patrick
Opal | Level 21

Your code with a few tweaks as below should return what you're after.

DATA _NULL_;
  file 'D:\EF/VACHLINVARMv1.TXT' lrecl=455 pad termstr=crlf;
  IF _N_ = 1 THEN
    DO;
      SET E.tatehdr;
      PUT
        @1    FILE_TYPE
        @51   TOTAL_RECORDS_IN_FILE
        @61   FILE_NAME
        @86   FILE_IDREC
        @118  FILE_RPT_PERIOD
        ;
    END;
  SET E.slevel;
  PUT 
    @1 RECORD_NUM 
    @11 FIPS 
    @13 AGNCY_NUM  
    @49 TABLE_NAME 
    @69 WEA
    @119 TYPE 
    @169 FinalWea  
    @184 TYPE2 
    @234 TOTAL_INDICATOR 
    @435 COUNT 
    ;
RUN;
Tom
Super User Tom
Super User

Why are you writing a CR at column 455 if you don't WANT a CR at column 455?

 

If you want to write fixed length records then change the settings in the FILE statement.

file 'myfile' lrecl=455 ;

If you don't want any end-of-line characters then use RECFM=F instead of the default RECFM=V.

If you want to make a variable length file, but have every line padded to the full LRECL then add the PAD option.

 

If you want to control what  end-of-line characters written the use the TERMSTR= option of the FILE statement.  You can choose to have CRLF ( a DOS aka Windows file), LF (a Unix file) or CR (the abandoned standard of the 1980's MacOS).

 

 

SannaSanna
Quartz | Level 8

Hi. Do you mean something like this (see below)? Still did not work. My system tells me the line is incorrect as it found record length of 456 when the expected length must be 444-445.


DATA _NULL_;
file 'D:\EF/VACHLINVARMv1.TXT' pad termstr=crlf lrecl=445 ;
IF _N_ = 1 THEN
DO;
SET E.tatehdr;
PUT
@1 FILE_TYPE
@51 TOTAL_RECORDS_IN_FILE
@61 FILE_NAME
@86 FILE_IDREC
@118 FILE_RPT_PERIOD
;
END;
SET E.slevel;
PUT
@1 RECORD_NUM
@11 FIPS
@13 AGNCY_NUM
@49 TABLE_NAME
@69 WEA
@119 TYPE
@169 FinalWea
@184 TYPE2
@234 TOTAL_INDICATOR
@435 COUNT
;
RUN;

Peter_C
Rhodochrosite | Level 12
SAS adds the TERMSTR (depending on platform, LF or CRLF)  to the results of PUT statements, unless you set RECFM=F 
(the default for RECFM is V).

Of course, you can use FILE statement options PAD and TERMSTR= CRLF (assuming default is still RECFM=V)


That is why you might get 446 or larger, instead of the 445.

( by adding that TERMSTR)


Avoid that problem by using RECFM=F and LRECL=445. Then the record length should be truncated to 445.


Just because the last item on the PUT statement places a 0Dx at 445 doesn't mean that is the total line width:
Beware using the implied format for variable COUNT in 
  @435 COUNT 

A default numeric format might use character width 12 which results in 446 characters before the TERMSTR is added, when RECFM=V

Also, earlier variables might be big enough to overflow that 445


Full clarity needs something like a PROC Contents report of the input dataset and the log of the session including the NOTEs following this export step
SannaSanna
Quartz | Level 8
Thank you so very much for this response.
Tom
Super User Tom
Super User

Read the file back in with SAS.  The NOTEs will show you the min and max line length.

data _null_;
  infile 'D:\EF/VACHLINVARMv1.TXT' ;
  input;
run;   

If that is showing 455 then the problem is with the other software.  What software is it that is saying the lines have more than 455 characters?

 

Things to check.

  • Does the other software expect the lines to have just LF as end-of-line marker? If so it will count the CR as one of the characters on the line.
  • Does the variable COUNT have values that take more than one character to print? Such as 10 or -1 or 1.5 ?
  • Do any of the character values use non 7-bit ASCII characters?  What encoding is your SAS session using?  What encoding is the target system expecting.  
    • Note it really only makes sense to use a single byte encoding when using fixed position files.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 781 views
  • 0 likes
  • 5 in conversation