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

发布于 2024-10-03 17:12:18 字数 460 浏览 3 评论 0原文

假设我有一个数据集:

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

我没有看到任何可以进行“交替”排序的过程排序的特殊选项,所以我可能不得不将我的数据集“通过控制”进行子集化,然后以交错/交替的方式在数据步骤中重新组合。

有什么想法吗?谢谢。

Suppose I have a dataset:

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;

I would like that to be sorted in such a way that the resulting dataset looks like:

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

I don't see any special options for proc sort that would do an "alternating" sort, so I will probably have to subset my dataset "BY control", then recombine in a data step in such a way that they interleave/alternate.

Any ideas? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-10-10 17:12:18
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文