Date: Wed, 2 Aug 2006 12:32:46 -0400
Reply-To: "Howard Schreier <hs AT dc-sug DOT org>" <nospam@HOWLES.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Howard Schreier <hs AT dc-sug DOT org>" <nospam@HOWLES.COM>
Subject: Re: How to tell if a dataset has been sorted
On Wed, 2 Aug 2006 08:12:23 -0700, Ken Karan <posible88-sswug@YAHOO.COM> wrote:
>The problem with the sortedby indicator is it is often not preserved once a
>dataset has been "re-set" into another data step.
It's also possible for it to be present but incorrect, since it can be
manipulated via PROC DATASETS.
>A more resource intensive
>method but more sure fire method would be:
> infile datalines;
> input var1 @@;
>1 3 2
> set _last_;
> old1 = lag(var1);
> if var1 < old1 then do;
> put 'Note: Input dataset data1 not sorted by var1. Halting datastep.';
>If you had more than one sort variable, you could concatenate the sort
>variables into one "key" and check that in the lag function.
Ken may be working too hard. BY processing will do the same thing. For example:
892 data _null_;
893 set sashelp.class;
894 by age;
ERROR: BY variables are not properly sorted on data set SASHELP.CLASS.
Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 FIRST.Age=1 LAST.Age=1 _ERROR_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set SASHELP.CLASS.
However, the BY spec can also be satisfied with an index. It seems to me
that Ken's approach is one way to prevent getting a false confirmation that way.