简体   繁体   中英

Convert SAS Datetime to SQL Datetime

This is what i am facing currently. I am having difficulty to convert SAS Datetime to SQL Datetime.

The display value in SAS Datetime is 28Mar2018:10:01:06 which is actually "28Mar2018:10:01:96"dt when doing Proc SQL.

The display value is SQL Datetime is 2018-03-28 10:01:06.000 which is actually '2018-03-28 10:01:06.000' when using SQL Pass through.

I have the SAS datetime value and want to use it in SQL Pass through statement. I have been finding online but i couldnt find any related information. The most i could find is only converting Date.

The Passthrough statement should be something like this(without the connect to xxx statement)

%let sasdatetime=2018-03-28 10:01:06.000;

/*How to convert the value above to SQL format and apply in proc sql below*/


Proc sql:

connect to xxx
select *

from table A

where birthdatetime = &thedatetime;

Seeking for your kind advice.

Below is an example for SAS pass-through to Teradata. You can make change for your desired database:

%let sasdatetime=2018-03-28 10:01:06.000; 

%let tdlogin=%unquote(user=yourDBUsrNm  pwd=yourDBPassWord MODE=Teradata      tdpid=servername); 

 PROC SQL;
   connect to teradata( &tdlogin connection = global);
   CREATE TABLE work.test1 AS 
   SELECT * from connection to teradata
    ( select *

        from table A

    where birthdatetime = compress("'"||put(&sasdatetime,yymmddn8.)||"'") 
    );                      
   disconnect from teradata;   

 QUIT;

your query is known as explicit SQL pass through, where you connect (by using connect statement) to database. In explcit pass through you have to adhere to the rules of that particular database ( cannot use any components of SAS), but explicit SQL pass through only allows you to use macro variables.

In various database generally date or datetime are accessed as shown below, which is very different than the way we access dates in SAS

where  datecolumn = '2018-03-28'

problem with using macro variable as shown below and using in single quote is that macro variable do not resolve in single quotes and double quotes used to indicate column name in most of databases, so neither single quote or double quote on macro variable works

 where  datecolumn = '&macrovariable'

one way to solve is to define macrovariable and then use %bquote as shown below.

%let sasdatetime=2018-03-28 10:01:06.000; 

Proc sql:
connect to xxx
select *
from table A
where birthdatetime =  %bquote('&sasdatetime') ;

one more way is to have single quotes, while defining macro variable and then use no quotes in your query as shown below.

     %let sasdatetime='2018-03-28 10:01:06.000'; 

Proc sql:
connect to xxx
select *
from table A
where birthdatetime =  &sasdatetime ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM