如何在SAS中使用最小/最大变量进行简单数学运算

如何在SAS中使用最小/最大变量进行简单数学运算,sas,Sas,我目前正在SAS中运行一个宏代码,我想计算最大值和最小值。现在我的代码行是: hhincscaled = 100*(hhinc - min(hhinc) )/ (max(hhinc) - min(hhinc)); hhvaluescaled = 100*(hhvalue - min(hhvalue))/ (max(hhvalue) - min(hhvalue)); 我想做的是用下面的计算重新衡量家庭收入和价值变量。我试图减去每个变量的最小值,然后从相应的最大值中减去它,然后通过乘以100来缩放

我目前正在SAS中运行一个宏代码,我想计算最大值和最小值。现在我的代码行是:

hhincscaled = 100*(hhinc - min(hhinc) )/ (max(hhinc) - min(hhinc));
hhvaluescaled = 100*(hhvalue - min(hhvalue))/ (max(hhvalue) - min(hhvalue));

我想做的是用下面的计算重新衡量家庭收入和价值变量。我试图减去每个变量的最小值,然后从相应的最大值中减去它,然后通过乘以100来缩放它。我不确定这是否是正确的方法,或者SAS是否以我想要的方式识别代码。

我假设您正在执行数据步骤。数据步骤在数据集中的记录上有一个隐式循环。您只能访问当前循环的记录(有些例外)

“SAS”方法是计算最小值和最大值,然后将它们添加到数据集中

Proc sql noprint;
create table want as
select *,
       min(hhinc) as min_hhinc,
       max(hhinc) as max_hhinc,
       min(hhvalue) as min_hhvalue,
       max(hhvalue) as max_hhvalue
from have;
quit;

data want;
set want;
hhincscaled = 100*(hhinc - min_hhinc )/ (max_hhinc - min_hhinc);
hhvaluescaled = 100*(hhvalue - min_hhvalue)/ (max_hhvalue - min_hhvalue);

/*Delete this if you want to keep the min max*/
drop min_: max_:;
run;

另一种SAS方法是使用
PROC MEANS
(或
PROC SUMMARY
或您选择的备选方案)创建max/min表,并将其合并。不需要SQL知识,速度可能也差不多

proc means data=have;
  *use a class value if you have one;
  var hhinc hhvalue;
  output out=minmax min= max= /autoname;
run;

data want;
  if _n_=1 then set minmax;  *get the min/max values- they will be retained automatically and available on every row;
  set have;
  *do your calculations, using the new variables hhinc_max hhinc_min etc.;
run;
如果您有一个class语句,例如“按状态”或类似的分组,请在
proc means
中添加该语句,然后根据类变量执行
merge
,而不是
want
中的第二个集合。合并时需要一个已排序(初始)的数据集



您还可以在
SAS-IML
中执行此操作,其工作方式与您在上面的思考方式更为相似。IML是SAS交互式矩阵语言,与SAS基础语言相比,它更类似于
r
matlab

我不熟悉这种标准化方法,但您也应该看看proc标准和proc标准。