How can I read a SAS dataset?

前端 未结 5 1707
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 02:34

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 man

5条回答
  •  清酒与你
    2021-02-06 03:25

    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.

提交回复
热议问题