if-statementconditional-statementssasnested-if

SAS IF Condition data retrieval


I have a dataset that has many fields out of the 7 needed fields for this condition is

So, I need to check whether either of status_reason_code_1 OR status_reason_code_2 OR status_reason_code_3 is having a value of '68', then we need to pick up the associated status_reason_code_dt i.e. if status_reason_code_1 is 68 I need to retrive the status_reason_code_1_date . Then If status_reason_code_1_date is equal to or less than charge_date then set a value of 'Y' OR else 'N'.

How do I code this in SAS ? How to retrieve the associated date and do the rest of the comparison? When status_reason_code_1 is associated with Status_reason_Code_1_date

I need the code something like this .

''''DATA test1; SET test2; IF status_reason_code_1 = '68' OR status_reason_code_2 = '68' OR status_reason_code_3 = '68' --> Here if status_reason_code_1 is equal to 68 then we should take status_reason_code_1_date and compare it with charge_date. Need a nested if condition here IF status_reason_code_1_date <= charge_date THEN flag = 'Y' ELSE flag='N''''


Solution

  • So define two arrays for the two groups of 3 variables. Loop over them checking for the 68 and then date until you either finish the list or find your condition.

    data want;
       set have;
       array code status_reason_code_1-status_reason_code_3 ;
       array date status_reason_code_1_date status_reason_code_2_date status_reason_code_3_date ;
       found='N';
       do index=1 to 3 until(found='Y') ;
          if code[index]='68' and .Z < date[index] <= charge_date then found='Y';
       end;
       drop index;
    run;
    

    NOTE: Notice how the DATE series of variable names is harder to work with. That is because you placed the numeric sequence number in the MIDDLE of the variable name instead of at the end.