1

I'm trying to read a sas7bdat file from SAS (product of the SAS Institute) into Python.

Yes, I'm a aware that we could export to *.csv files, but I'm trying to avoid that as that will double the number of files we need to create.

There's good documentation for doing this in Visual Basic. Still, I want it in Python. For example, in VB you could write...

Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset

obConnection.Provider = "sas.LocalProvider"
obConnection.Properties("Data Source") = "c:\MySasData"
obConnection.Open

rs.Open "work.a", cn, adOpenStatic, adLockReadOnly, adCmdTableDirect

To open your dataset.

But I can't crack the nut to make this work in python.

I can type...

import adodbapi

cnstr = 'Provider=sas.LocalProvider;c:\\MySasData'

cn = adodbap.connect(cnstr)

And a can get a cursor...

cur = cn.cur()

But beyond that, I'm stumped. I did find a cur.rs, which sounds like a recordset, but it is an object with a type of None.

Also, to preempt some alternative methods...

  1. I do not want to create *.csv files in SAS.
  2. The computer with Python does not have SAS installed, but does have the Providers for OLE DB installed. I know for a fact that the VB code I provided works without SAS in read-only mode. You can download these drivers here: http://support.sas.com/downloads/browse.htm?cat=64
  3. I am not expert in SAS. Honestly, I find their tool cumbersome, confusingly documented, and slow. I noticed that there are some other products listed called "IOMProvider" and "SAS/SHARE". If there's an easier way of doing this using those ADO providers, feel free to document it. However, what I'm really looking for is a way of doing this entirely within Python with a relatively simple bit of code.
  4. Oh, and I'm aware of Python's sas7bdat package, but we're using Python 3.3.5 and it doesn't seem to be compatible. Also, I couldn't figure out how to use it on 2.7 anyways as there's not a lot of documentation and even a question on how to use the tool, which, to this day, is unanswered. Python sas7bdat module usage

Thanks!

Community
  • 1
  • 1
none
  • 1,187
  • 2
  • 13
  • 17
  • I don't have a computer to test this on, but I think your connection string is not correct. `cnstr = 'Provider=sas.LocalProvider;Data Source=c:\\MySasData'` You might need quotes on the c:\MySasData. – DomPazz Aug 19 '14 at 03:57
  • Hello. Thanks for the comment. Unfortunately, no, that's not it. If I type a real directory, such as `C:\\A real folder` without quotes it works just fine. When I type a fake folder name, you get an error message. So I'm pretty sure the syntax there is OK. The only difference front he manual I noticed is that python appears to require two backslashes to properly escape out. I'm pretty sure that syntax is correct. – none Aug 19 '14 at 05:22

1 Answers1

0

Didn't test it with SAS as I don't have a provider installed currently, it should go like this:

cn = adodbapi.connect(cnstr)

# print table names in current db
for table in cn.get_table_names():
    print(table)

with cn.cursor() as c:
    #run an SQL statement on the cursor
    sql = 'select * from your_table'

    c.execute(sql)

    #get the results
    db = c.fetchmany(5)

    #print them
    for rec in db:
        print(rec)

cn.close()

EDIT: Just found this http://support.sas.com/kb/30/795.html so you might need to use other provider for this method, have a look at IOM privoder (https://www.connectionstrings.com/sas-iom-provider/ , http://support.sas.com/documentation/tools/oledb/gs_iom_tasks.htm)

vasja
  • 4,732
  • 13
  • 15
  • 1
    Hello. Thank you for your comment. So I think the problem is that to use the IOM Provider, you have to start an IOM server. Again, I'm no expert in SAS, but I'm not sure how to do that if I don't have SAS installed. All I have is the Provider for OLE DB, which is free. Is sas.LocalProvider simply not compatible with adodbapi? Or can I start an IOM Server from a free tool? Thanks! – none Aug 19 '14 at 13:55
  • 1
    No, you have to have SAS installed and Integration Technologies licensed to start a server that the IOM provider can work with. – DomPazz Aug 19 '14 at 15:46