我有1个数据集,其中包含学校内容的内容描述:
num description
content1 math
content2 spanish
content3 geography
content4 chemistry
content5 history
在另一个数据集(学生)中,我具有数组content1-content5,并且我使用一个标志来指示每个学生都有的内容。
学生
name age content1 content2 content3 content4 content5
BOB 15 1 1 1 1
BRYA 16
CARL 15 1 1
SUE 17 1 1 1
LOU 15 1
如果我使用这样的代码:
data students1;
set students;
array content[5];
format allcontents $100.;
do i=1 to dim(content);
if content[i]=1 then do;
allcontents=cat(vname(content[i]),',',allcontents);
end;
end;
run;
结果是:
name age content1 content2 content3 content4 content5 allcontents
BOB 15 1 1 1 1 content1,content2,content3,content5,
BRYA 16
CARL 15 1 1 content2,content5,
SUE 17 1 1 1 content3,content4,content5,
LOU 15 1 content5
1)我想使用查找表(数据集内容)的名称来使用内容的名称,而不是变量allcontents中content [1-5]的数组名称。我怎样才能做到这一点?
2)及更高版本,我想要按内容描述而不是按学生描述的结果,像这样:
description name age
math BOB 15
spanish BOB 15
geography BOB 15
history BOB 15
spanish CARL 15
history CARL 15
spanish SUE 17
chemistry SUE 17
history SUE 17
history LOU 15
是否有可能?
谢谢。
首先,从这篇文章中获取%create_hash()宏。
使用哈希表查找值。
data students1;
set students
array content[5];
format num $32. description $16.;
if _n_ = 1 then do;
%create_hash(cnt,num,description,"contents");
end;
do i=1 to 5;
if content[i]=1 then do;
num = vname(content[i]);
rc = cnt.find();
output;
end;
end;
keep description name age;
run;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句