Skip to content
Advertisement

Writing SAS dates to SQL Server databse

How to write SAS dates to Microsoft SQL Server 2016 Date data type in database?

I got SAS data with a sas date DataEndDay and I want to write that into a database. The following bit is in use (buffer is just to speed up the testing-failing) :

Of course because SAS dates are numbers, direct writing is going to fail. What works is if I hard-code this as:

But If I convert the SAS date to string in SAS datasteps:

and try to write either of these, I get conversion error:

ERROR: ICommand::Execute failed. : Conversion failed when converting date and/or time from character string.

When I select the string it looks pretty ok:

‘2018-12-12’

previously I’ve managed to write dateTimes with similar trick, which works:

Anyone ran into similar issues? How could I write the dates? I could ask them to change the column to dateTime, maybe….

Thank you in advance.

Edit:

I managed to develop a work-around, which works but is ugly and -frankly- I don’t like it. It so happens that my date is same for all rows, so I can assing it to macro variable and then use it in database writing.

Of course this would fail immediately should DataEndDay vary, but maybe demonstrates that something is off in Proc SQLs select clause….

Edit Edit Pasted the question to SAS forums

Advertisement

Answer

I finally managed to crack the issue. The issue was for the missing values. As I am passing the values as strings into the database the parser interpreted missing values as real dots instead of empty strings. The following works:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement