Search code examples
if-statementsassas-macro

IF STATEMENTS LOGIC


This is my thought logic for my if statements in SAS. It is not running because I was told I am using it improperly. Can someone provide assistance please?

ERROR 180-322: Statement is not valid or it is used out of proper order.

data tmp_final_tab;
    merge data.final_alerts_ramts(in=a) data.final_alerts_repamts(in=b) data.final_alerts_passthru(in=c)
            data.final_alerts_wires(in=d) data.final_alerts_low_volume(in=e) data.final_alerts_bhvr(in=f);
    by clnt_no;
    if a;
        if pamt_activity_cr >= &rnd_pct_atl then atl_inda1 = 1; else atl_inda1 = 0;
        if pamt_activity_dr >= &rnd_pct_atl then atl_inda2 = 1; else atl_inda2 = 0;
    if b;
        if pamt_activity_cr >= &rnd_pct_atl then atl_indb1 = 1; else atl_indb1 = 0;
        if pamt_activity_dr >= &rnd_pct_atl then atl_indb2 = 1; else atl_indb2 = 0;
    if c;
        if (ptam/total_Amount) * 100 >= &pt_pct_atl then atl_indc = 1; else atl_indc = 0;
    if d;
        if tot_amt_wire >= &hrc_wire_amt_atl then atl_indd = 1; else atl_indd1 = 0;
        if tot_in_amt >= &wire_in_cnt_amt_atl then atl_indd2 = 1; else atl_indd2 = 0;
        if tot_out_amt >= &wire_out_cnt_amt_atl then atl_indd3 = 1; else atl indd3 = 0; 
    if e;
        if tot_amt_lv >= &lv_amt_atl then atl_ind = 1; else atl_ind = 0;
    if f;
        if tot_amt_bhvr >= &bhvr_w_hist_atl then atl_ind = 1; else atl_ind = 0;
    if a or b or c or d or e or f
        if (atl_inda + atl_indb + atl_indc + atl_indd + atl_inde + atl_indf > 0) then btl_ind = 0; else btl_ind = 1;
run;

Solution

  • An if statement in SAS always has the following form (without <>):

    if <boolean logic> then <code>;
    

    If the evaluated logic needs to do more than one thing, it follows this form:

    if <boolean logic> then do;
         <code>;
         <code>;
         <code>;
         ...
    end;
    

    It looks like all of your code creates binary variables. This programming shortcut in SAS will make binary 1/0 variables for you:

    binary_var = (<boolean logic>);
    

    If the logic is true, binary_var is 1. Otherwise, it is 0.

    The correct form of your code is below with a cleaner form of your new binary variables.

    data tmp_final_tab;
        merge data.final_alerts_ramts     (in=a) 
              data.final_alerts_repamts   (in=b) 
              data.final_alerts_passthru  (in=c)
              data.final_alerts_wires     (in=d) 
              data.final_alerts_low_volume(in=e) 
              data.final_alerts_bhvr      (in=f)
        ;
    
        by clnt_no;
    
        if a then do;
            atl_inda1 = (pamt_activity_cr >= &rnd_pct_atl);
            atl_inda2 = (pamt_activity_dr >= &rnd_pct_atl);
        end;
    
        if b then do;
            atl_indb1 = (pamt_activity_cr >= &rnd_pct_atl);
            atl_indb2 = (pamt_activity_dr >= &rnd_pct_atl);
        end;
    
        if c then atl_indc = ( (ptam/total_Amount) * 100 >= &pt_pct_atl);
    
        if d then do;
            atl_indd  = (tot_amt_wire >= &hrc_wire_amt_atl);
            atl_indd2 = (tot_in_amt >= &wire_in_cnt_amt_atl);
            atl_indd3 = (tot_out_amt >= &wire_out_cnt_amt_atl);
        end;
    
        atl_ind = ( (e AND tot_amt_lv >= &lv_amt_atl) OR (f AND tot_amt_bhvr >= &bhvr_w_hist_atl) );
    
        btl_ind = (sum(atl_inda, atl_indb, atl_indc, atl_indd, atl_inde, atl_indf) > 0);
    run;
    

    You technically do not even need if statements here. You can incorporate the dataset in logic into each binary variable (e.g. binary_var = (a AND <logic>);. If missing values are not needed from this operation, you can remove those if statements entirely.