LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (June 2004, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 22 Jun 2004 09:01:22 -0400
Reply-To:     "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject:      Re: Question Regd. FILENAME() function and FILE statement.

kanthan wrote: > Hi, > > I have a question to ask SAS-L. I know that there are two ways to > create &#8216;fileref&#8217; to an external file. > > a. Using FILENAME global statement. > b. Using FILENAME function. > > As per the V8 documentation for FILE statement, I can use > &#8216;fileref&#8217; generated by both FIELNAME function as well as > FILENAME statement, in a FILE statement to refer to an external file. > > But I don&#8217;t know how to use the &#8216;fileref&#8217; generated > by a FILENAME function in a FILE statement. I am a bit confused in > this that I am not able to use it. > > Error:1 > ------- > > data _null_; > fname='fname'; > rc1=filename(fname,'sas.out1'); > file fname; /* ERROR: SAS doesn&#8217;t find the > &#8216;fileref&#8217; FNAME */ > put "something to the file:"; > run; > > ERROR:2 > ------- > > data _null_; > fname='fname'; > rc1=filename(fname,'sas.out1'); > file &#8216;fname&#8217;; /* ERROR: SAS thinks that > &#8216;fname&#8217; is the external > filename, thus gives error */ > put "something to the file:"; > run; > > Can you clarify me how to use &#8216;fileref&#8217; generated by a > FILENAME function in a FILE statement? >

You should have noticed that when a fileref does not exist, the thing after the FILE statement is presumed to name a .dat file in some default output location.

225 data _null_; 226 file foo; 227 run;

NOTE: The file FOO is: File Name=C:\Documents and Settings\Rich\sas91\foo.dat, RECFM=V,LRECL=256

The fileref of the FILE statement must exist prior to the DATA Step. When the fileref does exist prior to the DATA Step, changing it during the data step will not change where the DATA Step writes. Why does it not change ? Because the running data step already has the fileref open, preventing filename() statement from reassigning it.

i.e. This writes to file dummy1.txt ------ 163 filename x 'c:\temp\dummy1.txt'; 164 165 data _null_; 166 path = pathname ('y'); put path=; 167 rc = filename ('y', 'c:\temp\dummy2.txt'); 168 path = pathname ('y'); put rc= path=; 169 170 path = pathname ('x'); put path=; 171 rc = filename ('x', 'c:\temp\dummy2.txt'); 172 path = pathname ('x'); put rc= path=; 173 174 175 file x; 176 put 'x'; 177 rc = filename ('x',''); file log; put rc=; 178 stop; 179 run;

NOTE: The file X is: File Name=c:\temp\dummy1.txt, RECFM=V,LRECL=256

path=c:\temp\dummy2.txt rc=0 path=c:\temp\dummy2.txt path=c:\temp\dummy1.txt rc=20036 path=c:\temp\dummy1.txt rc=20036 NOTE: 1 record was written to the file X. The minimum record length was 1. The maximum record length was 1. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds

180 181 %put %sysfunc(pathname(x)); c:\temp\dummy1.txt 182 %put %sysfunc(pathname(y)); c:\temp\dummy2.txt ------

From !SASROOT\core\sasmacro\sysrc.sas %let _SEFOPEN = 20036; /* file is currently open */

A fileref created during a DATA Step can be used in other file functions such as FILENAME() or FILEREF(). Be sure to check and deal with the FILENAME() return code.

In you want to dynamically change the file written to by a FILE statement, use the FILEVAR= option. The value of the variable named in the filevar= option is the location where writing will occur.

fname='sas.out1'; file <arbitrary_fileref> filevar=fname; put 'goes to sas out1'; file log; put 'goes to log';

most commonly, arbitrary_fileref is DUMMY

V9 tip: You can unconditionally put to the log window using the PUTLOG statement.

-- Richard A. DeVenezia http://www.devenezia.com/downloads/sas/samples


Back to: Top of message | Previous page | Main SAS-L page