How can I read a SAS dataset?

前端 未结 5 1677
伪装坚强ぢ
伪装坚强ぢ 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:14

    You could make a SAS-to-CSV conversion program.

    Save the following in sas_to_csv.sas:

    proc export data=&sysparm
        outfile=stdout dbms=csv;
    run;
    

    Then, assuming you want to access libname.dataset, call this program as follows:

    sas sas_to_csv -noterminal -sysparm "libname.dataset"
    

    The SAS data is converted to CSV that can be piped into Python. In Python, it would be easy enough to generate the "libname.dataset" parameters programmatically.

    0 讨论(0)
  • 2021-02-06 03:14

    I think you might be able to use ADO, See the SAS support site for more details.

    Disclaimer:

    1. I haven't looked at this for a while
    2. I'm not 100% sure that this doesn't require additional licensing
    3. I'm not sure if you can do this using Python
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 03:28

    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. "

    0 讨论(0)
  • 2021-02-06 03:36

    There's now a python package that will allow you to read .sas7bdat files, or convert them to csv if you prefer

    https://pypi.python.org/pypi/sas7bdat

    0 讨论(0)
提交回复
热议问题