sas 信息日期时间

发布于 2024-08-03 21:12:40 字数 197 浏览 5 评论 0原文

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

例如

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

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;

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

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

发布评论

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

评论(5

飘逸的'云 2024-08-10 21:12:41

通常,anydt* 系列信息确实有效。

data _null_;
  from_dt = input("01/01/1960 00:00", anydtdtm.);
  put from_dt= :datetime20.;
run;
/* on log
from_dt=01JAN1960:00:00:00
*/

The anydt* family of informats do work, usually.

data _null_;
  from_dt = input("01/01/1960 00:00", anydtdtm.);
  put from_dt= :datetime20.;
run;
/* on log
from_dt=01JAN1960:00:00:00
*/
Smile简单爱 2024-08-10 21:12:41

我不认为特定的信息是内置的,但推出自己的信息相对容易。下面是创建自定义日期时间信息(这需要 cntlin 数据集)和自定义格式(使用图片语句)的示例,该格式读取您的特定日期时间,然后将其格式化以使其看起来与输入相同。我通过假设时间部分始终为午夜 (00:00) 来简化它,但如果您还需要跟踪时间部分,则可以轻松扩展它(只需将 86400 数字更改为 3600 即可获取每小时,并且 60每分钟)。如果您打开 work.infmt 数据集以查看其外观,将有助于了解正在发生的情况。

/* 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;

这给出了这样的输出:

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

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
看轻我的陪伴 2024-08-10 21:12:41

SAS 可能不支持数据所在的特定日期时间格式。您可以尝试将传入数据转换为更友好的格式,也可以使用 substr, DHMS 和 MDY 函数:

data test;
    format dtstr $16. dt datetime22.4;
    dtstr='01/01/1960 00:00';
    day=substr(dtstr,1,2);
    month=substr(dtstr,4,2);
    year=substr(dtstr,7,4);
    hour=substr(dtstr,11,2);
    minute=substr(dtstr,15,2);
    dt=DHMS(MDY(1*month,1*day,1*year),1*hour,1*minute,0);
    output;
run;

或者您可以将日期时间字符串转换为 datetimew.d 格式并输入格式化字符串

data test;
    format dtstr $16. dstr $8. tstr $5. indtstr $14. dt datetime22.4;
    dtstr='01/01/1960 00:00';
    dstr=put(input(substr(dtstr,1,10),mmddyy10.),date8.);
    tstr=substr(dtstr,12);
    indtstr=dstr!!':'!!tstr;
    dt=input(indtstr,datetime14.0);
    output;
run;

:转换可以压缩为单个但复杂的语句,因此为此创建一个宏可能是一个不错的决定。

SAS might not support the specific datetime format your data is in. You could either try to convert the incoming data to a frendlier format or you could parse the datetime using the substr, DHMS and MDY functions:

data test;
    format dtstr $16. dt datetime22.4;
    dtstr='01/01/1960 00:00';
    day=substr(dtstr,1,2);
    month=substr(dtstr,4,2);
    year=substr(dtstr,7,4);
    hour=substr(dtstr,11,2);
    minute=substr(dtstr,15,2);
    dt=DHMS(MDY(1*month,1*day,1*year),1*hour,1*minute,0);
    output;
run;

Or alternatively you could convert the datetime string into a datetimew.d format and input the formatted string:

data test;
    format dtstr $16. dstr $8. tstr $5. indtstr $14. dt datetime22.4;
    dtstr='01/01/1960 00:00';
    dstr=put(input(substr(dtstr,1,10),mmddyy10.),date8.);
    tstr=substr(dtstr,12);
    indtstr=dstr!!':'!!tstr;
    dt=input(indtstr,datetime14.0);
    output;
run;

The conversion can be compressed to a single but complex statement, so creating a macro for this might be a good decision.

对你再特殊 2024-08-10 21:12:41

此条目来自 SAS 知识库 包含用于解析和格式化日期时间的代码。看起来 SAS 有一个很棒的在线帮助系统。

Google 网上论坛的交流中的第三条消息可能也会有帮助。它讨论了输入日期时间,并提供了代码。

你的问题很难理解,而且我对 SAS 知之甚少,我只能提供这么多。希望有帮助。

This entry from the SAS knowledgebase includes code for parsing and formatting datetime. Looks like SAS has a great online help system.

The third message in this exchange on Google groups may be helpful as well. It talks about inputting datetime, and provides code.

Your question is so hard to decipher, and I know so little about SAS, that's about all I can offer. Hope it helps.

戒ㄋ 2024-08-10 21:12:41

按照此处的列表,我认为没有。

可以想象,您可以使用 proc 格式 创建自己的格式,但我认为这会非常困难。 Ville Koskinen 的建议可能是您最好的选择。

Going by the list here, I don't think there is one.

Conceivably you could create your own with proc format, but I think that would be very difficult. Ville Koskinen's suggestion is probably your best bet.

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