|
The many uses of the filename statement |
|
|
|
|
SAS Tip #136
The filename statement allows us to do much more than just read and write files. By using various device types we can read and write to a range of places, including:
- DISK – this is the default allowing text & other formatted files
- DUMMY – files where nothing is kept
- TEMP - temporary files, which only exist till end of session
- PIPE - used to read the output from programs that are run
- PRINTER - used to send output directly to a printer
- CATALOG - used to read and write to SAS catalog members
- CLIPBOARD - used to access the host clipboard to exchange data with other applications
- EMAIL - allows emails to be sent
- FTP – reads/write to a file on an FTP site
- URL – reads from a web site
- SOCKET – read/write to a TCP socket
- SASXBAMW - Allows access to files on a WebDAV server
Code
* The following two filename statements are equivalent,
since DISK is the default filename engine ;
filename out disk 'c:\test.txt' ;
filename out 'c:\test.txt' ;
data _null_ ;
file out ;
set sashelp.class ;
put _all_ ;
run ;
* dummy file which is a "black hole" ;
filename rubbish dummy ;
data _null_ ;
file rubbish ;
set sashelp.class ;
put _all_ ;
run ;
* useful to allocate a temporary file ;
filename wrk temp ;
data _null_ ;
file wrk ;
put 'test' ;
run ;
data _null_ ;
infile wrk ;
input ;
put _infile_ ;
run;
* read output of a program from a pipe ;
filename cmd pipe 'dir c:' ;
data _null_ ;
infile cmd ;
input ;
put _infile_ ;
run ;
* Send output directly to default printer ;
filename p printer ;
data _null_ ;
file p ;
set sashelp.class ;
put _all_ ;
run ;
* Can read & write to catalog members of various types ;
filename cat catalog 'work.test.my.output' ;
data _null_ ;
file cat ;
set sashelp.class ;
put _all_ ;
run ;
* read & write to clipboard ;
filename clip clipbrd ;
data _null_ ;
file clip ;
put 'Some text to paste into somewhere.' ;
run ;
* I pasted the contents of clipboard into the following comment - note:
it also put in a carriage return ;
/*Some text to paste into somewhere.
*/
* Send emails ;
FILENAME report EMAIL 'phil @ woodstreet.org.uk' subject='test mail'
attach='c:\test.txt' ;
data _null_ ;
file report ;
put 'run ;
* read or write to FTP sites ;
filename host ftp 'README.txt' host='ftp.sas.com' cd='/techsup/download'
user='anonymous' debug ;
data _null_ ;
infile host ;
input ;
put _infile_ ;
run ;
* read from a web site ;
filename x url 'http://www.sas.com' ;
data _null_ ;
infile x ;
input ;
put _infile_ ;
run ;
* access TCP sockets - too complex to cover here ... ;
filename local socket ':5000' server reconn=3;
* access a file at a web site (example taken from documentation) ;
filename foo sasxbamw 'http://www.mycompany.com/production/files/rawFile.txt'
user='pass='jd75ld';
Tested under SAS 9.1.3 SP4 using Windows XP Professional
Wood Street Consultants Ltd. |
tips AT woodstreet.org.uk | www.woodstreet.org.uk
|
|
Last Updated ( Wednesday, 11 October 2006 )
|