DATA Step, Macro, Functions and more

Filename Statement Issue

Accepted Solution Solved
Reply
Contributor
Posts: 73
Accepted Solution

Filename Statement Issue

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.


Accepted Solutions
Solution
‎05-04-2012 09:40 AM
SAS Employee
Posts: 7

Re: Filename Statement Issue

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

View solution in original post


All Replies
Solution
‎05-04-2012 09:40 AM
SAS Employee
Posts: 7

Re: Filename Statement Issue

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

Contributor
Posts: 73

Re: Filename Statement Issue

Thanks....This explains it to me.

Valued Guide
Posts: 2,177

Re: Filename Statement Issue

If you want to define the physical destination at runtime, look into the FILE statement option FILEVAR=

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 287 views
  • 3 likes
  • 3 in conversation