CharlieATX
CharlieATX

Reputation: 13

Comparing Dates in SAS (Date1=Date2)

I have two dates in my SAS dataset that I would like to compare i.e., does date1 = date2. I attempted to do this using the ifn and ifc functions but have a suspicion they are not working correctly. I took this code directly from a sugi, but have worked with these functions successfully comparing character/numeric variables. I also successfully attempted the comparison using proc sql, but I want to learn to do this in a data step.

My code is below:

 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;

Any insight is appreciated!

Upvotes: 0

Views: 2761

Answers (1)

Stu Sztukowski
Stu Sztukowski

Reputation: 12944

SAS dates are simply numbers relative to Jan 1st, 1960 (i.e., day 0). You can compare them using any standard type of comparison. Here are a few ways to do it.

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;

Upvotes: 0

Related Questions