BookmarkSubscribeRSS Feed
sasuser143
Calcite | Level 5

How to convert multiple sas datasets into xpt files by using data steps or proc steps.

3 REPLIES 3
ballardw
Super User

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.

Tom
Super User Tom
Super User

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.

Ksharp
Super User
/*****
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 .

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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