代码之家  ›  专栏  ›  技术社区  ›  Banjer

如何在SAS中对数据集进行排序以使记录交错?

sas
  •  1
  • Banjer  · 技术社区  · 14 年前

    假设我有一个数据集:

    data animals;
       input animal  $
             group   $
             control $;
    datalines;
    dog A c1
    dog B c1
    dog C c1
    dog D c2
    dog E c2
    dog F c2
    dog G c3
    dog H c3
    dog I c3
    ;
    run;
    

    我希望以这样的方式对结果数据集进行排序:

    dog A c1
    dog D c2
    dog G c3
    dog B c1
    dog E c2
    dog H c3
    dog C c1
    dog F c2
    dog I c3
    

    我没有看到任何特殊的proc排序选项可以进行“交替”排序,所以我可能需要“通过控制”对数据集进行子集,然后在数据步骤中重新组合,使它们交错/交替。

    有什么想法吗?谢谢。

    1 回复  |  直到 14 年前
        1
  •  7
  •   Lauren Samuels    14 年前
    proc sort data= animals out= animals2;
        by control group;
    run;
    
    data animals2;
        set animals2;
        by control;
        retain orderWithinControlType;
        if first.control then orderWithinControlType = 1;
        else orderWithinControlType +1;
    run;
    
    proc sort data= animals2 out= animals3;
        by orderWithinControlType control;
    run;
    
    proc print data= animals3;
    run;
    
    推荐文章