Help using Base SAS procedures

SQL database pass-through auto-output date as date fields instead of text fields

Reply
Frequent Contributor
Posts: 92

SQL database pass-through auto-output date as date fields instead of text fields

Currently the code below will output the results to the SAS dataset WORK.UOBS_RMTL.

The field SUBMISSION_DATE in WORK.UOBS_RMTL is in text. Is there anyway for SAS to auto pick up as date format?

I prefer not to run another data step to convert text to date using INPUT.

proc sql;

CONNECT to ODBC as oradb (datasrc=UOBS_PROD user=&SUser. password=&SPwd.);

%put &sqlxmsg;

   CREATE TABLE WORK.UOBS_RMTL (COMPRESS=YES) AS

  SELECT *

  FROM connection to oradb 

       (

  SELECT

   CA_NUMBER

  , CONVERT(varchar(10), SUBMISSION_DATE, 126) AS SUBMISSION_DATE

  FROM [DRLPP].[dbo].[SML_CA]

) ;

%put &sqlxmsg;

DISCONNECT from oradb;

quit;

run;

Frequent Contributor
Posts: 92

Re: SQL database pass-through auto-output date as date fields instead of text fields

SAS SQL Date formatting - Stack Overflow

Someone suggested like this:

proc sql;
  create table data.test as
  select ID, INPUT(CREATION_DATE,datetime20.) AS CREATION_DATE
  from connection to odbc
  ( select ID, DATE AS CREATION_DATE
  from maintable );
quit;

If I have 12 date columns, it means I have to define 12 times. Quite time-consuming.Smiley Sad

Am looking for some lazy coding.

Super Contributor
Posts: 334

Re: SQL database pass-through auto-output date as date fields instead of text fields

I think the only ways that SAS "guesses" at variables types is through proc import or the import wizard.

You could do the conversions in a data step using arrays and a do loop to save a little on the typing.

EJ

N/A
Posts: 1

Re: SQL database pass-through auto-output date as date fields instead of text fields

I had this problem in connecting to newer SQL server database and the solution was to get an updated version of the odbc interface. The problem there was that the database had a new date field datatype. In the old odbc interface the field was processed as a text field and in the new one it was processed as sas datetime field.

Contributor
Posts: 21

Re: SQL database pass-through auto-output date as date fields instead of text fields

In the sample code you provided you are converting data type from date to character. If you just select the field you need without conversion:

         SELECT    CA_NUMBER,

                          SUBMISSION_DATE

          FROM ...

you will have SUBMISSION_DATE as date if it is date in database.

Ask a Question
Discussion stats
  • 4 replies
  • 399 views
  • 0 likes
  • 4 in conversation