【问题标题】:How to do simple mathematics with min/max variable in SAS如何在 SAS 中使用 min/max 变量做简单的数学运算
【发布时间】:2015-03-21 03:22:14
【问题描述】:

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

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

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

【问题讨论】:

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

标签: sas


【解决方案1】:

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

执行此操作的“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;

【讨论】:

    【解决方案2】:

    另一种 SAS 方法是使用 PROC MEANS(或 PROC SUMMARY 或您选择的替代方案)创建最大/最小表并将其合并。不需要 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;
    

    如果您有一个类语句 - 即,像“按状态”或类似的分组 - 将其添加到 proc means 中,然后通过您的类变量执行 merge 而不是 want 中的第二组。它需要一个排序的(初始)数据集来合并。


    您还可以选择在SAS-IML 中执行此操作,其工作方式与您在上面的想法更相似。 IML 是 SAS 交互式矩阵语言,更类似于 rmatlab,而不是 SAS 基础语言。

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      相关资源
      最近更新 更多