【问题标题】:SAS What if Analysis data interval with gapsSAS 如果分析数据区间有间隙怎么办
【发布时间】:2016-07-24 09:06:40
【问题描述】:

我对 SAS what if 语句有疑问。假设我们有以下程序:

data work.stress;
infile tests;
input ID Name $ Tolerance;
if tolerance <= 5 then tolerance = "Low";
else if tolerance >= 6 then tolerance = "High";
run;

我知道我们应该始终确保数据间隔之间没有间隙,但只要说这是我们正在运行的程序,如果有一个观测值的容差为 5.5,该怎么办?输出会显示语法错误吗?

谢谢!

JessX

【问题讨论】:

    标签: sas


    【解决方案1】:

    当使用if-then-else 时,不会因未能适合任何特定的 if/else 语句而导致语法错误;它只是没有做任何事情。因此,在您的情况下,tolerance 如果其值为 5.5,则将具有其原始值。这通常在您想要审查极值的情况下完成,尽管您通常会使用特殊的缺失值(假设您希望从分析中排除这些极值):

    data work.stress;
      infile tests;
      input ID Name $ Tolerance;
      if tolerance <= 5 then tolerance = .L;
      else if tolerance >= 6 then tolerance = .H;
    run;
    

    (事实上,在您编写它时,它将缺少“高”“低”组的值,因为您将其定义为数值。)

    如果您使用select-when,那么您必须考虑每一个可能的值 - 如果您没有otherwise,它会在when 未涵盖的任何值上失败。这就是使用select-when 很有帮助的部分原因。

    【讨论】:

      【解决方案2】:

      会有错误,但不是因为你提到的原因。如果您尝试用字符串替换数字,SAS 将不允许这样做。

      两种可能的途径:

      1. 使用您指定阈值的proc format 声明格式。介于阈值之间的值将按原样显示(数字)。
      2. 创建一个新变量。

      在此示例中,介于 5 和 6 之间的任何值都将缺少新的 tolerance 变量的值,除非您添加“MID”类别:

      data work.stress;
        infile tests(rename=(Tolerance=Tol_num));
        input ID Name $ Tolerance;
        format tolerance $8.;
        if tol_num = . then tolerance = "?";
        else if tol_num <= 5 then tolerance = "LOW";
        else if tol_num >= 6 then tolerance = "HIGH";
        else tolerance = "MID";
        drop tol_num;
      run;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 2015-06-01
        • 1970-01-01
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多