gpt4 book ai didi

sas - SAS中的虚拟变量

转载 作者:行者123 更新时间:2023-12-04 13:42:49 25 4
gpt4 key购买 nike

假设我们有一些数据集 people其中有一个分类变量 income有 4 个级别(1、2、3、4)。我们将如何在 SAS 中对此进行编码?可不可能是:

data people;
set people;
if income=1 then income1=1;
else if income=2 then income2=1
else if income =3 then income3=1;
run;

换句话说,这将为四个级别创建三个虚拟变量。这是正确的吗?

最佳答案

一种更灵活的方法是使用数组。

data people;
set people;
array incomes income1-income4;
do _t = 1 to dim(incomes);
if income=_t then income[_t] = 1;
else if not missing(income) then income[_t]=0;
else income[_t]=.;
end;
run;

关于sas - SAS中的虚拟变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com