Yes, SAS Can Do! Manage External Files with SAS Programming
Total Page:16
File Type:pdf, Size:1020Kb
Yes, SAS® Can Do! --- Manage External Files With SAS Programming (Paper 3262 - 2015) Justin Jia, TransUnion Canada Amanda Lin, CIBC, Canada April 26-29, 2015 Dallas, Texas, USA Overview . Introduction . I. System Statements and Shell Commands X command and statement / %Sysexec macro statement/ Systask command . II. SAS Piping Functionality . III. SAS Call System Routine and System Function . IV. New SAS File Management Functions DCREATE function/ RENAME function/ FDELETE function/DLCREATEDIR option . V. Application Examples . Conclusion 2 Introduction . A good file management system is important for efficient documentation and project management. It would be beneficial to automate and standardize repetitive routine tasks via SAS programming: create, rename, move, copy, delete. Proc DATASETS, though powerful, is only effective for managing SAS files. How can we manage non-SAS external files or directories? 3 I. System Statements and Shell Commands . X <'command '> ; X " mkdir "&path.\ WLMU 999" "; . %Sysexec macro statement %sysexec mkdir "&path.\WLMU 999" ; . Systask statement systask command %unquote(%str(%') mkdir "&path.\WLMU 999" %str(%')); 4 Pros / Cons of System Statements/Commands . Advantages Not easy to monitor the execution status of submitted commands They use simple and easy DOS or (no return code). Unix commands. They cannot perform conditional Able to perform all kinds of file management tasks (create, execution since they are global. rename, copy, move, delete). data _null_; They are global commands and Action= 0; can exist anywhere in a SAS if Action=1 then do; program. X "mkdir "&path.\Try“ " ; . Disadvantages end; run; The X command will be executed every They prompt annoying flashing DOS window. time and the new directory Try will be created regardless of the value of Action. 5 II. SAS Piping Functionality . Piping is a channel of parallel communications External between SAS and other SAS Piping external applications. Program . The issued command will be directed to external programs, and the execution results will feed Unnamed Piping back to SAS via piping. FILENAME fileref PIPE . We can use SAS Filename statement to ‘program name /command ’ invoke unnamed piping. <options> ; 6 Manage External Files via SAS Piping Error messages will write to SAS log for unsuccessful ***** Create a new directory.*****; executions. For example, if the directory already exists: 16 Filename Create PIPE "mkdir Filename Create PIPE &path.\By_Pipe "; "mkdir &path.\By_Pipe"; 23 data Create ; 24 infile Create; 25 input ; data _null_ ; 26 put _infile_; 27 run; infile Create; NOTE: The infile CREATE is: input ; Unnamed Pipe Access Device, put _infile_; *Write the PROCESS=mkdir D:\WLMU2015\Projects\By_Pipe, RECFM=V, LRECL=256 execution results to SAS Stderr output: log. A subdirectory or file D:\WLMU2015\Projects\By_Pipe already exists. run; NOTE: 0 records were read from the infile CREATE. NOTE: The data set WORK.CREATE has 0 observations and 0 variables. 7 III. SAS Call Routine and System Function . Call System Routine data Create; Name="pid1280"; CALL SYSTEM (command); NewDir= "&path.\"||strip(Name); Check= fileexist(NewDir); Command=character string/ if Check=0 then do; expression or a character variable. command="mkdir "||'"'||NewDir||'"'; call system(command); It is a local command rather than a RC=symget("SYSRC"); global one, it must be used in a /*RC= &SYSRC;*/ Not working! DATA step. /*RC=system(command);*/ if RC="0" then put "Note: Directories It is a callable routine, allowing Created Successfully: " NewDir; conditional execution. else put "Note: Directories Cannot Be Created: " NewDir; . System Function end; It issues an operating system command for execution during a else if Check=1 then put "Note: SAS session. Directory Already Exists: " NewDir; Run; 8 IV. New SAS File Management Functions DCREATE Function: create a new directory. From SAS 9.2, some new Syntax: DCREATE (directory-name, <parent- functions are introduced directory>) ; for file management uses. data _Null_; . They can work on both SAS pid="PID9999"; files and non-SAS external NewDir="&path.\"||strip(pid); files. Check=fileexist(NewDir); if Check=0 then do; . They are true SAS Created=dcreate(strip(pid),"&path."); functions with simple end; syntax, easy to use. else if Check=1 then put "Note: Directory Already Exists: " NewDir; . Local and callable run; commands allowing *Note: If successful, the DCREATE function conditional execution. returns a complete pathname of a new external directory; a blank text string for unsuccessful execution. 9 RENAME Function . A new function since SAS RENAME Function: rename a SAS file, 9.2. external file or directory. Powerful and capable of working on both SAS files Syntax: and external files, RENAME(old-name, new-name , < type>); directories. Easy and convenient to use. Old-Name: the current name of a file or directory. It supports library reference use. It returns a return code of New-Name: the new name of a file or zero for successful execution, and non-zero directory. value for unsuccessful Type: specifies the element type, the possible execution. values are DATA, VIEW, CATALOG, ACCESS, FILE. 10 Rename files and directories libname Test"&path.\Test“; *define a libref. data _null_; /***rename a SAS data set using libref.;***/ RC1=rename("Test.AAA", "BBB", "data"); /*rename a SAS data set using the full pathname.;*/ RC2=rename("&path.\Test\AAA.sas7bdat", "&path.\Test\BBB.sas7bdat", "file"); /***rename an external file.;/ RC3=rename("&path.\Test\Sales.pdf", "&path.\Test\2012 Sales.pdf", "file"); /***rename a Windows directory.;/ RC4=rename("&path.\Test", "&path.\New_Test", "file"); /***rename a Unix directory.;/ RC5=rename("/&path./Test", "/&path./New_Test", "file"); Run; 11 Use Rename Function to move and delete files. RENAME(old-name, new-name, data _null_; <type>); /**move an external file to a different directory and change its name.; */ We can use RENAME function RC3=rename("&path.\Test\Sales.pdf", to move and delete files if we "&path.\New Test\2012 Sales.pdf", "file"); specify the New Name with a /***move a directory along with its contents to a different directory, and path different from that of the also change its name. ;*/ Old Name. RC6=rename("&path.\Test\AAA", "&path.\New Test\BBB", "file"); This is a creative use of /***delete an external file by moving it RENAME function discovered to a specified Trash directory.;*/ by the paper authors. RC4=rename("&path.\Test\Sales.pdf", "&path.\Trash\Sales.pdf", "file"); Run; 12 FDELETE Function and DLCREATEDIR Option Syntax:FDELETE(fileref|directory); New global system option in SAS 9.3. When put in effect, the A new function since SAS 9.2, it can LIBNAME can automatically create a permanently delete SAS or non-SAS files new directory and then assign the and empty directories, must use a fileref libref, if it does NOT exist. rather than a physical name. The default NODLCREATEDIR It returns 0 for a successful operation and system option can cancel this effect. other value for an unsuccessful operation. options dlcreatedir; filename DEL "&path.\Test\Sales.pdf"; libname AAA "&path.\AAA"; data _Null_; NOTE: Library AAA was created. RC=fdelete("DEL"); NOTE: Libref AAA was successfully if RC=0 then put "Note: Files deleted assigned as follows: successfully. "; Engine: V9 else put "Files cannot be deleted.“; Physical Name: Run; D:\WLMU2015\Projects\AAA. 13 Summary of Advantages and Disadvantages Advantages Disadvantages I. Systems 1. They use simple and easy DOS, Unix or other OS commands. 1. They prompt annoying flashing DOS window. Statements and 2. They are global commands and can exist anywhere in a SAS program. 2. It is not easy to monitor the execution status of the Shell Commands 3. They are able to perform all kinds of file management tasks (create, submitted command (no return code). rename, copy, move, delete). 3. They cannot perform conditional execution since they are global. II. SAS Piping 1. It uses simple DOS, Unix or other OS commands, no fleeting DOS 1. It requires more SAS coding than other methods. Functionality window. 2. It cannot perform conditional execution since it is 2. We can look into SAS log to monitor the execution status. global. 3. It is able to perform all kinds of file management tasks (create, rename, copy, move, delete). III. SAS Call 1. They use simple DOS, Unix or other OS commands. 1. They are local and must exist in a SAS DATA step. Routine and 2. We can look into the return code to monitor the execution status. 2. They trigger the annoying fleeting DOS window. System Function 3. They are able to perform all kinds of file management tasks (create, rename, copy, move, delete). 4. They are local and callable commands, allowing conditional execution. IV. New SAS File 1. They are true SAS functions with simple syntax, easy to use. 1. They are local and must exist in a SAS DATA step. Management 2. No fleeting DOW window. 2. They cannot perform all the file management tasks. Functions 3. They can work on both SAS files and non-SAS external files. 3. Only few functions are available for use in SAS. 4. They are local and callable, allowing conditional execution. 5. We can look into the return code to monitor the execution status. 14 V. Application Example I: Create Routine Folder Structure For each project, we need create below %macro Create(path=, pid=); folder structure. options mprint symbolgen ; Filename List "physical path\List.xls"; Below Excel file controls the conditional if Create="Y" then do; procNewDir=Project_Folder||" import