Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a SAS dataset?

Tags:

sas

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.

like image 923
Chris B. Avatar asked Mar 03 '10 19:03

Chris B.


People also ask

Can you read a SAS file in Python?

In Python, there are two useful packages Pyreadstat, and Pandas that enable us to open SAS files. If we are working with Pandas, the read_sas method will load a . sav file into a Pandas dataframe.

How many ways we can read data into SAS?

We can provide reading data to SAS in two ways, one when the data is too large, so read it from an external file instead of typing, but when the data is too small, then it is convenient to type the data in the SAS program instead of reading it from an external file. It is known as instream data.


1 Answers

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.

like image 170
Martin Bøgelund Avatar answered Oct 03 '22 23:10

Martin Bøgelund