Assuring Your Directory Exists - SESUG

[Pages:4]Fall 2008

The SESUG Informant

Volume 9, Issue 2

Assuring Your Directory Exists

Do you ever have a production job fail because the output directory does not exist? Either it needed to be set up and wasn't or someone accidently deleted it; or the path was just keyed incorrectly.

If it is a night-production job, wouldn't it be better if the output was written to another directory when this problem occurs? The log could reflect the anomaly and the files could be moved manually to the right location the next morning.

Ed Heaton has been programming in SAS since 1988 - professionally since 1993. He has presented papers at SUGI (now SAS Global Forum) and in-house, local, and regional SAS Users Groups since 2000. Ed acted as the academic Co-chair of the 2007 SESUG conference.

As a senior systems analyst at Westat, Ed teaches SAS in formal classes, aids other SAS programmers through the SAS Help Desk, writes papers for the Westat intranet, and develops corporate applications.

Here's a macro that will do just that. You can code something like...

LibName foo "%confirmedPath( knownPath=%sysGet(TEMP)

, desiredBranch=foo\child\grandchild)" ;

and your library reference will be created even if you recently ran a Windows cleanup routine that deleted these folders in your TEMP directory. Or, you can code...

LibName xlsLib "%confirmedPath( knownPath=\\server\project

, desiredBranch=Data.20080923)\myNewExcelFile.xls" ;

with assurance that your Excel file will be created even if the Data.20080923 folder was not created. Or your...

Ods pdf file="%confirmedPath( knownPath=..

, desiredBranch=Reports)\myReport.pdf" ;

will not bomb because it can't find the Reports folder.

If you want to be especially careful, you can add...

%local fatalError ; Ods pdf file="%confirmedPath(

knownPath=.. , desiredBranch=Reports )\myReport.pdf" ; %if &fatalError %then %do ; EndSas ;

%end ;

so that processing will end if you have a slash or a double-quote in the path or if the known path is not valid. The macro will set the value for the &fatalError macro variable.

Feel free to copy this macro and use it.

(Continued on page 2)

Fall 2008 Assuring Your Directory Exists

The SESUG Informant

Volume 9, Issue 2 Page 2

******************************************************************************* MACRO: %confirmedPath()

OBJECTIVE: This macro will return a path.

VALID: anywhere

USAGE: LibName foo "%confirmedPath( knownPath=%sysGet(TEMP) , desiredBranch=foo\child\grandchild )" ; LibName xlsLib "%confirmedPath( knownPath=\\server\project , desiredBranch=Data )\myNewExcelFile.xls" ; Ods pdf file=%confirmedPath( knownPath=.. , desiredBranch=Reports )\myReport.pdf" ;

PROGRAMMER:

Edward Heaton, SAS Senior Systems Analyst

SAS? Certified Advanced Programmer for SAS? 9

Westat (An Employee-Owned Research Corporation),

1650 Research Boulevard, Rockville, MD 20850-3195

Voice: (301) 610-4818

Fax: (301) 294-2085

mailto:EdHeaton@

PARAMETERS:

knownPath=

specifies a folder that is known to exist.

desiredBranch= specifies a branch from the above folder that might exist.

If it does not, this macro will create it.

OUTPUT: The fully qualified path of the desired folder.

STORAGE: This source code and its compiled macro are stored in \\server\volume\path\SasAutos\sasmacr.sas7bcat. A copy of the source code is kept in \\ server\volume\path\SasAutos. You can access macros in this catalog using something like... LibName macroCat "\\server\volume\path\SasAutos\" access=readOnly ; Options mStored sasMStore=macroCat mAutoSource ;

(Continued on page 3)

Fall 2008 Assuring Your Directory Exists

The SESUG Informant

Volume 9, Issue 2 Page 3

AUDIT TRAIL: 20080611 EH Developed macro to return a valid path, even if part of the tree does not already exist. It will create the missing parts.

*******************************************************************************/ %macro confirmedPath(

knownPath=. , desiredBranch= ) ;

/* If the &fatalError macro variable exists where this macro is called, it can be used to stop processing after returning from this macro. If it doesn't exist there, that's okay; &fatalError will be local to this macro and will have no effect. */ %let fatalError = 0 ;

/* Create macro variables to store W A R N I N G and E R R O R so that they do not show up in the code when we search the log for these problems. */ %local hmmm ; %let hmmm = WA ; %let hmmm = &hmmm.RNING ; %local ohNo ; %let ohNo = ER ; %let ohNo = &ohNo.ROR ;

%if not %sysFunc( fileExist( &knownPath ) ) %then %do ; %put &ohNo: Directory &knownPath\ cannot be found. ; %let fatalError = 1 ; %return ;

%end ;

%local i ; %let i = 1 ; %do %while ( %length( %scan( &desiredBranch , &i , \ ) ) ) ;

%if %sysFunc( fileExist( &knownPath\%scan( &desiredBranch , &i , \ ) ) ) %then %let knownPath = &knownPath\%scan(&desiredBranch,&i,\) ; %else %do ; /* If this folder does not exist, create it and add the new folder name to the path in the &knownPath macro variable. Then test for success. */ %local newPath ; %let newPath = %sysFunc( dCreate( %scan( &desiredBranch , &i , \ ) , &knownPath ) ) ; %if %length(&newPath) %then %do ; %put &hmmm: &newPath does not exist. ; %let knownPath = &newPath ; %put NOTE: &knownPath created. ; %end ; %else %do ;

(Continued on page 4)

Fall 2008 Assuring Your Directory Exists

The SESUG Informant

Volume 9, Issue 2 Page 4

%put &ohNo: &knownPath\%scan(&desiredBranch,&i,\). ; %put &ohNo- does not exist and could not be created. ; %let fatalError = 1 ; %return ; %end ; %end ; %let i = %eval( &i + 1 ) ; %end ;

&knownPath

%mEnd confirmedPath ; /******************************************************************************/

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download