将时间戳转换为SAS宏变量

发布于 2025-01-30 06:54:56 字数 302 浏览 1 评论 0原文

如何将时间戳转换为数值值,而无需微秒的值将其四舍五入并将其存储在宏变量中

data temp;

ts='2022-05-18:10:11:12.796429';

ts1 = input(ts,anydtdtm26.);

putlog 'ts1-->' ts1;

call symput('new_ts1',ts1);

run;

%put new_ts1: &new_ts1;

ts1-->1968487872.8

new_ts1: 1968487872.8

是否可以存储实际值而不是圆形值?

how to convert the timestamp into numeric value without microseconds value getting rounded up and store it in a macro variable

data temp;

ts='2022-05-18:10:11:12.796429';

ts1 = input(ts,anydtdtm26.);

putlog 'ts1-->' ts1;

call symput('new_ts1',ts1);

run;

%put new_ts1: &new_ts1;

ts1-->1968487872.8

new_ts1: 1968487872.8

Is it possible to store the actual value instead of the round value?

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

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

发布评论

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

评论(1

夜灵血窟げ 2025-02-06 06:54:56

您一直在问同样的问题。

校正

否。将DateTime(又称时间戳)值存储到SAS中的Micro Second是不可能的。对于将小数点作为浮点数编号存储的小数值将超过精度的限制。

您可以将大量数字存储为整数的微秒数。但并不是以微秒为代表的秒数为秒。

示例:

836  data test;
837    second = '18MAY2022:10:11:12'dt + 0.796429;
838    micros = '18MAY2022:10:11:12'dt*1E6 + 796429;
839    put second= comma30.6 / micros = comma30. ;
840  run;

second=1,968,487,872.796420
micros=1,968,487,872,796,429

请注意,在以秒数为单位存储时,微秒位置数字是如何丢失的,但是当存储为微秒的整数计数时,该值仍然存在。

不。您不能使用AnyDTDTM Informat将特定的字符串转换为具有微秒精度的DateTime值。使用其他方法。并将微秒存储在DateTime的单独变量中。

date test;
  ts='2022-05-18:10:11:12.796429';
  dt=dhms(input(ts,yymmdd10.),0,0,input(substr(ts,12),time8.));
  microseconds = input(scan(ts,-1,'.'),6.);
  format dt datetime19.  microseconds z6. ;
run;

否。您不能使用隐式传递到DB2来移动DateTime值并将精度保留到Micro Second。

要将值从DB2移动到SAS,将它们转换为DB2中的字符串并移动字符串。然后,您可以使用上述方法在需要的情况下转换为实际的DateTime值。

要将值移回DB2,您可以逆转该过程并将值作为字符串移动,然后使用DB2代码将其转换为DB2时间戳值。或生成代码通过explicit Pass在DB2中运行(也许是通过将DB2时间戳的文本构建到宏变量中)将传递该值。

因此,也许类似:

%let myts=%put<what ever the heck syntax works for timestamp value in DB2>;
proc sql;
 connect to db2 ... ;
 execute by db2 
   (insert into myschema.mytable (myts)
    values ( &myts. )
   )
  ;

您可以尝试构建这样的字符串,以使日期,时间和微秒片段串联。在将微秒转换为字符串时,请确保使用z格式,以便小于十分之一的值将具有所需的前导零。

因此

data _null_;
  set test;
  call symputx('myts',cats(
     "'",put(datepart(dt),yymmddd10.)
    ,":',put(dt,tod8.)
    ,".",put(milliseconds,z6.)
    ,"'"));
run;
     

You keep asking this same question.

Correction

NO. It is impossible to store datetime (aka timestamp) values to the micro second in SAS. The values will exceed the limits of precision for storing decimal values with fractions as floating point numbers.

You can store that large a number as an integer number of microseconds. But not as a number of seconds with microseconds represented as fractions of a second.

Example:

836  data test;
837    second = '18MAY2022:10:11:12'dt + 0.796429;
838    micros = '18MAY2022:10:11:12'dt*1E6 + 796429;
839    put second= comma30.6 / micros = comma30. ;
840  run;

second=1,968,487,872.796420
micros=1,968,487,872,796,429

Notice how the microseconds place digit is lost when stored in number of seconds but when stored as an integer count of microseconds instead the value is still there.

NO. You cannot use the ANYDTDTM informat to convert that specific string into a datetime value with microsecond precision. Use a different method. And store the microseconds in a separate variable from the datetime.

date test;
  ts='2022-05-18:10:11:12.796429';
  dt=dhms(input(ts,yymmdd10.),0,0,input(substr(ts,12),time8.));
  microseconds = input(scan(ts,-1,'.'),6.);
  format dt datetime19.  microseconds z6. ;
run;

enter image description here

NO. You cannot use implicit passthru to DB2 to move datetime values and retain the precision to the micro second.

To move the values from DB2 to SAS convert them to a string in DB2 and move the string. You can then use a method like above to convert into an actual datetime value if you need.

To move the values back to DB2 you reverse the process and move the value as a string and use DB2 code to convert it to a DB2 timestamp value. Or generate code to run in DB2 via explicit pass thru (perhaps by building the text of a DB2 timestamp literal into a macro variable) that will transfer the value.

So perhaps something like:

%let myts=%put<what ever the heck syntax works for timestamp value in DB2>;
proc sql;
 connect to db2 ... ;
 execute by db2 
   (insert into myschema.mytable (myts)
    values ( &myts. )
   )
  ;

You could try to build such a string be concatenating the date, time and microsecond pieces. Make sure the use the Z format when converting the microseconds into a string so that values less than a tenth of a second will have the needed leading zeros.

So if you want to generate a string like '2022-05-18:10:11:12.796429' you might do this:

data _null_;
  set test;
  call symputx('myts',cats(
     "'",put(datepart(dt),yymmddd10.)
    ,":',put(dt,tod8.)
    ,".",put(milliseconds,z6.)
    ,"'"));
run;
     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文