How to convert multiple sas datasets into xpt files by using data steps or proc steps.
The XPT file format is pretty old, with versions going back to SAS 5. Newer data sets can have characteristics that do not fit in the old format such as long variable names, longer than 8 characters, long variable labels, longer than 40 characters, long variable values, longer than 200 characters. Note that the XPT file can contain multiple data sets, are you sure that you need individual files?
Do you know which version of the XPT file that you want?
SAS does supply a macro that you provide the output file name, the source library and the list of sets to add to the XPT that can pick an appropriate file format based on contents.
You should be able to find the macro %LOC2XPT in your online documentation.
Or Proc Cport.
Be specific about what you mean by XPT file. The XPT filename extension is not specific to any particular format.
SAS has three main transport file formats.
The original SAS V5 transport format. (Note that only supports 8 character names and character variables up to 200 bytes long). For that you can use the XPORT libname engine.
The CPORT format generated by PROC CPORT and read by PROC CIMPORT.
There is also an updated version of the original transport format that supports longer variable names and longer character strings. To generate that you need to use the autocall macro %LOC2XPT(). And to read one back in your need to use the %XPT2LOC() macro.
/*****
As @ballardw pointed out,
there are two kind of XPT files.
V5 is old, V8 is new.
*****/
/**********For V5 version XPT files***********************/
libname in v9 'c:\temp\SAS' access=readonly; /*the path for sas datasets*/
%let path_out= c:\temp\XPT ; /*the path for XPT files*/
data _null_;
set sashelp.vtable(keep=libname memname where=(libname='IN'));
call execute(cat("libname tranfile xport '&path_out\",lowcase(strip(memname)),".xpt';proc copy in=in out=tranfile ; select ",strip(memname),";run;"));
run;
/**********For V8 version XPT files***********************/
libname in v9 'c:\temp\SAS' ; /*the path for sas datasets*/
%let path = c:\temp\XPT ; /*the path for XPT files*/
data _null_;
set sashelp.vtable(keep=libname memname where=(libname='IN'));
call execute(catt("filename z '&path.\",lowcase(memname),".xpt';",'%nrstr( %loc2xpt(libref=in,memlist=',lowcase(memname),',filespec=z))'));
run;
P.S. the XPT file generated by PROC CPORT is not real XPT file, the FDA would not accept it .
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.