【问题标题】:Stop SAS from replacing missing value with 0阻止 SAS 用 0 替换缺失值
【发布时间】:2020-12-03 12:43:35
【问题描述】:

我有一个包含 8 个缺失值的因变量。它目前是一个定量变量。但是,我想将它们分成中位数及以下;并高于中位数,但同时保留 8 个缺失值。以下代码将缺失值替换为零,我不明白为什么。

data baseline_all_disc2;
set baseline_all3;
if health_state_m eq . then health_state_m_disc=.; /*This line of code doesn't seem to be working*/
if health_state_m LE 60 then health_state_m_disc=0;
else health_state_m_disc=1;
run;

请帮忙!

【问题讨论】:

    标签: sas missing-data


    【解决方案1】:

    您需要使用 IF/ELSE IF,而不仅仅是多个 IF 语句。您的代码按照您显示的方式正常运行。

    第一个 IF -> health_state_m_disc 设置为缺失。 第二个 IF -> LE 60 - Missing 被认为小于所以这也被评估为 true。切换到使用 IF/ELSE IF 以避免每次都被评估第二个 IF 语句。

    添加 ELSE,这将起作用。

    data baseline_all_disc2;
    set baseline_all3;
    if health_state_m eq . then health_state_m_disc=.; /*This line of code doesn't seem to be working*/
    ELSE if health_state_m LE 60 then health_state_m_disc=0;
    else health_state_m_disc=1;
    run;
    

    编辑:如果您有多个缺失值编码,则另一种选择,即 ., .A-.Z MISSING() 函数的一个好处是它对字符变量和数字变量的作用相同。

    data baseline_all_disc2;
    set baseline_all3;
    if missing(health_state_m) then call missing(health_state_m_disc); /*This line of code doesn't seem to be working*/
    ELSE if health_state_m LE 60 then health_state_m_disc=0;
    else health_state_m_disc=1;
    run;
    

    【讨论】:

    • 你真的应该使用missing() 而不是eq .。否则将不会捕获特殊的缺失值,因此仍会导致目标变量设置为零。
    • 在我的整个 SAS 职业生涯中,我很少看到特殊缺失值的使用。它们很有用并且确实存在,但它是一种罕见的 IME 用例。
    【解决方案2】:

    缺失值被认为小于数值。 if health_state_m LE 60 then health_state_m_disc=0; 行将其更改为 0。对于您的第二个 if 语句,添加缺失值检查。

    if(NOT missing(health_state_m) AND health_state_m LE 60) then health_state_m_disc=0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      相关资源
      最近更新 更多