SAS 数据步骤 - 来自 table 的动态 select 字段

SAS data step - dynamic select field from table

我对一个案例没有什么问题。 请查看 table 有。 想要。 我的过程:从 VAR 中获取值,除以 12,四舍五入到最接近的整数,然后将 WANT table 放入 FIELD&"result of the action" 的 RES 字段值中。 我该怎么做? 我的最终 table 有几千条记录,这些字段有百 - 两种。

DATA HAVE;
infile DATALINES dsd missover;
input ID VAR FIELD1 FIELD2 FIELD3 FIELD4;
CARDS;
1, 10, 1, 6, 5, 6
2, 20, 1, 6, 8, 8
3, 30, 5, 8, 12, 7
4, 40, 5, 9, 5, 6
5, 50, 8, 10, 12, 8
;run;

DATA WANT;
infile DATALINES dsd missover;
input ID RES;
CARDS;
1, 1
2, 6
3, 12
4, 6
5, 8
;run;

谢谢!

您似乎想使用 ARRAY 语句。这将允许您通过数组中的索引引用变量。

data want;
  set have ;
  array f field1-field4;
  index=ciel(var/12);
  if 1 <= index <= dim(f) then res=f[index];
run;

排列变量并适当计算索引。如果您有数百个类似的字段,则可以使用多个变量数组。

data want;
  set have;
  array costs cost_year1-cost_year4;
  array plans plan_year1-plan_year4;

  res_costs = costs[ceil(var/12)];
  res_plans = plans[ceil(var/12)];
run;

如果 var 值超出范围 (<0 or > 40),我不会检查索引计算并让步骤失败。 ERROR: 会告诉您数据值不符合预期的 数据模型 并且需要对策略或数据收集进行一些更正。