sas informat datetime [英] sas informat datetime

查看:379
本文介绍了sas informat datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议在适当的SAS信息阅读日期时间(dd / mm / yyyy hh:mm)?



例如

  data _null_; 
informat from_dt datetime ????
输入from_dt;
put from_dt =;
卡;
01/01/1960 00:00
;运行;


解决方案

我不认为特定的信息是内置的,在,但它是比较容易滚动自己的。以下是创建自定义datetime informat(这需要一个cntlin数据集)和一个自定义格式(使用picture语句)读取您的特定日期时间,然后将其格式化为与输入相同的示例。我简化了它,假设时间部分总是在午夜(00:00),但是如果你需要跟踪时间部分(只需将86400的数字更改为3600,每小时可以轻松扩展),而60每分钟)。如果您打开work.infmt数据集以查看它的样子,它有助于了解发生了什么。

  / *创建自定义日期时间格式为月/日/年< space>小时:分钟* / 
proc格式;
图片mydt other ='%0m /%0d /%0Y%0H:%0M'(datatype = datetime);
运行;

/ *使用上述格式创建一个自定义信息* /
data infmt;
保留fmtnamedtwithspace类型I;
do label =1jan1960:00:00dt to
2jan2059:00:00dt by 86400;
start = trim(left(put(label,mydt。)));
输出;
结束
运行;
proc格式cntlin = infmt;
运行;


/ *现在将信息和格式应用于数据* /
data _null_;
输入from_dt $ 1-20;
格式from_dt2 mydt。
from_dt2 = input(from_dt,dtwithspace。);
put from_dt2 =;
卡;
01/01/1960 00:00
01/02/1999 00:00
;
运行;

这样输出如下:

  278 data _null_; 
279输入from_dt $ 1-20;
280格式from_dt2 mydt。
281 from_dt2 = input(from_dt,dtwithspace。);
282 put from_dt2 =;
283张卡;

from_dt2 = 01/01/1960 00:00
from_dt2 = 01/02/1999 00:00


Can anyone advise on the appropriate SAS informat to read in a datetime (dd/mm/yyyy hh:mm) ???

eg

data _null_;
informat from_dt datetime????.;
input from_dt ;
put from_dt=;
cards;
01/01/1960 00:00
;run;

解决方案

I don't think that specific informat is built-in, but it is relatively easy to roll your own. Below is an example of a creating a custom datetime informat (this requires a cntlin data set) and a custom format (using the picture statement) that reads in your specific datetime and then formats it back out to look the same as the input. I simplified it by assuming the time part was always midnight (00:00), but it can be easily expanded if you need to keep track of the time parts as well (just change the 86400 number to 3600 to get every hour, and 60 for every minute). It helps to see what is going on if you open the work.infmt data set to see what it looks like.

/* Create a custom datetime format as Month/Day/Year<space>Hours:Minutes*/
proc format;
  picture mydt other='%0m/%0d/%0Y %0H:%0M' (datatype=datetime);
run;

/* Create a custom informat using the format above */
data infmt ;
retain fmtname "dtwithspace" type "I" ;
   do label = "1jan1960:00:00"dt to
              "2jan2059:00:00"dt by 86400 ; 
      start = trim(left(put(label,mydt.)));
      output ;
   end ;
run ;
proc format cntlin = infmt ;
run ;


/* Now apply the informat and format to the data */
data _null_;
input from_dt $ 1-20;
format from_dt2 mydt.;
from_dt2=input(from_dt, dtwithspace.);
put from_dt2=;
cards;
01/01/1960 00:00
01/02/1999 00:00
;
run;

This gave an output like this:

278  data _null_;
279  input from_dt $ 1-20;
280  format from_dt2 mydt.;
281  from_dt2=input(from_dt, dtwithspace.);
282  put from_dt2=;
283  cards;

from_dt2=01/01/1960 00:00
from_dt2=01/02/1999 00:00

这篇关于sas informat datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