The sample code on the Full Code tab can be used to dynamically convert transport files of XPORT type into SAS data sets. The macro loops through the files in the directory and runs PROC COPY for each transport file it finds. By using this sample macro, the need to write individual LIBNAME statements for each .xpt file is eliminated.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
To run the below code successfully, you must have or create a folder named temp that resides on the c:\ drive (else the error appears, ERROR: A character operand was found. The condition was: &memcnt).
Note: For another macro to list all fiiles in a directory, see Sample 24696.
/* The code below creates a transport file in the temp folder for use by the macro */
libname testlib xport 'c:\temp\trans.xpt';
proc copy in=sashelp out=testlib;
select retail;
run;
/* Note: Macro starts here */
/* Macro using PROC COPY and the XPORT engine for reading transport files*/
%macro drive(dir,ext,out);
%let filrf=mydir;
/* Assigns the fileref of mydir to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
/* Returns the number of members in the directory */
%let memcnt=%sysfunc(dnum(&did));
/* Loops through entire directory */
%do i = 1 %to &memcnt;
/* Returns the extension from each file */
%let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);
/* Checks to see if file contains an extension */
%if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&ext) %then %do;
/* Checks to see if the extension matches the parameter value */
/* If condition is true, submit PROC COPY statement */
%if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or
(%superq(ext) = and %superq(name) ne) %then %do;
libname old xport "&dir.\%qsysfunc(dread(&did,&i))";
libname new "&out";
proc copy in=old out=new;
run;
%end;
%end;
%end;
/* Close the directory */
%let rc=%sysfunc(dclose(&did));
/* END MACRO */
%mend drive;
/* Macro call */
/*First parameter is the source folder, the second parameter is extension being */
/*searched for, and the third parameter is the target directory for the */
/*converted files. */
%drive(c:\temp,xpt,c:\temp\)
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
1401 %drive(c:\temp,xpt,c:\temp\) NOTE: Libref OLD was successfully assigned as follows: Engine: XPORT Physical Name: c:\temp\trans.xpt NOTE: Libref NEW was successfully assigned as follows: Engine: V9 Physical Name: c:\temp NOTE: Input library OLD is sequential. NOTE: Copying OLD.RETAIL to NEW.RETAIL (memtype=DATA). NOTE: BUFSIZE is not cloned when copying across different engines. System Option for BUFSIZE was used. NOTE: There were 58 observations read from the data set OLD.RETAIL. NOTE: The data set NEW.RETAIL has 58 observations and 5 variables. NOTE: PROCEDURE COPY used (Total process time): real time 0.02 seconds cpu time 0.01 seconds
Type: | Sample |
Date Modified: | 2008-12-12 10:10:36 |
Date Created: | 2008-11-13 13:41:05 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Windows Millennium Edition (Me) | ||
Microsoft Windows XP Professional | 9.2 TS2M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.2 TS2M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS2M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS2M0 | |||
Microsoft Windows NT Workstation | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 95/98 | ||||
Microsoft® Windows® for x64 | 9.2 TS2M0 | |||
Microsoft Windows XP 64-bit Edition | 9.2 TS2M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS2M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS2M0 | |||
OS/2 | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS2M0 | |||
Windows Vista | 9.2 TS2M0 |