BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JohnH
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Bago
SAS Employee

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

3 REPLIES 3
Bago
SAS Employee

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

JohnH
Fluorite | Level 6

Thanks....This explains it to me.

Peter_C
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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