Valid in: | DATA Step |
Category: | File-handling |
Type: | Executable |
Operating environment: | The INFILE statement contains operating environment-specific material. See the SAS documentation for your operating environment before using this statement. |
See: | INFILE Statement under Windows, UNIX, and z/OS |
You must have previously associated the fileref with an external file in a FILENAME statement, a FILENAME function, or an appropriate operating environment command.
infile dummy ftp user='myuid' pass='xxxx' filevar=file_to_read;
If you specify RECFM=N, make sure that the LRECL is large enough to hold the largest input item. Otherwise, it might be possible for the delimiter to be split across the record boundary.
You can specify either I, T, or both.
When using FILEVAR=, it is not possible to know whether the input file that is currently open is the last file or not. When the DATA step comes to an end-of-file marker or the end of all open data sets, it performs an orderly shutdown. In addition, if you use FILEVAR with FIRSTOBS, a file with only a header record in a series of files will trigger a normal shutdown of the DATA step. The shutdown occurs because SAS reads beyond the end-of-file marker and the DATA step terminates. You can use the EOF= option to avoid the shutdown.
infile file-specification firstobs=50 obs=100;
infile file-specification linesize=72;
infile 'external file' n=5;
input #2 name : $25. #3 job : $25. #5;
The INPUT statement includes a #5 pointer control, even though no data is read from that record.
record-number | specifies the record number of the last record to read in an input file that is read sequentially. |
MAX | specifies the maximum number of observations to process, which will be at least as large as the largest signed, 32–bit integer. The absolute maximum depends on your host operating environment. |
infile file-specification obs=100;
Use SHAREBUFFERS to update specific fields in an external file instead of an entire record.
To access the contents of the input buffer in another statement without using the _INFILE_= option, use the automatic variable _INFILE_.
The _INFILE_ variable does not have a fixed width. When you assign a value to the _INFILE_ variable, the length of the variable changes to the length of the value that is assigned.
put _infile_ $hex100.;
data scores; infile datalines delimiter=','; input test1 test2 test3; datalines; 91,87,95 97,,92 ,1,1 ;
data scores; input test1 test2 test3; datalines; 91,87,95 97,,92 ,1,1 ;
infile datalines dsd;
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
----+----1----+----2 1 22 333 4444 55555
infile 'external-file' truncover;
data num; infile datalines dsd; input x y z; datalines; ,2,3 4,5,6 7,8,9 ;
2
is assigned to variable
Y.
a
and b
function
as delimiters: data nums; infile datalines dsd delimiter='ab'; input X Y Z; datalines; 1aa2ab3 4b5bab6 7a8b9 ; proc print; run;
PRD
is
used as the delimiter. Note that the string contains uppercase characters.
By using the DLMSOPT= option, PRD
, Prd
, PRd
, PrD
, pRd
, pRD
, prD
,
and prd
are all valid delimiters.data test; infile datalines dsd dlmstr='PRD' dlmsopt='i'; input X Y Z; datalines; 1PRD2PRd3 4PrD5Prd6 7pRd8pRD9 ; proc print data=test; run;
data scores; infile datalines dsd; input Name : $9. Score Team : $25. Div $; datalines; Joseph,76,"Red Racers, Washington",AAA Mitchel,82,"Blue Bunnies, Richmond",AAA Sue Ellen,74,"Green Gazelles, Atlanta",AA ; proc print; run;
data weather; infile datalines missover; input temp1-temp5; datalines; 97.9 98.1 98.3 98.6 99.2 99.1 98.5 97.5 96.2 97.3 98.3 97.6 96.5 ;
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
infile datalines stopover;Because SAS does not find a TEMP4 value in the first data record, it sets _ERROR_ to 1, stops building the data set, and prints data line 1.
filename phonebk host-specific-path;
data _null_;
file phonebk;
input line $80.;
put line;
datalines;
Jenny's Phone Book
Jim Johanson phone: 619-555-9340
Jim wants a scarf for the holidays.
Jane Jovalley phone: (213) 555-4820
Jane started growing cabbage in her garden.
Her dog's name is Juniper.
J.R. Hauptman phone: (49)12 34-56 78-90
J.R. is my brother.
;
run;
data a;
infile file-specification length=linelen lrecl=510 pad;
input firstvar 1-10 @; /* assign LINELEN */
varlen=linelen-10; /* Calculate VARLEN */
input @11 secondvar $varying500. varlen;
run;
data qtrtot(drop=jansale febsale marsale aprsale maysale junsale); /* identify location of 1st file */ infile file-specification-1; /* read values from 1st file */ input name $ jansale febsale marsale; qtr1tot=sum(jansale,febsale,marsale); /* identify location of 2nd file */ infile file-specification-2; /* read values from 2nd file */ input @7 aprsale maysale junsale; qtr2tot=sum(aprsale,maysale,junsale); run;
data allsales; length fileloc myinfile $ 300; input fileloc $ ; /* read instream data */ /* The INFILE statement closes the current file and opens a new one if FILELOC changes value when INFILE executes */ infile file-specification filevar=fileloc filename=myinfile end=done; /* DONE set to 1 when last input record read */ do while(not done); /* Read all input records from the currently */ /* opened input file, write to ALLSALES */ input name $ jansale febsale marsale; output; end; put 'Finished reading ' myinfile=; datalines; external-file-1 external-file-2 external-file-3 ;
data _null_; /* The INFILE and FILE statements */ /* must specify the same file. */ infile file-specification-1 sharebuffers; file file-specification-1; input state $ 1-2 phone $ 5-16; /* Replace area code for NC exchanges */ if state= 'NC' and substr(phone,5,3)='333' then phone='910-'||substr(phone,5,8); put phone 5-16; run;
data _null_; infile file-specification-1 length=a; input; a=a-20; file file-specification-2; put _infile_; run;
data _null_; infile file-specification start=s; input; s=11; file file-specification-2; put _infile_; run;
filename phonbill host-specific-filename;
data _null_;
file phonbill;
input line $80.;
put line;
datalines;
City Number Minutes Charge
Jackson 415-555-2384 <25> <2.45>
Jefferson 813-555-2356 <15> <1.62>
Joliet 913-555-3223 <65> <10.32>
;
run;
data _null_; infile phonbill firstobs=2; input; city = scan(_infile_, 1, ' '); char_min = scan(_infile_, 3, ' '); char_min = substr(char_min, 2, length(char_min)-2); minutes = input(char_min, BEST12.); put city= minutes=; run;
City Number Minutes Charge Jackson 415-555-2384 <25> <2.45> Jefferson 813-555-2356 <15> <1.62> Joliet 913-555-3223 <65> <10.32>
data _null_; do i = 1 to 3; fname= 'external-data-file' || put(i,1.) || '.dat'; file datfiles filevar=fname; do j = 1 to 5; put i j; end; file 'external-filenames-file'; put fname; end; run; data _null_; infile 'external-filenames-file' _infile_=fname; input; infile datfiles filevar=fname end=eof; do while(^eof); input; put fname _infile_; end; run;
NOTE: The infile 'external-filenames-file' is: File Name=external-filenames-file, RECFM=V, LRECL=256 NOTE: The infile DATFILES is: File Name=external-data-file1.dat, RECFM=V, LRECL=256 external-data-file1.dat 1 1 external-data-file1.dat 1 2 external-data-file1.dat 1 3 external-data-file1.dat 1 4 external-data-file1.dat 1 5 NOTE: The infile DATFILES is File Name=external-data-file2.dat, RECFM=V, LRECL=256 external-data-file2.dat 2 1 external-data-file2.dat 2 2 external-data-file2.dat 2 3 external-data-file2.dat 2 4 external-data-file2.dat 2 5 NOTE: The infile DATFILES is File Name=external-data-file3.dat, RECFM=V, LRECL=256 external-data-file3.dat 3 1 external-data-file3.dat 3 2 external-data-file3.dat 3 3 external-data-file3.dat 3 4 external-data-file3.dat 3 5