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 ‘fileref’ to an external file.
>
> a. Using FILENAME global statement.
> b. Using FILENAME function.
>
> As per the V8 documentation for FILE statement, I can use
> ‘fileref’ generated by both FIELNAME function as well as
> FILENAME statement, in a FILE statement to refer to an external file.
>
> But I don’t know how to use the ‘fileref’ 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’t find the
> ‘fileref’ FNAME */
> put "something to the file:";
> run;
>
> ERROR:2
> -------
>
> data _null_;
> fname='fname';
> rc1=filename(fname,'sas.out1');
> file ‘fname’; /* ERROR: SAS thinks that
> ‘fname’ is the external
> filename, thus gives error */
> put "something to the file:";
> run;
>
> Can you clarify me how to use ‘fileref’ 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