BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

I was given a folder full of .xpt files.  Is there code that can read in the XPT folder and convert them into SAS datasets (.sas7bdat)?  I have never dealt with XPT files before, so I don't have a clue where to start.  Here is a sample of the code that I have been attempting:

libname xptfile xport 'C:\XPT Datasets';

libname sasfile 'C:\SAS Datasets';

data sasfile.ca125cec;

   set xptfile.ca125cec;

run;

data sasfile.ca125cen;

   set xptfile.ca125cen;

run;

The problem with this is that the xptfile library contains nothing when read in.  The log says that the library was successfully created, but when I click on the library icon to open it, it gives me an error message: "ERROR, Invalid file, C:\XPT Datasets."  Therefore, when the data steps are executed, nothing goes in, and nothing goes out.

Any thoughts?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If the files a V5 transport files then you can point to them with a LIBNAME statement using the XPORT engine, but you have to point to the individual .XPT files, not the folder that contains them.

libname sasfile 'C:\SAS Datasets';

libname xptfile xport 'C:\XPT Datasets\ca125cec.xpt' access=readonly;

proc copy inlib=xptfile outlib=sasfile;

run;

libname xptfile xport 'C:\XPT Datasets\ca125cen.xpt' access=readonly;

proc copy inlib=xptfile outlib=sasfile;

run;


View solution in original post

14 REPLIES 14
Tom
Super User Tom
Super User

If the files a V5 transport files then you can point to them with a LIBNAME statement using the XPORT engine, but you have to point to the individual .XPT files, not the folder that contains them.

libname sasfile 'C:\SAS Datasets';

libname xptfile xport 'C:\XPT Datasets\ca125cec.xpt' access=readonly;

proc copy inlib=xptfile outlib=sasfile;

run;

libname xptfile xport 'C:\XPT Datasets\ca125cen.xpt' access=readonly;

proc copy inlib=xptfile outlib=sasfile;

run;


djbateman
Lapis Lazuli | Level 10

Tom,

Do you have a suggestion for running that code through a macro or a loop if I have dozens of .xpt files?  I usually do something like this:

proc sql;

      select count(distinct memname) into :numfiles from sashelp.vtable where libname='XPTFILE';

      %let numfiles=%sysfunc(compress(&numfiles.));

      select distinct memname into :file1-:file&numfiles. from sashelp.vtable where libname='XPTFILE';

quit;

Then I would run your code through a loop. However, this does not work because the xport option in the libname statement does not allow xptfile to appear in the vtable.  I don't understand XPT, so I don't know where to find a list of the files in that library.  Any thoughts?

Tom
Super User Tom
Super User

Get the operating system to tell you the names.  If your system doesn't allow that then you can use the DOPEN, DREAD functions.

%let path=C:\XPT Datasets;

filename xptfiles pipe "dir /b ""&path\%str(*).xpt"" " ;

filename code temp;

data files;

  infile xptfiles truncover;

  input filename $100. ;

  file code;

  put 'filename xptfile "&path\' filename +(-1) '" access=readonly;';

   put 'proc copy inlib=xptfile outlib=sasfile; run;' ;

run;


libname sasfile '....';

%inc code / source2 ;


paddyb
Quartz | Level 8

did u get solution with macro?

ajkalale
Obsidian | Level 7

Wow, What a simple solution! 

NO headache noe

j1914
Calcite | Level 5

Hi! How do you set the data name after you complete this step?

DrAbhijeetSafai
Lapis Lazuli | Level 10

Simply Amazing! Thanks!

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
JacquesCr
Calcite | Level 5

Hi djbateman, see my solution...

 

/* allocating output directories */
     libname sasfile 'J:\SDTM\c_DefineXML\Define-XML-2-0_RP20140424\sdtm\sas_output';

 

/* create filename 'xptprgs' to capture all *.xpt files */
     filename xptprgs pipe "dir /b J:\SDTM\c_DefineXML\Define-XML-2-0_RP20140424\sdtm\xpt_input\*.xpt";

 

/* create an extra variable 'xptprgs' to contain only the names of the xpt-files */
data work.xptprgs;
     length filepath $20;


   infile xptprgs length=reclen;

 

   input filepath $varying256. reclen ;

 

   xptprgs=reverse(substr(trim(left(reverse(filepath))),5));

 

run;

 

/* create macro variable 'xptprgs' to capture all individual xpt file names */
proc sql;
   select
          xptprgs

     into:

          xptprgs separated by ' '
     from
         work.xptprgs
;
quit;

%put &xptprgs;


/* loop through all individual files */
%macro xpt2sas;

 

     %do i=1 %to %sysfunc(countw(&xptprgs));

 

          libname xptfile xport "J:\SDTM\c_DefineXML\Define-XML-2-0_RP20140424\sdtm\xpt_input\%scan(&xptprgs, &i).xpt"      access=readonly;

 

          proc copy inlib=xptfile outlib=sasfile;
          run;


          %end;

     run;
%mend xpt2sas;

%xpt2sas;

 

Kr, 

Jacques

sdunn8898
Calcite | Level 5

THANK YOU!!! This is PERFECT and exactly what I needed.

Tommer
Obsidian | Level 7
This was so helpful! Solved my problem!
Peter_C
Rhodochrosite | Level 12
Why not just wrap this in a macro loop
libname xptlib xport "&path2xpt_file" access=readonly;
* clear receiving library (work here) ;
Proc datasets mt= data nolist kill lib= work ; quit;
* copy all tables in xpt to work lib ;
Proc copy in= xptlib out=work ; run ;
* deal with the tables in WORK;


Rajesh0136
Calcite | Level 5

I have the same issue copy and convert the XPTs. I am using the below code. 

 

libname sasfile 'C:\SAS Datasets';
libname xptfile xport 'C:\XPT Datasets\ca125cec.xpt' access=readonly;
proc copy inlib=xptfile outlib=sasfile;
run;

 

I am getting the below note. I was able to create the Libray with XPTs, Why the file is invalid.

 

NOTE: Input library XPTFILE is sequential.
ERROR: Invalid file, C:\XPT Datasets\ca125cec.xpt.
NOTE: Statements not processed because of errors noted above.
NOTE: PROCEDURE COPY used (Total process time):
real time 2.56 seconds
cpu time 2.38 seconds

NOTE: The SAS System stopped processing this step because of errors.

 

May I know how to avoid the above error and get the XPTs converted.

 

 

JacquesCr
Calcite | Level 5

 Hi Rajesh, 

I had a look at your code and did not find anything strange except that i changed the single quotes to double quotes and then it worked. 

 

libname xptfile xport "C:\XPT Datasets\ca125cec.xpt" access=readonly;

 

Else, I did not see anything peculiar.

 

Hope this helps.

 

Jacques

ritajhuang
Calcite | Level 5
Hi everyone,
 
I'm trying to read to .xpt data using the SAS studio (University Edition)
I'm not sure how should I write this code... Please kindly advise, thank you!
 
Note: My .xpt files locate at C:/SASUniversityEdition/myfolders/Bankdata
 
 
 
 
libname sasfile 'C:\folders\myfolders\Bankdata';
 
libname xptfile xport 'C:\XPT Datasets\CALL1103.xpt' access=readonly;
 
proc copy inlib=xptfile outlib=sasfile;
 
run;
 
libname xptfile xport 'C:\XPT Datasets\CALL1103.xpt' access=readonly;
 
proc copy inlib=xptfile outlib=sasfile;
 
run;

 

 

 

 

Thank you very much in advance!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 187917 views
  • 13 likes
  • 12 in conversation