【问题标题】:creating variables to capture high and low scores using first. and last. in SAS使用 first 创建变量以捕获高分和低分。最后。在 SAS
【发布时间】:2018-02-12 10:04:15
【问题描述】:

考虑以下列出玩家得分的示例

data scores;
input player$ score;
cards;
    A   22
    A   26
    A   38
    B   22
    B   58
    B   60
;
run;

基本上,我想创建两个变量,分别称为“highscore”和“lowscore”,用于捕捉每个玩家的最高和最低分。 例如,玩家 A 的高分是 38,玩家 B 的低分是 22。

我尝试了以下代码

data highlow;
set scores;
lowscore=last.score;
highscore=first.score;
run;

它创建了两个变量,但输出不是我想要的。相反,它会为每个观察结果列出 0。

如何在 SAS 中使用 first. 和 last. 命令创建这两个变量

【问题讨论】:

  • 你想产生什么输出?您希望每个玩家只进行一次观察吗?

标签: sas subset


【解决方案1】:

您需要为 SAS 使用 by 语句来创建 first. 和 last. 变量。但它们不采用 by 变量的值,它们只是布尔标志,用于指示您是否在第一次(或最后一次)观察 BY 变量的这个特定值。

如果您只想找到最小值和最大值,并且您的值已经排序,那么这很简单。

data want ;
  set scores;
  by player score ;
  if first.player then lowscore=score ;
  retain lowscore ;
  if last.player ;
  highscore=score ;
  keep player lowscore highscore ;
run;

请注意,您需要保留 lowscore 变量,以便在数据步移至下一个观察值时不会清除在第一个观察值上设置的值。您可以通过在 SET 语句周围使用 DO 循环来避免对 RETAIN 的需要。

data want ;
  do until (last.player);
    set scores;
    by player score ;
    if first.player then lowscore=score ;
    highscore=score ;
  end;
  keep player lowscore highscore ;
run;

如果数据是按玩家排序的,而不是按玩家和得分排序的,那么您需要添加更多逻辑来查找最小值和最大值。

data want ;
  do until (last.player);
    set scores;
    by player ;
    lowscore=min(lowscore,score);
    highscore=max(highscore,score);
  end;
  keep player lowscore highscore ;
run;

如果您还想保留所有原始观察结果,请添加另一个 DO 循环以重新读取数据并输出详细信息行。

data want ;
  do until (last.player);
    set scores;
    by player ;
    lowscore=min(lowscore,score);
    highscore=max(highscore,score);
  end;
  do until (last.player);
    set scores;
    by player ;
    output;
  end;
run;

【讨论】:

    【解决方案2】:

    proc sql 的方法。

    保留所有记录:

    proc sql;
       select *, min(score) as lowscore,max(score) as highscore from scores group by player;
    quit;
    

    保持低分和高分:

     proc sql;
           select player, min(score) as lowscore,max(score) as highscore from scores group by player;
     quit;
    

    【讨论】:

      【解决方案3】:

      首先。最后。仅存储值为 1 或 0 的真或假,即特定值是 first.variable 还是 last.variable。你可以先用。最后。真假逻辑,然后赋值。您必须执行此逻辑来分配值 if first.player then lowscore =score; 有关详细说明,请参见下面的链接。

      http://documentation.sas.com/?docsetId=lrcon&docsetTarget=n01a08zkzy5igbn173zjz82zsi1s.htm&docsetVersion=9.4&locale=en 下面的代码将实现你想要做的事情

      data scores;
      input player$ score;
      cards;
      A   22
      A   26
      A   38
      B   22
      B   58
      B   60
      ;
      run;
      
      proc sort data = scores;
      by player score ;
      run;
      
      data scores1;
      set scores;
      by player score ;
      retain lowscore;
      if first.player then lowscore =score;
      if last.player then highscore = score;
      if last.player then output;
      drop score;
      run;
      
      data scores_final;
      merge  scores scores1;
      by player;
      run;
      

      【讨论】:

        【解决方案4】:

        您需要先按播放器对数据集进行排序。然后当你设置它时,你应该按播放器添加。您的代码缺少“By player;”线。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-05
          • 2020-04-30
          相关资源
          最近更新 更多