比较SAS中的日期(日期1 =日期2)

时间:2017-05-23 13:32:05

标签: date sas

我想在SAS数据集中有两个日期,即date1 = date2。我尝试使用ifn和ifc函数执行此操作,但怀疑它们无法正常工作。我直接从sugi获取此代码,但已成功使用这些函数比较字符/数字变量。我也成功尝试使用proc sql进行比较,但我想学习在数据步骤中做到这一点。

我的代码如下:

 data not_in_HDD_3; 
 set not_in_HDD_2; 
  start=STMT_PERIOD_FROM;
  if start=. then start=ADMIT_START_OF_CARE;  
  start_2=input(start, ANYDTDTE8.); 
     format start_2 MMDDYY10.; 
       drop start; 
  rename start_2=start; 
  dob=input(birth_date, ANYDTDTE8.);
      format dob MMDDYY10.; 
 Birth_record = ifn (start eq dob, 0 , 1);
 ifc_result = ifc(start=dob,"True","False","Missing");
 ifn_result = ifn(start=dob,1,0,.);
 ifn_result_fmt = put(ifn_result,ifn_label.);
 fuzz_result = ifc(fuzz(start-dob),"True","False","Missing");
 drop ifn_result;
 run; 


 proc sql;
 create table not_in_HDD_4 as
    select *,
       case
   when (start=dob) then "True"
   else "False"
    end as sql_case_var length=8
   from not_in_HDD_3;
  quit;

非常感谢任何见解!

1 个答案:

答案 0 :(得分:0)

SAS日期只是相对于1960年1月1日(即第0天)的数字。您可以使用任何标准类型的比较来比较它们。以下是一些方法。

data want;
    set have;
    dob = input(birth_date, anydtdte8.);

    /* 1 and 0 indicator if they are equal/not equal */
    result1 = (start = dob);

    /* 1, 0, missing indicator: we are exploiting the fact
       that adding a missing value produces a missing value.
       Note that this is not the case with the sum() function */
    if(start + dob = .) then call missing(result2);
        else if(start = dob) then result2 = 1;
            else result2 = 0;

    /* select function */
    select;
         when(start = dob) result3 = 1;
         when(start + dob = .) result3 = .;
         otherwise result3 = 0;
    end;
run;