<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Capturing PRESORTED information from Proc Sort in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756840#M239027</link>
    <description>&lt;P&gt;Much appreciation for the tip. The unfortunate angle though is that the PRESORTED option seems to work even when the metadata is inaccurate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create dummy data for test purposes;
* Disclaimer: all values are fake; 
data test;
length patid encounterid 4.;
infile datalines;
input patid encounterid;
datalines;
12345 876543
24256 987654
14233 263547
42987 763853
;
run;

* sort the data; 
proc sort data = test;
by patid encounterid;
run;

* test the sortedby metadata values;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Create a new data set from the original;
data test2;
set test; 
run;  

* Now look at the metadata again;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test2";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Look at the log output using the presorted option though;
proc sort data = test2 presorted; 
by patid encounterid;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The caveat is that one can sort a data set, but if one used that sorted data to make another data set (in the example, I literally did nothing to test), the sortedby metadata values for the resulting table don't carry over and are reset to 0.&lt;BR /&gt;Unfortunately (or fortunately, because it DOES keep my job interesting) in this distributed database environment in which I work, I don't have a say in (or even knowledge of) the code the remote locations use to create the data sets my QA program evaluates.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Jul 2021 00:13:29 GMT</pubDate>
    <dc:creator>jvigeant</dc:creator>
    <dc:date>2021-07-27T00:13:29Z</dc:date>
    <item>
      <title>Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756789#M239002</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all -&amp;nbsp;&lt;/P&gt;&lt;P&gt;A function of quality assurance production code I maintain is to verify whether or not large stored tables are sorted. To that end I have been using the PRESORTED option in proc sort in conjunction with proc printto to:&lt;BR /&gt;1. Write the procedure results to an external log,&lt;BR /&gt;2. Read the results of the log back into a SAS data set, and&lt;BR /&gt;3. Parse the string looking for the specific text "Sort order of input data set has been verified"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This method works well enough, though I feel it isn't particularly efficient given the myriad platforms upon which this code is run (these are packages we distribute to remote organizations). I have to imagine that "behind the scenes" there has to be some kind of SAS system variable produced when using the PRESORTED option, though I can't find any documentation of it. It would be much preferred to capture an automatic system variable to perform the check rather than creating a volume of extraneous log files and parsing the text to get my result.&lt;BR /&gt;&lt;BR /&gt;Does anyone know if such functionality exists?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 20:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756789#M239002</guid>
      <dc:creator>jvigeant</dc:creator>
      <dc:date>2021-07-26T20:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756800#M239004</link>
      <description>&lt;P&gt;Either the view SASHELP.Vcolumn, or if using Proc SQL, Dictionary.columns has information about which variables are used in sorting in the data set. The variable Sortedby has the order each variable appears on the sort order as numeric. If 0 in the variable then not sorted by that variable and if the set is not sorted then all the values are 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The view SASHELP.Vtable, or Dictionary.tables, has a few variables related to if the data is sorted and which sort sequence is used but not the variable(s) involved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you get into information about indexes that is stored in SASHELP.Vindex or Dictionary.Indexes.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 20:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756800#M239004</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-26T20:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756840#M239027</link>
      <description>&lt;P&gt;Much appreciation for the tip. The unfortunate angle though is that the PRESORTED option seems to work even when the metadata is inaccurate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create dummy data for test purposes;
* Disclaimer: all values are fake; 
data test;
length patid encounterid 4.;
infile datalines;
input patid encounterid;
datalines;
12345 876543
24256 987654
14233 263547
42987 763853
;
run;

* sort the data; 
proc sort data = test;
by patid encounterid;
run;

* test the sortedby metadata values;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Create a new data set from the original;
data test2;
set test; 
run;  

* Now look at the metadata again;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test2";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Look at the log output using the presorted option though;
proc sort data = test2 presorted; 
by patid encounterid;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The caveat is that one can sort a data set, but if one used that sorted data to make another data set (in the example, I literally did nothing to test), the sortedby metadata values for the resulting table don't carry over and are reset to 0.&lt;BR /&gt;Unfortunately (or fortunately, because it DOES keep my job interesting) in this distributed database environment in which I work, I don't have a say in (or even knowledge of) the code the remote locations use to create the data sets my QA program evaluates.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 00:13:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756840#M239027</guid>
      <dc:creator>jvigeant</dc:creator>
      <dc:date>2021-07-27T00:13:29Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756841#M239028</link>
      <description>&lt;P&gt;The &lt;EM&gt;validated&lt;/EM&gt; flag is what matters. See &lt;A href="https://sasnrd.com/sas-sorted-validated-flag/" target="_self"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 00:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756841#M239028</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-27T00:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756842#M239029</link>
      <description>&lt;P&gt;And vote &lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/create-a-VALIDATESORT-data-set-option-to-validate-the-sort-order/idi-p/288038" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 00:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756842#M239029</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-27T00:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756848#M239031</link>
      <description>&lt;P&gt;A couple of additional details to make your life even more interesting ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A DATA step can change the values of&amp;nbsp; BY variables, so that the new data set is no longer in order.&amp;nbsp; Perfectly valid code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create a new data set from the original;
