BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
r3570
Obsidian | Level 7

Hi All,

 

I have 26 datasets in a folder and i wanted to convert all of them into XPT at one go.

 

Can any one help me with a macro ?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@r3570 wrote:
Hi Tom,

I was able to create a single xpt file which included all the datasets in
it using proc copy.

Because it is hectic task to write 26 programs for each dataset to convert
into xpt, I need a macro which creates xpt file separately for each dataset
in one go.

If you can place all the data sets into one file what is the advantage of creating 26 files?

View solution in original post

8 REPLIES 8
maguiremq
SAS Super FREQ

Have you tried using the XPORT engine in the LIBNAME statement? Here's some documentation:

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/movefile/p07no93eh1e7oun1fmp0anvi7k3k.htm

 

I'm assuming you would know how to make a macro out of something like this. There's also this article, but I didn't give it much of a read. There's a section specifically dedicated toward a macro.

 

https://www.lexjansen.com/pharmasug/2004/FDACompliance/FC07.pdf

r3570
Obsidian | Level 7

Yes i know hoe to convert a single dataset in xpt file using xport in libname statement. but not able to figure out how to convert all the datasets at a time instead of doing it one by one.

Ksharp
Super User
/*************一个SAS数据集 转成 一个XPT文件************/
/* path to store xpt file*/
%let path_out= D:\XiaKeShan\my_code\数据管理\转XPT文件\XPT  ;
/* path to address sas dataset*/
%let path_in= D:\XiaKeShan\my_code\数据管理\转XPT文件\SAS  ;


/*transform a sas file into a xpt dataset*/
libname in v9 "&path_in";
data _null_;
rc=filename('x',"&path_in");
did=dopen('x');
do i=1 to dnum(did);
  memname=dread(did,i);
call execute(cat("libname tranfile xport '&path_out\",scan(memname,1,'.'),".xpt';proc copy in=in out=tranfile;select ",scan(memname,1,'.'),";run;"));
end;

run;

Here is V5 version XPT file.

Ksharp
Super User

Here is V8 version XPT file .

 

/*************一个SAS数据集 转成 一个XPT文件************/
/* path to store xpt file*/
%let path_out= D:\XiaKeShan\my_code\数据管理\转XPT文件\XPT  ;
/* path to address sas dataset*/
%let path_in= D:\XiaKeShan\my_code\数据管理\转XPT文件\SAS  ;


/*transform a sas file into a xpt dataset*/
libname in v9 "&path_in";
data _null_;
rc=filename('x',"&path_in");
did=dopen('x');
do i=1 to dnum(did);
  memname=dread(did,i);
call execute(cas("%loc2xpt(libref=in,memlist=",scan(memname,1,'.'),",filespec=&path_out\",scan(memname,1,'.'),".xpt)"));
end;

run;
Tom
Super User Tom
Super User

Do you want one XPT file with all 26 datasets in it? 

Or a separate XPT file for each dataset?

Or some other combination?

 

What type of file to you want to create? XPT does not have a fixed meaning.  SAS can make CPORT files using PROC CPORT or you can make XPORT files using XPORT engine (for V5 compatible datasets) or the %LOC2XPT() macro for datasets that use features not supported by version 5 SAS datasets.

r3570
Obsidian | Level 7
Hi Tom,

I was able to create a single xpt file which included all the datasets in
it using proc copy.

Because it is hectic task to write 26 programs for each dataset to convert
into xpt, I need a macro which creates xpt file separately for each dataset
in one go.
Tom
Super User Tom
Super User

Inputs are probably the libref (or optionally the path) to where to find the datasets and the path for writing the XPT files.  Another optional input might be a list of members or logic for how to select the members.

 

Get the list of members.

proc contents data=&libref.._all_ noprint out=contents; run;

Use the list to generate the code to create the XPORT files.

data _null_;
  set contents;
  by memname;
  if first.memname;
  call execute(catx(' '
   ,'libname out xport',quote(catx('/',"&targetdir",cats(lowcase(memname),'.xpt'))),';'
   ,'proc copy inlib=',libname,'outlib=out;','select',nliteral(memname),';run;'
  ));
run;
ballardw
Super User

@r3570 wrote:
Hi Tom,

I was able to create a single xpt file which included all the datasets in
it using proc copy.

Because it is hectic task to write 26 programs for each dataset to convert
into xpt, I need a macro which creates xpt file separately for each dataset
in one go.

If you can place all the data sets into one file what is the advantage of creating 26 files?

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
  • 8 replies
  • 10022 views
  • 0 likes
  • 5 in conversation