数据集的排序顺序

发布于 2024-07-15 00:09:49 字数 31 浏览 5 评论 0原文

SAS中有没有固有的方法来查找数据集的排序顺序?

Is there any inherent method in SAS to find the sort order of a dataset?

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

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

发布评论

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

评论(5

还不是爱你 2024-07-22 00:09:53

如果 sas 没有对数据进行排序,但您认为它可能已排序,您可以尝试像已排序一样处理它,并处理可能会或可能不会发生的错误。

* This works, swap some values to see how an error looks;
data foo;
input height;
cards;
1
2
3
4
;
run;

data _null_;
set foo;
by height;
run;

可以在宏中检测和重置错误状态,但这种方法可能会变得混乱。 更多信息请此处

If sas didn't sort the data but you think it might be sorted, you can try to process it as though it's sorted and deal with the errors that may or may not occur as a result.

* This works, swap some values to see how an error looks;
data foo;
input height;
cards;
1
2
3
4
;
run;

data _null_;
set foo;
by height;
run;

Error states can be detected and reset in macro, but that approach is likely to get messy. More info on that here

峩卟喜欢 2024-07-22 00:09:52

我假设您正在解决的问题是尝试确定数据集是否需要以编程方式进行排序。 我们使用的最佳解决方案是在有疑问时简单地使用 proc sort。

当然,您可能会说,这是开销和处理。是的,但是如果数据集已经正确排序,则 proc sort 会知道这一点,并让您的代码以最少的处理继续进行。 它内置了“如果已排序则继续”逻辑。

如果这不是您想要解决的问题,请详细说明,我们会看看是否可以提供帮助。

I'm assuming that the problem you are solving is trying to determine whether a dataset needs to be sorted in a programmatic manner. The best solution we've used is to simply use proc sort when in doubt.

Of course you might say, that's overhead and processing.. Well yes, but if the dataset is already sorted correctly, proc sort with know it and let your code move on with minimal processing. It has the "if sorted then move on" logic built in.

If this isn't the problem you are trying to solve, elaborate and we'll see if we can help.

掐死时间 2024-07-22 00:09:52

您可以使用 Attrc 函数。

文档位于 http://support.sas.com/ onlinedoc/913/getDoc/en/lrdict.hlp/a000147794.htm

它类似于以下内容

data _null_;
   dsid=open("work.a","i");
   sortby=attrc(dsid,"SORTEDBY");
   put  sortby=;
   rc=close(dsid);
run;

You can use the Attrc function.

Docs at http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000147794.htm

It goes something like the following

data _null_;
   dsid=open("work.a","i");
   sortby=attrc(dsid,"SORTEDBY");
   put  sortby=;
   rc=close(dsid);
run;
傲鸠 2024-07-22 00:09:51

如果数据确实已被“排序”,那么是的。 在 proc 内容生成的输出的底部有一个关于排序信息的小表。

使用已按某种排序顺序的数据构建的数据集可能没有附加此信息,您将需要开始探索数据以确定其顺序。

If the data has indeed been "sorted", yes. There is a small table concerning sort information at the bottom of the output produced by proc contents.

Datasets that have been built with data that was already in some kind of sort order may not have this information attached to them and you will need to begin to explore the data to determine its order.

得不到的就毁灭 2024-07-22 00:09:50

正如 Sassy 所说,SAS 知道数据集是否已排序的唯一方法是它是否进行了排序,或者您是否明确告诉它排序顺序。 如果您没有执行这些步骤中的任何一个,它将不知道数据是否按任何类型的顺序排列。

我喜欢 AFHood 尝试对其进行排序的想法。 如果 SAS 知道它是这样排序的,它只会告诉您并且不会再这样做。

注意:输入数据集已排序,未排序 
  

这里有一些研究数据排序的其他想法......享受吧。

如果你只想手动查看,可以使用 proc content data=libname.data;run; 并查看输出。 有一个属性叫sorted。 如果您使用窗口模式,您可以右键单击资源管理器中的数据集并选择属性,然后单击详细信息选项卡并查看排序依据值。

对于编程测试方法,您可以使用过程内容中的输出数据集。 Sorted 和 Sortedby 列将告诉您数据集是否已排序以及按哪个变量排序。 通过运行下面的代码来尝试一下。

/* In an unsorted data set, proc contents will give missing values
   for the sorted and sortedby columns of its output data */
proc contents data=sashelp.class out=class_contents noprint;run;
proc print data=class_contents;
  var memname name sorted sortedby;
run;

/* Now sort and observe the changes in the sorted and sortedby columns */
proc sort data=sashelp.class out=class_sorted; by name;run;
proc contents data=class_sorted out=class_sorted_contents;run;
proc print data=class_sorted_contents;
  var memname name sorted sortedby;
run;`enter code here`

As Sassy says, the only way SAS knows if a data set is sorted is if it did the sorting, or if you explicitly tell it the sort order. If you haven't done either of these steps, it will have no idea if the data is in any type of order.

I like AFHood's idea of just trying to sort it. If SAS knows it is sorted that way it will just tell you and won't do it again.

NOTE: Input data set is already sorted, no sorting done

Here are some other ideas for investigating data sorting...Enjoy.

If you just want to look at it manually, you can use proc contents data=libname.data;run; and look at the output. There is an attribute called sorted. If you are using the windowing mode you can right click on the data set in the explorer and choose properties, then click the details tab and see the sortedby values.

For a programmatic testing approach, you can use an output data set from proc contents. The sorted and sortedby columns will tell you if the data set is sorted and which variable it is sorted by. Try it by running the code below.

/* In an unsorted data set, proc contents will give missing values
   for the sorted and sortedby columns of its output data */
proc contents data=sashelp.class out=class_contents noprint;run;
proc print data=class_contents;
  var memname name sorted sortedby;
run;

/* Now sort and observe the changes in the sorted and sortedby columns */
proc sort data=sashelp.class out=class_sorted; by name;run;
proc contents data=class_sorted out=class_sorted_contents;run;
proc print data=class_sorted_contents;
  var memname name sorted sortedby;
run;`enter code here`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文