DATA Step, Macro, Functions and more

dopen to open directory name that contains spaces

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

dopen to open directory name that contains spaces

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?


Accepted Solutions
Solution
‎05-31-2012 10:24 AM
SAS Employee
Posts: 25

Re: dopen to open directory name that contains spaces

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

View solution in original post


All Replies
Solution
‎05-31-2012 10:24 AM
SAS Employee
Posts: 25

Re: dopen to open directory name that contains spaces

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

Occasional Contributor
Posts: 6

Re: dopen to open directory name that contains spaces

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

Super User
Super User
Posts: 7,080

Re: dopen to open directory name that contains spaces

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));

Occasional Contributor
Posts: 6

Re: dopen to open directory name that contains spaces

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.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 833 views
  • 0 likes
  • 3 in conversation