我有个人记录的数据集,组织成家庭。
目标是创建一个名为NCH的新变量,如果一个人年满65岁或以上,并且 1意味着与儿子同住 2意味着与女儿一起生活 3意味着与儿子和女儿共同生活 。意味着不与孩子一起生活
我认为带有do循环的数组应该能够实现这一点,但我在SAS中做这个的经验很少。有谁知道如何做到这一点?
答案 0 :(得分:0)
我想出了两个解决方案,一个使用proc sql,另一个使用数据步骤。这是proc sql解决方案:
proc sql;
create table new as
select *,
case when 'M' in (select b.sex from people as b where a.serial=b.serial
and (a.pernum = b.momloc or a.pernum = b.poploc))
then 1 else 0 end as son,
case when 'F' in (select c.sex from people as c where a.serial=c.serial
and (a.pernum = c.momloc or a.pernum = c.poploc))
then 1 else 0 end as daughter,
case when calculated son = 1 and calculated daughter = 0 then 1
when calculated son = 0 and calculated daughter = 1 then 2
when calculated son = 1 and calculated daughter = 1 then 3
else .
end as nch
from people as a
where age >= 65;
quit;
"案例"表达就像"如果那么"在常规SAS。主查询是针对表人的,给出了一个"别名"一个。括号中的select语句称为子查询。他们使用别名b和c查询表人。他们的子查询是检查天气存在男性(或女性),其中序列在b(或c)中等于序列,而pernum等于b(或c)中的momloc或poploc。
以下是数据步骤解决方案:
data test;
set people;
where age >= 65;
son = 0;
daughter = 0;
do i = 1 to nobs;
set people (keep=serial momloc poploc sex age
rename=(serial=serial1 momloc=momloc1 poploc=poploc1 sex=sex1 age=age1))
point=i nobs=nobs;
if sex1 = 'M' and serial=serial1 and (pernum = momloc1 or pernum = poploc1)
then son = 1;
if sex1 = 'F' and serial=serial1 and (pernum = momloc1 or pernum = poploc1)
then daughter = 1;
end;
if son = 1 and daughter = 0 then nch = 1;
else if son = 0 and daughter = 1 then nch = 2;
else if son = 1 and daughter = 1 then nch = 3;
else nch = .;
drop serial1 momloc1 poploc1 sex1 age1 son daughter;
run;
这里的数据步骤是循环通过集合" people"一次只记录一条记录,并且对于每条记录,它循环遍历每条记录相同的数据集" people"。 "则Nobs"是"人"和"点"中的观察数量。是与每条记录对应的数字。