BookmarkSubscribeRSS Feed
TomQuin
Calcite | Level 5
Does anybody know if there is a way in SAS coding to check a given
directory is writable or not ? I used Fopen( ) and Fwrite( ) functions, but they seem only work on a file not a directory.
Thanks.
3 REPLIES 3
deleted_user
Not applicable
The "writability" of a directory may be limited by a ReadOnly flag which is a file system attribute, or the permissions of the user, which is a security system attribute.

It is possible to use Windows APIs to retrieve security attributes, but the method requires a little work and may not work on all implementations of Enterprise Guide. I found my API calls transferred transparently, but I didn't expect it to be as easy since this is a client / server model and server implementations are often limited for very good reasons.

If you are able to create a file in a directory with the SCL functions you specified, then surely that means you can write to the directory, or have I missed something obvious? Can you not create a dummy file name, get a listing of files in the directory, verify the file does not exist, then create and open it to show whether you can or cannot write in the directory? It sounds like an eminently worthwhile candidate for a macro once you get the code right.

Kind regards

David
deleted_user
Not applicable
I once wrote a macro to prove if a path was "writeable". It would try writing to the path using FOPEN(), FWRITE() etcetera functions and test return codes ~but I can't find that macro now!

Anyway, that was before SAS9 introduced the DCREATE function.
This snip from online doc indicates a simpler approach is possible.

new-directory=DCREATE(directory-name<,parent-directory>)
Arguments
new-directory
contains the complete pathname of the new directory, or contains an empty string if the directory cannot be created.

full details at http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a002332693.htm.

If "new-directory" is not blank, then you can create a directory, and that folder is writeable. (Just make sure the "new-directory" does not exist before executing that function 😉 )


PeterC
deleted_user
Not applicable
That is a great function Peter.

On first execution:

14 Data _NULL_;
15 RC = DCreate( "Test1", "C:\Temp");
16 Put "Return code from DCreate is " RC;
17 Run;

Return code from DCreate is C:\Temp\Test1


On second execution:

14 Data _NULL_;
15 RC = DCreate( "Test1", "C:\Temp");
16 Put "Return code from DCreate is " RC;
17 Run;

Return code from DCreate is


So the existence of the directory doesn't seem to put the directory at risk of being replaced which is a robust solution. I especially like the return code which lends itself to being stored in a macro symbol for resolution into a path or file reference elsewhere in the code. I am especially thinking of the situation where the path is derived from some date or calculated parameter, and one need then only calculate it once.


All my path creations are done using a macro that invokes the Windows API CreateDirectoryA which has the benefit of return codes from the Windows OS. These are richer than the Go / NoGo result you get from the SAS SCL functions, including "Already exists" and "Path not found". It also permits defining a security descriptor if you want to. However, it does require some infrastructure.

Kind regards

David

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1802 views
  • 0 likes
  • 2 in conversation