EDG956
EDG956

Reputation: 982

Is this interpretation of SAS code in Python equivalent?

I have the following SAS code

data output_data (keep=col1 col2 col3 col4);
    set input_data;

    by col1;

    retain col2 col3 col4;
    format col2 col3 col4 $8.

    if first.col1 then do;
        col2=.;
        col3=.;
        col4=.;
    end;
    
    if source_col2 ne col2 ne . then do;
        col3='foo';
        col4='bar';
        output;
    end;
    
    col2 = source_col2;
run;    

I must translate it to python code. My problem is that I don't quite understand what the source_col2 ne col2 ne . expression evaluates to and I don't have access to a SAS environment to test my hypothesis.

Is is something like the follwing?

if source_col2 is not None and col2 is not None and source_col2 != col2:
    pass

Upvotes: 0

Views: 115

Answers (1)

Joe
Joe

Reputation: 63424

SAS defines this particular operator, though I don't recommend using it as it's not clear to anyone who hasn't come across this. From SAS Operators in expressions:

TABLE NOTE 8:  

An exception to this rule occurs when two comparison operators surround a quantity. For example, the expression x<y<z is evaluated as (x<y) and (y<z).

This applies to all of the "equality" operators, ie, lt, le, gt, ge, eq, ne, and in (though I'm not totally sure how it would apply to in).

In this case, the correct translation is if source_col2 ne col2 and col2 ne .; if col2 is . then it will always be false no matter what source_col2 is. This may or may not be the desired behavior.

Example:

data _null_;
    if 4 ne 4 ne . then do;
        put "4 ne 4 matched";
    end;
    if 4 ne 3 ne . then do;
        put "4 ne 3 matched";
    end;
    if . ne 3 ne . then do;
        put ". ne 3 matched";
    end;
    if 4 ne . ne . then do;
        put "4 ne . matched";
    end;
    if . ne . ne . then do;
        put ". ne . matched";
    end;
    
run;   

This returns:

 4 ne 3 matched
 . ne 3 matched

So only when the two values are unequal, and the right value is not missing.

Upvotes: 1

Related Questions