【问题标题】:Getting next observation per group每组获得下一次观察
【发布时间】:2016-11-29 09:58:09
【问题描述】:

我正在处理 SAS 中的数据集,以获得下一个观察值的分数应该是列 Next_Row_score 的当前观察值。如果没有下一个观察值,则 Next_Row_score 列的当前观察值应为每个组(ID)的“null”。为了更好地说明,我在数据集下面提供了示例:

ID Score 
10 1000 
10 1500 
10 2000 
20 3000 
20 4000 
30 2500 

结果输出应该像 -

ID Salary Next_Row_Salary 
10 1000   1500 
10 1500   2000 
10 2000   . 
20 3000   4000 
20 4000   .  
30 2500   2500  

提前感谢您的帮助。

【问题讨论】:

  • 你有 sas/ets 的许可证吗?

标签: sas analytics


【解决方案1】:
data want(drop=_: flag);
   merge have have(firstobs=2 rename=(ID=_ID Score=_Score));
   if ID=_ID then do;
       Next_Row_Salary=_Score;
       flag+1;
   end;
   else if ID^=_ID and flag>=1 then do;
           Next_Row_Salary=.;
           flag=.;
   end;
   else Next_Row_Salary=score;
run;

【讨论】:

  • 陈盛林,谢谢你的帮助。但是,使用您提供的代码,我只能在 Salary 列中获取 next_row_salary 的值。所以输出不是问题中提供的所需格式。
【解决方案2】:

试试这个:

data have;
  input ID Score;

  datalines;
10 1000 
10 1500 
10 2000 
20 3000 
20 4000 
30 2500 
;
run;

proc sql noprint;
    select count(*)  into :obsHave
    from have;
quit;

data want2(rename=(id1=ID Score1=Salary) drop=ID id2 Score);
    do i=1 to &obsHave;
        set have point=i;
        id1=ID;
        Score1=Score;
        j=i+1;
        set have point=j;
        id2=ID;
        if id1=id2 then do;
            Next_Row_Salary = Score;
        end;
        else Next_Row_Salary=".";
        output;
    end;
stop;
;
run;

【讨论】:

    【解决方案3】:

    有一个更简单(至少在我看来)不涉及循环的 proc sql 方法:

    data have;
      input ID Score;
      datalines;
    10 1000 
    10 1500 
    10 2000 
    20 3000 
    20 4000 
    30 2500 
    ;
    run;
    
    /*count each observation's place in its ID group*/
    data have2;
        set have;
        count + 1;
        by id;
        if first.id then count = 1;
    run;
    
    /*if there is only one ID in a group, keep original score, else lag by 1*/
    proc sql;
        create table want as select distinct
            a.id, a.score,
            case when max(a.count) = 1 then a.score else b.score end as score2
            from have2 as a
            left join have2 (where = (count > 1)) as b
            on a.id = b.id and a.count = b.count - 1
            group by a.id;
    quit;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2016-08-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多