我有一个问题,试图分配一个值的范围内的变量组合。
我现在编写的代码如下所示:
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个变量,我必须编写每一个组合,并给他们所有的分数。同样,代码不是一个问题,但它没有效率,而且很难判断你是否错过了一个组合。我正在研究如何使用数组,但我只想说,数组有点让我害怕。如果有人能提供任何帮助,如何获得一个整洁和有效的解决办法,我将非常感激。感谢您的阅读。
发布于 2018-01-18 15:40:33
理查德是对的。查找表是明确的解决方案。如果出于某种原因,您坚持让这些分数硬编码,并寻找更容易阅读的语法,您可以使用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;发布于 2018-01-18 15:30:52
有了这么大的评分要求,创建一个现有组合的查找表并添加分数列比硬编码if语句要好得多。
对于103个值(Y/N/Blank)变量,您需要处理3**10 (或59,049)组合。我可不想对此进行编码!
步骤1.获得实际组合
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 ...
;步骤2.编辑score_lookup_table
为查找表中的每一行输入一个得分值。您可以使用SAS表编辑器(查看表)。如果您正在做大量的复制和粘贴,导出到Excel并在完成后重新导入。
步骤3.使用查找表
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的缺失分数。这些将是新的组合,因为你上次更新的分数查找。
如果评分机制是基于规则的,则可能会使用一种评分算法来代替。
https://stackoverflow.com/questions/48322668
复制相似问题