tags:

views:

134

answers:

3

I have a lot of files in SAS format, and I'd like to be able to read them in programs outside of SAS. I don't have anything except the base SAS system installed. I could manually convert each one, but I'd like a way to do it automatically.

+1  A: 

I think you might be able to use ADO, See the SAS support site: http://bit.ly/bvWtU0 for more details. Disclaimer

  1. I haven't looked at this for a while and
  2. I'm not 100% sure that this doesn't require additional licensing, and
  3. I'm not sure if you can do this using Python
Ville Koskinen
+1  A: 

I have never tried http://www.oview.co.uk/dsread/, but it might be what you're looking for: "a simple command-line utility for working with datasets in the SAS7BDAT file format." But do note "This software should be considered experimental and is not guaranteed to be accurate. You use it at your own risk. It will only work on uncompressed Windows-format SAS7BDAT files for now. "

Louisa Grey
+1  A: 

You'll need to have a running SAS session to act as a data server. You can then access the SAS data using ODBC, see the SAS ODBC drivers guide.

To get the local SAS ODBC server running, you need to:

  1. Define your SAS ODBC server setup at described in the SAS ODBC drivers guide. In the example that follows, I'll connect to a server that is set up with the name "loclodbc".
  2. Add an entry in your services file, (C:\WINDOWS\system32\drivers\etc\services), like this:

    • loclodbc 9191/tcp

    ...set the port number (here: 9191) so that it fits into your local setup. The name of the service "loclodbc" must match the server name as defined in the ODBC setup. Note that the term "Server" has nothing to do with the physical host name of your PC.

Your SAS ODBC server is now ready to run, but is has no assigned data resources available. Normally you would set this in the "Libraries" tab in the SAS ODBC setup process, but since you want to point to data sources "on the fly", we omit this.

From your client application you can now connect to the SAS ODBC server, point to the data resources you want to access, and fetch the data.

The way SAS points to data resources is through the concept of the "LIBNAME". A libname is a logical pointer to a collection of data.

Thus

LIBNAME sasadhoc 'C:\sasdatafolder';

assigns the folder "C:\sasdatafolder" the logical handle "sasiodat".

If you from within SAS want access to the data residing in the SAS data table file "C:\sasdatafolder\test.sas7bdat", you would do something like this:

LIBNAME sasadhoc 'C:\sasdatafolder';
PROC SQL;
  CREATE TABLE WORK.test as
  SELECT *
  FROM sasadhoc.test
  ;
QUIT;

So what we need to do is to tell our SAS ODBC server to assign a libname to C:\sasdatafolder, from our client application. We can do this by sending it this resource allocation request on start up, by using the DBCONINIT parameter.

I've made some sample code for doing this. My sample code is also written in the BASE SAS language. Since there are obviously more clever ways to access SAS data, than SAS connecting to SAS via ODBC, this code only serves as an example.

You should be able to take the useful bits and create your own solution in the programming environment you're using...

SAS ODBC connection sample code:

PROC SQL;
  CONNECT TO ODBC(DSN=loclodbc DBCONINIT="libname sasadhoc 'c:\sasdatafolder'");
  CREATE TABLE temp_sas AS
  SELECT * FROM CONNECTION TO ODBC(SELECT * FROM sasadhoc.test);
QUIT;

The magic happens in the "CONNECT TO ODBC..." part of the code, assigning a libname to the folder where the needed data resides.

Martin Bøgelund
Actually, you don't need a running SAS session; the ODBC driver will automatically launch one for you. However, there's no easy way to programmatically create the ODBC configuration, so that has to be manually configured. Which is a problem, since you can't just load an arbitrary dataset.
Chris B.
By arbitrary, you probably mean "located anywhere, with any name"...? Or is it given as a parameter?
Martin Bøgelund
Located anywhere, with any name.
Chris B.
I'm certain this can be done. If you can write the location of the SAS data file to a file at runtime, from your calling application, SAS can be set to read this file upon ODBC start up, and make assignments in order to provide the data via ODBC. I'll look into this.
Martin Bøgelund