假设我们有一些数据集具有4级(1,2,3,4)people的分类变量income.我们如何在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;
Run Code Online (Sandbox Code Playgroud)
换句话说,这将为四个级别创建三个虚拟变量.这是正确的吗?
一种更灵活的方法是使用数组.
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;
Run Code Online (Sandbox Code Playgroud)