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
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.
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.
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
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
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;
@RichardDeVen's method is the "trick" I was referring to in my PS.
Dear KurtBremser, Ksharp and RichardADeVenezia,
Thank you all for your helpful solutions.
Michael
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.