阻止SAS将缺失值替换为0

问题描述

我有一个因变量,缺少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;

请帮助!

解决方法

缺少的值被认为小于数字值。第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;
,

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

第一个IF-> health_state_m_disc设置为丢失。 第二个IF-> LE 60-丢失被认为小于,因此这也算是正确。切换为使用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;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...