繁体   English   中英

将SAS日期时间转换为SQL日期时间

[英]Convert SAS Datetime to SQL Datetime

这就是我目前面临的问题。 我很难将SAS日期时间转换为SQL日期时间。

SAS Datetime中的显示值是28Mar2018:10:01:06,在执行Proc SQL时实际上是“ 28Mar2018:10:01:96” dt。

显示值为SQL日期时间为2018-03-28 10:01:06.000,当使用SQL传递时实际上为'2018-03-28 10:01:06.000'。

我有SAS日期时间值,并想在SQL直通语句中使用它。 我一直在网上找到,但找不到任何相关信息。 我能找到的最多只是转换日期。

Passthrough语句应该是这样的(没有connect to xxx语句)

%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;

寻求您的好建议。

以下是SAS直通到Teradata的示例。 您可以为所需的数据库进行更改:

%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;

您的查询称为显式SQL传递,您可以在其中通过使用connect语句连接到数据库。 在显式传递中,您必须遵守特定数据库的规则(不能使用SAS的任何组件),但是显式SQL传递仅允许您使用宏变量。

在各种数据库中,通常按如下所示访问日期或日期时间,这与我们在SAS中访问日期的方式大不相同。

where  datecolumn = '2018-03-28'

如下所示使用宏变量并在单引号中使用的问题是,在大多数数据库中,宏变量不会解析为用于指示列名的单引号和双引号,因此宏变量上的单引号或双引号均无效

 where  datecolumn = '&macrovariable'

解决的一种方法是定义宏变量,然后使用%bquote,如下所示。

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

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

另一种方法是在定义宏变量时使用单引号,然后在查询中不使用引号,如下所示。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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