Python 中对 SAS 代码的这种解释是否等效?

Is this interpretation of SAS code in Python equivalent?

我有以下 SAS 代码

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

    by col1;

    retain col2 col3 col4;
    format col2 col3 col4 .

    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;    

我必须将它翻译成 python 代码。我的问题是我不太明白 source_col2 ne col2 ne . 表达式的计算结果,而且我无权访问 SAS 环境来检验我的假设。

是不是像下面这样的东西?

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

SAS 定义了这个特定的运算符,但我不建议使用它,因为没有遇到过它的人都不清楚。来自 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).

这适用于所有“相等”运算符,即 ltlegtgeeqne,和 in(虽然我不完全确定它如何适用于 in)。

在这种情况下,正确的翻译是if source_col2 ne col2 and col2 ne .;如果 col2. 那么无论 source_col2 是什么它总是 false。这可能是也可能不是所需的行为。

示例:

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;   

这个returns:

 4 ne 3 matched
 . ne 3 matched

所以只有当两个值不相等时,才不会丢失正确的值。