Arrays SAS:Do循环和数组
我有个人记录的数据集,按家庭分类Arrays SAS:Do循环和数组,arrays,loops,sas,Arrays,Loops,Sas,我有个人记录的数据集,按家庭分类 序列号:家庭编号,同一家庭的成员具有相同的序列号 人数:该住户的人数 Pernum:分配给每个家庭中每个人的唯一编号 MOMLOC:显示此人的母亲在家庭中的人(按pernum),0表示家庭中没有此人的母亲 每个人的父亲都一样 SPLOC:适用于每个人的配偶 关系:个人与户主的关系,1=户主,2=配偶,3=子女,4=其他 目标是创建一个新的变量,称为NCH,如果一个人65岁或65岁以上,并且 1表示与儿子生活在一起 2表示与女儿生活在一起 3表示与儿子和女儿生
- 序列号:家庭编号,同一家庭的成员具有相同的序列号
- 人数:该住户的人数
- Pernum:分配给每个家庭中每个人的唯一编号
- MOMLOC:显示此人的母亲在家庭中的人(按pernum),0表示家庭中没有此人的母亲
- 每个人的父亲都一样
- SPLOC:适用于每个人的配偶
- 关系:个人与户主的关系,1=户主,2=配偶,3=子女,4=其他
我认为一个带有do循环的数组应该能够实现这一点,但我在SAS中很少有这样做的经验。有人知道怎么做吗 我提出了两个解决方案,一个使用proc-sql,另一个使用data-step。以下是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;
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;
“case”表达式类似于常规SAS中的“if-then”。主查询针对表people,该表的“别名”为a。括号中的select语句称为子查询。他们查询别名为b和c的表people。它们的子查询是检查是否存在一个男性(或女性),其中a中的序列等于b(或c)中的序列,a中的pernum等于b(或c)中的momloc或poploc
以下是数据步骤解决方案:
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;
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;
在这里,数据步骤一次循环通过一组“人”记录,对于每个记录,它循环通过同一数据集“人”的每个记录。“Nobs”是“people”中的观察次数,“point”是每个记录对应的次数。1。这似乎是PROC SQL的完美案例,而不是数据步骤。2.究竟应该如何从你描述的数据中确定孩子的性别?这个问题太宽泛了。试着自己解决这个问题,问问自己是否对它的某些方面有疑问。Stack Overflow不是“为你做你的工作”的网站。对不起,性别和年龄也包括在内。Joe,好吧,我想具体的问题是,你如何告诉SAS重复做一个动作,通过家庭序列,同时将一个人的MOMLOC或POPLOC与家庭中妈妈或爸爸的PERNUM匹配。我想这可能是一个介绍性问题,但很明显,我把它贴在这里是因为我没有人问。因为数据集太大,所以运行起来需要很长时间(几个小时),但我对它进行了细分,并尝试了代码,结果非常好。非常感谢你!