I am using dopen and dread to look at all of the files and directories in a particular directory structure, but dopen is failing to open directories whose names contain embedded spaces.
I'm running SAS 9.3 on a unix platform, and have escaped the spaces using the backslash (\) character, so a a directory named 'older mapping docs' is referred to as 'older\ mapping\ docs'.
The filename function to assign a fileref to the directory gives a zero (successful) return code, but when I try to use dopen to open the directory, the DID (directory ID) returned is 0, meaning the operation failed.
Any suggestions for working around this problem?
You should be able to do this without needing to escape the space. This works correctly for me:
data _null_ ;
rc=filename('mydir', "$HOME/space dir") ;
put rc=;
did=dopen('mydir') ;
put did=;
run ;
LOG:
1 data _null_ ;
2 rc=filename('mydir', "$HOME/space dir") ;
3 put rc=;
4 did=dopen('mydir') ;
5 put did=;
6 run ;
rc=0
did=1
You should be able to do this without needing to escape the space. This works correctly for me:
data _null_ ;
rc=filename('mydir', "$HOME/space dir") ;
put rc=;
did=dopen('mydir') ;
put did=;
run ;
LOG:
1 data _null_ ;
2 rc=filename('mydir', "$HOME/space dir") ;
3 put rc=;
4 did=dopen('mydir') ;
5 put did=;
6 run ;
rc=0
did=1
I'm using %sysfunc to open and read the directories using macro code.
my initial code did not escape the spaces, but was not working.
I have just experimented with using %nrbquote to preserve the spaces in the macro variables, and that does seem to work - thanks!
LOG excerpt:
9 %let _dirpath=%nrbquote(/data/hdi/supptbls/wlp_edwardlz/0003_201105/medmgmt/older mapping docs);
10 %let rc=%sysfunc(filename(filrf2,%nrbquote(&_dirpath)));
11 %put rc=&rc;
rc=0
12 %let _did2=%sysfunc(dopen(&&filrf2));
13 %put _did2=&_did2;
_did2=2
I think it is because you are passing the wrong parameter value into the DOPEN call because of an extra &.
The FILENAME function call will either use the filename in the macro variable FILRF2 (or if it is empty it will make up a name and set FILRF2 to the new name). So in the call to DOPEN you should be passing the value of FILRF2.
%let dsid=%sysfunc(dopen(&FILRF2));
Sorry - that was a typo from simplifying my example. I had a double ampersand because the number at the end of the filrf variable name is being calculated, so the field reference was &&filrf&fnbr.
The log from the corrected test code is:
8
9 %let _dirpath=%nrbquote(/data/hdi/supptbls/wlp_edwardlz/0003_201105/medmgmt/older mapping docs);
10 %let rc=%sysfunc(filename(filrf2,%nrbquote(&_dirpath)));
11 %put rc=&rc;
rc=0
12 %let _did2=%sysfunc(dopen(&filrf2));
13 %put _did2=&_did2;
_did2=2
This is working - thanks again.
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.
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.