data test2;
set test; 
if patid=. then patid=999;
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the new data set is not in sorted order.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you create a data set, you can specify a known order, such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create a new data set from the original;
data test2 (sortedby=patid encounterid);
set test; 
if patid=. then patid=999;
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the SORTEDBY field reports the data set as being sorted, when the data is actually not sorted.&amp;nbsp; So the VALIDATED field indicates that the SORTEDBY field has not been checked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you later use the data with a BY statement, you will get an error message.&amp;nbsp; And if you attempt to sort it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test2;
   by patid encounterid;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS will skip the PROC SORT, since it sees it as unnecessary.&amp;nbsp; (You will get a note in the log about PROC SORT being skipped.)&amp;nbsp; To get the sort to run, you would need to add the FORCE option on the PROC statement.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 01:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756848#M239031</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-07-27T01:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756852#M239032</link>
      <description>&lt;P&gt;You could do something like this that youi can expect to be consistent across all systems:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; /*Make an unsorted data set*/
  do i=1 to 18,1;
    set sashelp.class;
    output;
  end;
run;

%let presorted=no;
%let indsn=have;
%let byvars=i;


data _null_;
  set &amp;amp;indsn (keep=&amp;amp;byvars)  nobs=nexpected;
  by &amp;amp;byvars;
  if _n_=nexpected then call symput("presorted","yes");
run;

%put &amp;amp;=presorted;

%macro sort;
  %if &amp;amp;presorted=no %then %do;
    %put ***** SORTING dataset &amp;amp;INDSN  by &amp;amp;byvars *****;
    proc sort data=&amp;amp;indsn;
    by &amp;amp;byvars;
    run;
  %end;
  %else %put ***** NO SORT REQUIRED *****;
%mend sort;
%sort;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main point is that the DATA _NULL_ step never gets to the last obs (i.e. _N_ will never equal NEXPECTED) if the dataset is not sorted.&amp;nbsp; Even if is only the last observation that is out of order, the automatic variable _N_ only reaches NEXPECTED-1 (because the BY statement tells SAS to look ahead).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So by defaulting macrovar &amp;amp;PRESORTED to no, and correcting it in the data step when appropriate, this code avoids scanning the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you'll probably want to make the macro coding a bit more complete than above.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 02:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/756852#M239032</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-27T02:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757060#M239040</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/277879"&gt;@jvigeant&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Much appreciation for the tip. The unfortunate angle though is that the PRESORTED option seems to work even when the metadata is inaccurate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create dummy data for test purposes;
* Disclaimer: all values are fake; 
data test;
length patid encounterid 4.;
infile datalines;
input patid encounterid;
datalines;
12345 876543
24256 987654
14233 263547
42987 763853
;
run;

* sort the data; 
proc sort data = test;
by patid encounterid;
run;

* test the sortedby metadata values;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Create a new data set from the original;
data test2;
set test; 
run;  

* Now look at the metadata again;
proc sql noprint;
  select name, sortedby
    into :names separated by " "
       , :sortedby separated by " "
  from dictionary.columns
  where lowcase(libname) = "work" and 
        lowcase(memname) = "test2";
quit;
%put &amp;amp;names &amp;amp;sortedby;

* Look at the log output using the presorted option though;
proc sort data = test2 presorted; 
by patid encounterid;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The caveat is that one can sort a data set, but if one used that sorted data to make another data set (in the example, I literally did nothing to test), the sortedby metadata values for the resulting table don't carry over and are reset to 0.&lt;BR /&gt;Unfortunately (or fortunately, because it DOES keep my job interesting) in this distributed database environment in which I work, I don't have a say in (or even knowledge of) the code the remote locations use to create the data sets my QA program evaluates.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And does your previous Proc Contents approach show test2 as sorted? No it does not. So querying the dictionary table is no less valid than your previous approach. &lt;/P&gt;
&lt;P&gt;SORTED is only set when Proc Sort or Proc SQL Order by has been used on the data set (or possibly other procedures producing output data but your example uses data step).&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 06:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757060#M239040</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-27T06:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757063#M239042</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; SORTED is only set when Proc Sort or Proc SQL Order by has been used on the data set (or possibly other procedures&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Interesting point, I only ever thought about the first two. I'd expect proc means to validate a sort by (type if more than one type is requested and by) class values too (what about when formats are used?). I wonder about the hash output object. I'll try that tomorrow if I find the time.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 06:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757063#M239042</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-27T06:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing PRESORTED information from Proc Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757069#M239047</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt; SORTED is only set when Proc Sort or Proc SQL Order by has been used on the data set (or possibly other procedures&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Interesting point, I only ever thought about the first two. I'd expect proc means to validate a sort by (type if more than one type is requested and by) class values too (what about when formats are used?). I wonder about the hash output object. I'll try that tomorrow if I find the time.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I am not sure that other procedures will set the sorted flag but there are so many that I am not assuming all of them wont set a sorted property. Proc Summary for example with a CLASS statement will not set the sorted flag for proc contents to report but the data will be in "sort" order by _type_ and the class variables in Summary's way.&lt;/P&gt;
&lt;P&gt;Example this does not show sorted in proc summary but the data is in a very specific order equivalent to sort by Sex Age.&lt;/P&gt;
&lt;PRE&gt;Proc summary data=sashelp.class nway;
   class sex age;
   var height;
   output out=work.summary mean= max=/autoname;
run;

Proc contents data=work.summary;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jul 2021 07:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capturing-PRESORTED-information-from-Proc-Sort/m-p/757069#M239047</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-27T07:37:49Z</dc:date>
    </item>
  </channel>
</rss>

