Hey SAS-C,
I writing a code which usually takes URLs but occasionally a local file my pop up.
My question is: Is it possible to read local files with URL filename?
The code below produces errors:
filename in URL "file://localhost/C:/directory/test.txt"
debug recfm=N lrecl=1;
data _null_;
ex=fexist("in");
exTEXT = sysmsg();
put ex= / exTEXT = /;
run;
filename in;
The exTEXT returns: "ERROR: Physical file does not exist" even tough the file exists (pasting the same to chrome opens it).
Additionally the last line produce:
ERROR: At least one file associated with fileref IN is still in use.
ERROR: Error in the FILENAME statement.
All the best
Bart
If SAS is running on a server, that server won't be able to access your local computer.
If SAS is running on the server then the path would be:
filename in URL "file://localhost/home/&sysuserid./directory/test.txt"
debug recfm=N lrecl=1;
It doesn't matter if it is a laptop or a server, thing is about "to access a file on the machine by URL"
Bart
The documentation of the FILENAME URL statement lists only http and https access methods.
You will need to work around this with macro code that executes the proper statement for the filename which is delivered to your code.
True, it only mentions "http*", at least that part about the filename statement. I was hoping that maybe I missed some other place in the doc... but looks like there is no such place 😞
Bart
If the string literally starts with 'FILE://LOCALHOST/' then just adjust the string.
Example:
data have;
infile cards truncover;
input path $200.;
cards;
https://raw.githubusercontent.com/sasutils/macros/master/squote.sas
file://localhost/c:/downloads/squote.sas
;
data connections;
length fileref $8 engine $8 ;
set have;
fileref=cats('file',_n_);
if lowcase(path) in: ('http:','https:') then engine='url';
else if lowcase(path) =: 'file://localhost/' then do;
engine='disk';
path=substrn(path,18);
end;
else engine='disk';
call execute(catx(' ','filename',fileref,engine,quote(trim(path)),';'));
run;
data _null_;
infile file1 ;
input;
put _infile_;
run;
data _null_;
infile file2;
input;
put _infile_;
run;
1970 data connections; 1971 length fileref $8 engine $8 ; 1972 set have; 1973 fileref=cats('file',_n_); 1974 if lowcase(path) in: ('http:','https:') then engine='url'; 1975 else if lowcase(path) =: 'file://localhost/' then do; 1976 engine='disk'; 1977 path=substrn(path,18); 1978 end; 1979 else engine='disk'; 1980 call execute(catx(' 1980! ','filename',fileref,engine,quote(trim(path)),';')); 1981 run; NOTE: The data set WORK.CONNECTIONS has 2 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds NOTE: CALL EXECUTE generated line. 1 + filename file1 url "https://raw.githubusercontent.com/sasutils/macros/master/squote.sas" ; 2 + filename file2 disk "c:/downloads/squote.sas" ; 1982 1983 data _null_; 1984 infile file1 ; 1985 input; 1986 put _infile_; 1987 run; NOTE: The infile FILE1 is: (system-specific pathname), (system-specific file attributes) %macro squote(value); %if %sysevalf(&sysver < 9.3) %then %unquote(%str(%')%qsysfunc(tranwrd(%superq(value),%str(%'),''))%str(%')) ; %else %sysfunc(quote(%superq(value),%str(%'))) ; %mend squote; NOTE: 6 records were read from the infile (system-specific pathname). The minimum record length was 1. The maximum record length was 72. NOTE: DATA statement used (Total process time): real time 5.00 seconds cpu time 0.03 seconds 1988 1989 data _null_; 1990 infile file2; 1991 input; 1992 put _infile_; 1993 run; NOTE: The infile FILE2 is: (system-specific pathname), (system-specific file attributes) %macro squote(value); %if %sysevalf(&sysver < 9.3) %then %unquote(%str(%')%qsysfunc(tranwrd(%superq(value),%str(%'),''))%str(%')) ; %else %sysfunc(quote(%superq(value),%str(%'))) ; %mend squote; NOTE: 6 records were read from the infile (system-specific pathname). The minimum record length was 1. The maximum record length was 72. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
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.