本文介绍了SAS 无效的“闰年"日期问题 yymmdd8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些包含几个错误日期的原始数据.具体来说,有人在非闰年输入了2 月 29 日".例如:

I am reading in some raw data that has a couple of bad dates. Specifically, someone has keyed in "29th Feb" on a NON leap year. For example:

data _null_;
input test :yymmdd8.;
format test date9.;
cards;
20270229
run;

客户希望恢复到 2 月 28 日.是否有快速/有效的方法来执行此操作?例如相当于:

The customer would like this to revert to the 28th Feb. Is there a quick / efficient method of doing this? eg an equivalent of:

IF iserror(date) 然后 date=date-1;?

IF iserror(date) then date=date-1; ?

任何建议都非常感谢!

推荐答案

我会更加小心地确定日期.这是一种方法.hth.

I would be a bit more careful fixing dates. here is one way. hth.

%put sysvlong=&sysvlong sysscpl=&sysscpl;
/* sysvlong=9.02.01M0P020508 sysscpl=W32_VSPRO */

/* read a date both as character(temp) and numeric(date).
   if the numeric date is missing then check if the
   character date ends with "0229," if so, then change it
   to "0228" and see if it is a valid date.
   If OK, then that is it. otherwise, keep it missing. */
%let FEB29 = 0229;
%let FEB28 = 0228;
data one;
  drop temp;
  input temp $char8. @1 date ?? yymmdd8.;
  if missing(date) then link fix;
  format date b8601da.;
  put (_all_) (=);
  return;
fix:
  if length(strip(temp))^=8 then return;
  if substr(temp,5) ^= "&FEB29" then return;
  date = input(cat(substr(temp,1,4), "&FEB28"), ?? yymmdd8.);
return;
cards;
20080229  ok
20090229  should be changed to 28th
201XX229  this should be missing
20110229  -> 28
20120229  ok
20130229  -> 28
20270229  -> 28
;
run;

/* on log
temp=20080229 date=20080229
temp=20090229 date=20090228
temp=201XX229 date=.
temp=20110229 date=20110228
temp=20120229 date=20120229
temp=20130229 date=20130228
temp=20270229 date=20270228
NOTE: The data set WORK.ONE has 7 observations and 1 variables.
*/

这篇关于SAS 无效的“闰年"日期问题 yymmdd8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:13