Arrays SAS中大量变量的评分组合
我在尝试为一系列变量组合赋值时遇到了问题 我现在正在编写的代码如下所示:Arrays SAS中大量变量的评分组合,arrays,sas,Arrays,Sas,我在尝试为一系列变量组合赋值时遇到了问题 我现在正在编写的代码如下所示: if ai_hr_tat_flag = "Y" and ai_hr_flag = "N" and ai_wtp_flag = "N" and ai_prof_flag = "N" then score= "4" ; else ai_hr_tat_flag = "Y" and ai_hr_flag = "Y" and ai_wtp_flag = "N" and ai_prof_flag = "N" th
if ai_hr_tat_flag = "Y" and ai_hr_flag = "N" and ai_wtp_flag = "N" and ai_prof_flag = "N"
then score= "4" ;
else ai_hr_tat_flag = "Y" and ai_hr_flag = "Y" and ai_wtp_flag = "N" and ai_prof_flag = "N"
then score = "4" ;
else ai_hr_tat_flag = "N" and ai_hr_flag = "Y" and ai_wtp_flag = "Y" and ai_prof_flag = "N"
then score = "4" ;
else ai_hr_tat_flag = "" and ai_hr_flag = "Y" and ai_wtp_flag = "N" and ai_prof_flag = "Y"
then score = "5" ;
代码确实有效,但我面临的问题是,实际上有10个变量,我必须对每个组合进行编码,并给出所有分数。同样,编码不是问题,但效率不高,而且很难判断是否遗漏了一个组合。
我在考虑使用数组,但让我们说数组让我有点害怕。
如果有人能提供任何帮助,以获得一个整洁和有效的解决方案,我将不胜感激。
感谢阅读。对于如此大的评分要求,您最好创建一个现有组合的查找表,并添加一个评分列,而不是硬编码if语句 对于10个三值(Y/N/空白)变量,您需要处理3**10(或59049)个组合。我可不想给它编代码 第一步。得到实际的组合
proc sql;
create table score_lookup_table as
select
ai_hr_tat_flag
, ai_hr_falg
, ai_wtp_flag
, ai_prof_flag
, ... the six other flags ...
, count(*) as combination_count
, 0 as score
from have
group by
ai_hr_tat_flag
, ai_hr_falg
, ai_wtp_flag
, ai_prof_flag
, ... the six other flags ...
;
第二步。编辑分数查找表
为查找表中的每一行输入分数值。您可以使用SAS表格编辑器(查看表格)。如果要进行大量复制和粘贴,请导出到Excel,完成后重新导入
第三步。使用查找表
proc sort data=have out=want; by ...10 flag variables...;
proc sort data=score_lookup_table; by ...10 flag variables...;
data want;
merge want score_lookup_table;
by ...10 flag variables...;
run;
第四步
检查want
是否缺少分数。这些将是自上次更新分数查找以来的新组合
如果评分机制是基于规则的,则可能会使用评分算法来代替。理查德是对的。查找表是明确的解决方案。如果出于某种原因,您坚持将这些分数硬编码,并寻找更易于阅读的语法,您可以使用
select
语句
data have;
do ai_hr_tat_flag = 'Y','N',' ';
do ai_hr_flag = 'Y','N',' ';
do ai_wtp_flag = 'Y','N',' ';
do ai_prof_flag = 'Y','N',' ';
output;
end;
end;
end;
end;
run;
data want;
set have;
select (ai_hr_tat_flag || ai_hr_flag || ai_wtp_flag || ai_prof_flag);
when ('YNNN') score = '4';
when ('YYNN') score = '4';
when ('NYYN') score = '4';
when (' YNY') score = '5';
otherwise; /*this allows you checking if all combinations have been found. E.g. otherwise abort; terminates execution if a combination is missing*/
end;
run;
非常感谢Richard和Petr的回答,他们都做得非常好。我真的很感谢你在这方面的帮助。我将在我的代码中保留这两个版本,因为它们也可以用于其他事情:)再次感谢
when('YNNN','YYNN','NYYN')score='4'我认为代码>是一种更简单的方法。