Can you explain the following code’s behavior:
FILENAME StatFile 'C:\TEMP\TestStatus.txt';
DATA _NULL_;
*rc = FILENAME( 'StatFile', 'C:\TEMP\TestStatus.txt');
FILE StatFile;
PUT "This is a test";
PUT "THIS IS LINE 2";
*FILENAME StatFile CLEAR;
RUN;
FILENAME StatFile CLEAR;
If I enable either FILENAME statement within the DATA step, and disable the corresponding FILNAME statement outside of the step, the FILE statement will not recognize the fileref. Instead, the FILE statement will use the string to create statfile.dat in the current working directory.
Hey,
The FILENAME function executes during DATA step execution, while the FILE statement opens the file before the DATA step begins execution.
If you use the FILENAME in the DATA step, the DATA step compiles both the FILENAME function and FILE statement. At this point, no files have been created. Just before the DATA step executes, the fileref StatFile is opened for output (created). The fileref StatFile doesn't exist, so a file named StatFile.dat is created.
When the DATA step executes, the FILENAME function executes and creates the fileref StatFile. At this point, it is too late to use this fileref with the FILE statement as the FILE statement has already done its work.
Hope this helps,
Bago
Hey,
The FILENAME function executes during DATA step execution, while the FILE statement opens the file before the DATA step begins execution.
If you use the FILENAME in the DATA step, the DATA step compiles both the FILENAME function and FILE statement. At this point, no files have been created. Just before the DATA step executes, the fileref StatFile is opened for output (created). The fileref StatFile doesn't exist, so a file named StatFile.dat is created.
When the DATA step executes, the FILENAME function executes and creates the fileref StatFile. At this point, it is too late to use this fileref with the FILE statement as the FILE statement has already done its work.
Hope this helps,
Bago
Thanks....This explains it to me.
If you want to define the physical destination at runtime, look into the FILE statement option FILEVAR=
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.