views:

156

answers:

2

I'm using Python to read and write SAS datasets, using pyodbc and the SAS ODBC drivers. I can load the data perfectly well, but when I save the data, using something like:

cursor.execute('insert into dataset.test VALUES (?)', u'testing')

... I get a pyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter)') error.

The problem seems to be the fact I'm passing a unicode string; what do I need to do to handle this?

+1  A: 

Do you know what character encoding your database is expecting? If so, you could try encoding your Unicode string before executing the query. So if your database is expecting utf-8 strings, you could try something like:

encoding = 'utf-8' # or latin1 or cp1252 or something
s = u'testing'.encode(encoding)
cursor.execute('insert into dataset.test VALUES (?)', s)
Will McCutchen
Encoding isn't mentioned in the pyodbc documentation or the SAS ODBC documentation. So, in a word, no. I could just test a bunch of encodings, but I have no idea how to ensure I'm using the correct encoding if the system is running on a different machine.
Chris B.
A: 

You say "I can load the data perfectly well" ... does this mean that you can load data that contains characters that are NOT in the native encoding used on your platform (presumably cp1252 on Windows, but please confirm)? What is the SAS datatype of the first column of your SAS dataset?

This article in the SAS docs purports to show how you can find out the encoding used in a SAS dataset.

Encoding is mentioned in the SAS ODBC documentation. However you don't appear to be using SAS ODBC (i.e. SAS-language script accessing non-SAS data).

John Machin
When I say I can load the data perfectly well, I mean it comes through as a unicode string, not as bytes. And I'm using the external SAS ODBC drivers, not calling SQL from inside a SAS script.
Chris B.
Re your comment: 1st sentence: please answer these questions explicitly (0) do you mean comes through as a Python unicode object, not as a str object (1) can you load data that contains unicode characters that are NOT in the native encoding used on your platform (2) what is your platform (3) what is the native encoding on your platform (4) What is the SAS datatype of the first column of your SAS dataset? ... 2nd sentence: yeah, that's what I guessed, isn't it? Please try to find out the encoding used in your SAS dataset. Edit your question to add new information, don't do it in a comment.
John Machin