将DB2时间戳转换为SAS数字

发布于 2025-01-29 05:40:49 字数 1052 浏览 0 评论 0原文

我想将DB2时间戳转换为SAS数字

proc sql;
connect to db2 ;
     create table db2ts as
     select * from connection to db2
     (select char(current timestamp)
      from sysibm.sysdummy1)
      as db2ts(timestm);

   %put &sqlxmsg ;
   %put &sqlxrc ;

  disconnect from db2;
quit;

data _NULL_;

   set DB2TS;
    
putlog 'timestmb--' timestm;

datets =input(timestm,yymmdd10.);
timets =input(substr(timestm,12),time15.);
dt2=dhms(datets,0,0,timets);
format datets date9. timets time15.6 dt2 datetime26.6;

putlog 'currdatets:' dt2;

call symput('currdatets',catx('-',put(datepart(dt2),yymmdd10.),
     translate(put(dt2,tod15.6),'.',':')));

putlog 'currdatets:' currdatets;


run;

timestmb--2022-05-18-16.44.54.587001

currdatets:18MAY2022:16:44:54.587001

%put currdatets: &currdatets;

currdatets: 2022-05-18-16.44.54.587001

Proc sql:

Insert into table1

(Time, Type)

Values

(%sysfunc(quote(&currdatets)), 'A')

错误:

错误:值第1条的值1不匹配

对象项目列表中相应列的数据类型(在“选择”子句中)。

如何使用宏变量Currdatet插入DB2表中?

I want to convert the Db2 timestamp into SAS numeric

proc sql;
connect to db2 ;
     create table db2ts as
     select * from connection to db2
     (select char(current timestamp)
      from sysibm.sysdummy1)
      as db2ts(timestm);

   %put &sqlxmsg ;
   %put &sqlxrc ;

  disconnect from db2;
quit;

data _NULL_;

   set DB2TS;
    
putlog 'timestmb--' timestm;

datets =input(timestm,yymmdd10.);
timets =input(substr(timestm,12),time15.);
dt2=dhms(datets,0,0,timets);
format datets date9. timets time15.6 dt2 datetime26.6;

putlog 'currdatets:' dt2;

call symput('currdatets',catx('-',put(datepart(dt2),yymmdd10.),
     translate(put(dt2,tod15.6),'.',':')));

putlog 'currdatets:' currdatets;


run;

timestmb--2022-05-18-16.44.54.587001

currdatets:18MAY2022:16:44:54.587001

%put currdatets: &currdatets;

currdatets: 2022-05-18-16.44.54.587001

Proc sql:

Insert into table1

(Time, Type)

Values

(%sysfunc(quote(&currdatets)), 'A')

Error:

ERROR: Value 1 of VALUES clause 1 does not match the data type of

the corresponding column in the object-item list(in the SELECT clause).

how can I use the macro variable currdatets to insert into the DB2 table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2025-02-05 05:40:49

因此,您的第一个查询不是有效的SQL语法,因此我不确定它是如何工作的。尝试类似的内容:

create table db2ts as
  select * from connection to db2
  (select char(current timestamp) as timestmp
   from sysibm.sysdummy1)
;

这将创建一个数据集名称db2ts,其中一个名为timestmp的字符变量;

这应该与您通过此数据步骤获得的相似。

data db2ts ; 
  timestmp ='2022-05-17-12.02.43.387486';
run;

现在,您可以将字符串转换为日期和时间值,然后将其组合到DateTime值。

data want ;
  set db2ts;
  date=input(timestmp,yymmdd10.);
  time=input(substr(timestmp,12),time15.);
  datetime=dhms(date,0,0,time);
  format date date9. time time15.6 datetime datetime26.6;
  put (_all_) (=/);
run;

结果:

timestmp=2022-05-17-12.02.43.387486
date=17MAY2022
time=12:02:43.387486
datetime=17MAY2022:12:02:43.387486

如果您可以弄清楚DB2是否具有cast()函数的函数或选项,可以让您指定字符串是如何生成的,以便SAS可以立即识别为DateTime的东西,这将变得更容易。

So your first query is not valid SQL syntax so I am not sure how it even works. Try something like:

create table db2ts as
  select * from connection to db2
  (select char(current timestamp) as timestmp
   from sysibm.sysdummy1)
;

This will create a dataset name db2ts with a character variable named timestmp;

Which should be the similar to what you would get with this data step.

data db2ts ; 
  timestmp ='2022-05-17-12.02.43.387486';
run;

Now you can convert the string into a DATETIME value by first converting it into a DATE and a TIME value and then combining them.

data want ;
  set db2ts;
  date=input(timestmp,yymmdd10.);
  time=input(substr(timestmp,12),time15.);
  datetime=dhms(date,0,0,time);
  format date date9. time time15.6 datetime datetime26.6;
  put (_all_) (=/);
run;

Results:

timestmp=2022-05-17-12.02.43.387486
date=17MAY2022
time=12:02:43.387486
datetime=17MAY2022:12:02:43.387486

It would be easier if you could figured out if DB2 has a function, or option for the CAST() function, that can let you specify how the string is generated so that it is something that SAS can recognize immediately as a datetime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文