- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@RichardDeVen's method is the "trick" I was referring to in my PS.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear KurtBremser, Ksharp and RichardADeVenezia,
Thank you all for your helpful solutions.
Michael