0
votes

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!

1
This isn't related to your question but you can clean up your code a bit by using the coalesce function, return the first non-missing value. start = coalesce(STMT_PERIOD_FROM,ADMIT_START_OF_CARE). Also, I don't see any issues with your conditionals. Can add a portion of your dataset to your question? - J_Lard
Thanks! Unfortunately, I can't add this data, it is sensitive :( - CharlieATX
Can you just add the dates/if_results? - J_Lard
Hey @J_Lard There is something weird going on with the formatted date vars I created. I used your coalesce with my unformatted variables and the ifn function, and it worked liked a charm. SAS dates always get me! - CharlieATX
MAKE FAKE DATA THAT LOOKS LIKE YOUR REAL DATA. - Reeza

1 Answers

0
votes

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;