Вы находитесь на странице: 1из 2

FTP File Access Method - Leave That File In Place

Jack Shoemaker, Thotwave Technologies

Abstract because we have instead included the LS option


that instructs SAS to retrieve a directory listing from
In today’s networked environment, it is common to the remote server, ftp.sas.com, in this example.
find that the data need for application A on server B
is often stored on server C. Transferring, or copying, FTP Filename Options
the data from server C to server B is one method to
solve this problem. If your application is a SAS The HOST= filename option specifies the name of
program reading data through a FILENAME the remote server. You can use either dotted-quad
statement, the FTP access method provides a IP format, or a host name as shown here. SAS will
means to run your application while leaving the transverse your TCP network to found the specified
original data in place. host, so use whatever works on your local machine.
This is particularly useful inside firewalls where a
Filename Syntax simple hostname alone without domain will often
work.
The basic filename statement takes the form
The USER= filename option specifies the name of
FILENAME myfile ‘/path1/path2/data.txt’; account to use on the remote server. Many sites
maintain anonymous FTP access. This means you
This allows you to read or write to “data.txt” in a data are free to log on as the user called anonymous. If
step using INPUT and PUT statements referencing the remote server requires some other account, you
MYFILE. More generally, the FILENAME statement supply that name instead.
allows you to attach your SAS program to a data
stream with alias MYFILE. In this case that data By convention, you use your email address as your
stream is just a file. password when doing anonymous log on. The
PASS= filename option is used to specify the
The FTP access method allows you to use the password for the account specified in USER=.
FILENAME statement to alias a direct connection to
an FTP channel. You instruct SAS to create an FTP The LS option is one of the special filename options
connection by specifying the FTP file access method that cause FTP to do something other than just read
directly after your file-name token and before the file a file. Another important filename option is CD=
specification. You also need to include a few FTP- which changes the current directory on the remote
specific FILENAME options as well. Consider the server. Consider the following example.
following example.
filename x ftp 'BackB-KW-1997-14.dat'
filename x ftp '' cd = 'pub'
ls user = 'anonymous'
user = 'anonymous' pass = 'shoe@std.com'
pass = 'shoe@std.com' host = 'ftp.sas.com'
host = 'ftp.sas.com' ;
;
data _null_;
data _null_; infile x;
infile x; input;
input; put _infile_;
put _infile_; if _n_ = 5 then stop;
run; run;

The data step (_NULL_) reads from X as defined in In this case we wish to read a data file in the pub
the filename statement. The special data-step directory located on the SAS public FTP site. The
variable INFILE contains the contents of the last line example writes the first 5 lines to the SAS log. The
read by INPUT. So, the PUT command simply results are shown on the next page.
writes the contents of X to the SAS log.

But X is not just a file in this case. In fact, the file


specification is empty in this example. That’s
NOTE: 220 ewe3.sas.com FTP server (Version fashion using SAS in conjunction with the FTP
wu-2.5.0(1) Wed Aug 18 14:14:59 EDT 1999) access method. These short examples should run
NOTE: User anonymous has connected to FTP.
NOTE: The infile X is: as-is from your own local machines to get you
Filename=BackB-KW-1997-14.dat, started.
Pathname= "/pub" is current directory,
Local Host Name=hp-vectra, Contact Information
Local Host IP addr=216.215.128.64,
Service Hostname Name=ewe3.sas.com,
Service IP addr=149.173.1.16,Service For more information contact:
Name=FTP,
Service Portno=21,Lrecl=256,Recfm=Variable
Jack Shoemaker
Thotwave Technologies
--------------- ----- ---- ---- ---- ---- Suite 202
Agent: fddiprobe Domain: ALL.2 History 117 Edinburgh South
Apr 2 08:45:29 4.29 922M 3M 10K 28K
Apr 2 09:15:29 5.51 1G 7M 8K 27K Cary, NC 27511
NOTE: 5 records were read from the infile Email: shoe@std.com
X.

Note that the file contains three header lines – a


blank line, a line with dashes, and a line with column
names. We need to perform some standard data-
step programming to properly parse this file. Once
you have referenced the file through the FTP access
method, you can process it just as if it were a local
disk file. For example:

filename x ftp 'BackB-KW-1997-14.dat'


cd = 'pub'
user = 'anonymous'
pass = 'shoe@std.com'
host = 'ftp.sas.com'
;

data info;
infile x firstobs = 4;
format
tm time8.
;
input
@1 mmm $3. @;
if mmm ^= 'Per' then do;
input
@5 dd 2.
@8 tm time8.
@17 dur 6.
;
output;
end;
run;

proc print data = info( obs = 10 );


run;

Conclusion

The FTP access method is a neat and concise


extension to the familiar FILENAME statement. It
allows you to write any number of network-based
applications by providing programmatic access to
the FTP channel. Any task you can accomplish at
the bare FTP> prompt may be done in similar

Вам также может понравиться