BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Dear community members,

Hope you are healthy and feeling well these days.

I am not a programmer, so please forgive me for my unprofessional language. This is my problem:

I import into SAS a text file with no delimiters; variables are defined by fixed spaces. I am now getting this file in an old version (LRECL=316) and a new version (LRECL=322) with some changes in certain fields. I need to differentiate between the two based on this difference alone. Is there a function in SAS or SAS/SQL that will return the LRECL of a text file?

TIA,

Michael

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do this first:

data _null_;
infile "yourfile" lrecl=350 length=rlen obs=1;
input;
call symputx('rlen',rlen);
run;

This will store the observed length of the first line into &rlen, so you can run the following code conditionally.

 

PS if you read the file with RECFM=F or RECFM=N, which means that no line separators are present in the file, you will need a more complicated trick.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Do this first:

data _null_;
infile "yourfile" lrecl=350 length=rlen obs=1;
input;
call symputx('rlen',rlen);
run;

This will store the observed length of the first line into &rlen, so you can run the following code conditionally.

 

PS if you read the file with RECFM=F or RECFM=N, which means that no line separators are present in the file, you will need a more complicated trick.

Ksharp
Super User
data _null_;
 infile 'c:\temp\have.csv' end=last;
 input;
 retain lrecl ;
 lrecl=max(lrecl,length(_infile_));
 if last then putlog 'max lrecl is ' lrecl;
 run;

 

 

NOTE: The infile 'c:\temp\have.csv' is:
      File Name=c:\temp\have.csv,
      RECFM=V,LRECL=256

max lrecl is 74
NOTE: 148 records were read from the infile 'c:\temp\have.csv'.
      The minimum record length was 0.
      The maximum record length was 74.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

RichardDeVen
Barite | Level 11

Text file with fixed record layout and also HAS line termination characters

Use @Kurt_Bremser example utilizing INFILE LENGTH=<variable> option to retrieve data record length from 1st line of file.

 

Text file with fixed record layout and has NO line termination characters

The MOD function can be useful

  • Determine filesize
  • Compute
    • R1 = mod (filesize,316)
    • R2 = mod (filesize,322)
  • if R1 = 0 and R2 ^= 0 presume record length 316; else
    if R1 ^= 0 and R2 = 0 presume record length 322; else
    filesize is k x LCM(316,322).  Perform test inputs and your understanding of expected values to determine which record length (i.e. record type) you have.
    Note: LCM(316,322) is filesize 32,232

Example code for determining file size (in Windows)

filename au 'autoexec.sas';

data _null_;
  fid = fopen ('au');

  filesize = finfo(fid,'file size (bytes)');
  put 'NOTE: ' filesize=;

  rc = fclose (fid);
run;

 

MichaelvanStraten
Fluorite | Level 6

Dear KurtBremser, Ksharp and RichardADeVenezia,

Thank you all for your helpful solutions.

Michael

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
  • 5 replies
  • 3185 views
  • 7 likes
  • 4 in conversation