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

I'm using some old syntax - that I didn't write and don't understand very well - to create SAS data sets from .xpt files. 

* -------------------------------------------------------------------------------------------------------------------;
************************************************** 2013-2014 ********************************************************;
* -------------------------------------------------------------------------------------------------------------------;
* Demographics;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/DEMO_H.XPT' access=readonly;
* Drug Use;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/DUQ_H.XPT' access=readonly;
* Medical Conditions;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/MCQ_H.XPT' access=readonly;
* Prescription Medications - Drug Information;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/RXQ_DRUG.XPT' access=readonly;
* Prescription Medications;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/RXQ_RX_H.XPT' access=readonly;
* Cigarette Use;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/SMQ_H.XPT' access=readonly;
* Recent Tobacco Use;
LIBNAME xptfile xport '/home/myname/Projects/NHANES/XPT Files/SMQRTU_H.XPT' access=readonly;

PROC COPY INLIB=xptfile OUTLIB=nhanes;
RUN;

Is there a way to run this once and have all .xpt files in the "xptfile" library copied into the "nhanes" library as SAS data sets?

 

Currently, I need to run each LIBNAME statement individually and then run the PROC COPY. Not very efficient.

 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
libname x xport ('c:\temp\stocks.xpt' 'c:\temp\class.xpt' );
proc copy in=x out=work;
run;

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

A libname statement can be assigned to cover a collection of other libnames (or a list of directories).  So a proc copy could transcribe all the datasets in a collection of libraries (each listed in the "aggregate" library) to a destination library.  Here's an example:

 

libname sdat1 'c:\temp\sasdata1';
libname sdat2 'c:\temp\sasdata2';

data sdat1.class; set sashelp.class; run;
data sdat2.cars; set sashelp.cars; run;


libname srclibs (sdat1 sdat2);
libname tmp 'c:\temp';
proc copy out=tmp in=srclibs ;
run;

The log reports copying both datasets, one from each SDATx library, to the TMP library.

 

418  libname srclibs (sdat1 sdat2);
NOTE: Libref SRCLIBS was successfully assigned as follows:
      Levels:           2
      Engine(1):        V9
      Physical Name(1): c:\temp\sasdata1
      Engine(2):        V9
      Physical Name(2): c:\temp\sasdata2
419  libname tmp 'c:\temp';
NOTE: Libref TMP was successfully assigned as follows:
      Engine:        V9
      Physical Name: c:\temp
420  proc copy out=tmp in=srclibs ;
421  run;

NOTE: Copying SRCLIBS.CARS to TMP.CARS (memtype=DATA).
NOTE: There were 428 observations read from the data set SRCLIBS.CARS.
NOTE: The data set TMP.CARS has 428 observations and 15 variables.
NOTE: Copying SRCLIBS.CLASS to TMP.CLASS (memtype=DATA).
NOTE: There were 19 observations read from the data set SRCLIBS.CLASS.
NOTE: The data set TMP.CLASS has 19 observations and 5 variables.
NOTE: PROCEDURE COPY used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

 

I have no idea whether this behavior is replicated when reading from a collection of XPORT-engine libraries.

 

Also: if a dataset name appears in multiple locations in the "collective" library, then, in my experience, only the first one (i.e. the leftmost library) is accessed.

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
libname x xport ('c:\temp\stocks.xpt' 'c:\temp\class.xpt' );
proc copy in=x out=work;
run;

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
  • 2 replies
  • 554 views
  • 4 likes
  • 3 in conversation