<?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: Validating sort order without metadata or proc sort in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665086#M78987</link>
    <description>&lt;P&gt;Are you talking about data in &lt;STRONG&gt;your&lt;/STRONG&gt; SAS system or residing in someone else's? Or data extracted from your data sets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide the source of the information that Proc contents or dictionary tables can't report the sort status correctly. My understanding is that information is in the data set "header" information and should be there regardless of "who" created the data.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jun 2020 17:29:31 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-25T17:29:31Z</dc:date>
    <item>
      <title>Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665083#M78986</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi folks - long time listener, first time caller.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Quick background: I work in a distributed environment where a number of stakeholders take their proprietary data and translate it into a common data model. We have minimal input on how the stakeholders write their ETL code, we are concerned with evaluating compliance to the common data model. One requirement is to verify the datasets are sorted as specified.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;1. Since we don't create the datasets to be evaluated, we (so I have read) can't trust that the metadata (proc contents or dictionary tables) would accurately capture sort information.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. These are huge tables we are evaluating. If I tried a proc sort with the presorted option to verify the sort order, and the data is not already sorted, I run the risk of sorting the data: resource intensive and NOT what I want. I only want to verify whether it is sorted so I can notify the stakeholder that they need to sort the data.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;proc sort data = in.ds &lt;EM&gt;presorted&lt;/EM&gt; out = out.ds;&lt;BR /&gt;by varA varB varC;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;I'm looking for other suggestions outside of these. Can the sortedby= data step option be used to verify sort order, or is it only for creating sort order?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks much!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665083#M78986</guid>
      <dc:creator>jvigeant</dc:creator>
      <dc:date>2020-06-25T17:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665086#M78987</link>
      <description>&lt;P&gt;Are you talking about data in &lt;STRONG&gt;your&lt;/STRONG&gt; SAS system or residing in someone else's? Or data extracted from your data sets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide the source of the information that Proc contents or dictionary tables can't report the sort status correctly. My understanding is that information is in the data set "header" information and should be there regardless of "who" created the data.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665086#M78987</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-25T17:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665087#M78988</link>
      <description>&lt;P&gt;If Proc contents not an option, how about a dummy data step to test the desired sort order?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set sashelp.prdsale;
	by year product;
run;&lt;BR /&gt;&lt;BR /&gt;ERROR: BY variables are not properly sorted on data set SASHELP.PRDSALE.&lt;BR /&gt;ACTUAL=$561.00 PREDICT=$979.00 COUNTRY=CANADA REGION=EAST DIVISION=EDUCATION PRODTYPE=FURNITURE&lt;BR /&gt;PRODUCT=SOFA QUARTER=4 YEAR=1994 MONTH=Dec FIRST.YEAR=0 LAST.YEAR=0 FIRST.PRODUCT=0 LAST.PRODUCT=0&lt;BR /&gt;_ERROR_=1 _N_=24&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: There were 25 observations read from the data set SASHELP.PRDSALE.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The step would fail at the first time, and you would know for sure whether or not the data is sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just a thought,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665087#M78988</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-25T17:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665088#M78989</link>
      <description>&lt;P&gt;Run a data _null_ step on the dataset with the same BY statement. It will fail if the sort order is not correct.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665088#M78989</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T17:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665090#M78990</link>
      <description>&lt;P&gt;The &lt;A href="https://documentation.sas.com/?docsetId=lesysoptsref&amp;amp;docsetTarget=n0cdfgq2lsgve4n13cflndvlte98.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;sortvalidate system option&lt;/A&gt; can be used for this purpose. With this option (which is not the default) the sort procedure effectively sorts the data only if a sequence check fails.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665090#M78990</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-06-25T17:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665102#M78991</link>
      <description>&lt;P&gt;I'll refer to the following thread as my very informal source:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Accessing-checking-sort-order-in-a-dataset/m-p/29860#M7020" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Accessing-checking-sort-order-in-a-dataset/m-p/29860#M7020&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue of who creates the data only factors in because I don't know how it is created; there is no standard for the various stakeholders other than the results have pass a compliance audit that I am writing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 17:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665102#M78991</guid>
      <dc:creator>jvigeant</dc:creator>
      <dc:date>2020-06-25T17:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665115#M78992</link>
      <description>&lt;P&gt;This is a really good approach... I need to flesh it out a little bit regarding capturing the error information as a variable and applying it to downstream logic, but I think the payoff is that it gives a very instantaneous answer without much overhead.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 18:20:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665115#M78992</guid>
      <dc:creator>jvigeant</dc:creator>
      <dc:date>2020-06-25T18:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665207#M78996</link>
      <description>&lt;P&gt;Agreed that this is a good approach ... here are a couple of tools you might want to add to the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After each DATA _NULL_ step, use macro language to capture the value of &amp;amp;SYSERR and write a message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have the process working for one data set, set the global option OPTIONS NOSYNTAXCHECK;&lt;/P&gt;
&lt;P&gt;That way, your program can continue with checking another data set even if an error was encountered because of an unsorted data set.&lt;/P&gt;
&lt;P&gt;Always check your logic thoroughly before using this option!.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 00:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665207#M78996</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-06-26T00:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665208#M78997</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/277879"&gt;@jvigeant&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will&amp;nbsp; need to check the value of these two system macro variables to check for Error Status &amp;amp; Error Text&lt;/P&gt;
&lt;DIV class="sasSource"&gt;%put &amp;amp;=SYSERR;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;%put &amp;amp;=SYSERRORTEXT;&lt;/DIV&gt;</description>
      <pubDate>Fri, 26 Jun 2020 00:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665208#M78997</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2020-06-26T00:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Validating sort order without metadata or proc sort</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665220#M78998</link>
      <description>&lt;P&gt;You can trust SAS metadata (the descriptor portion of a SAS data set) IF the sort is also validated.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593137316724.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46680iC95C70D65B856BA1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593137316724.png" alt="Patrick_0-1593137316724.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;It is possible though that a data set is sorted without metadata stored that it's sorted (table &lt;EM&gt;sort2&lt;/EM&gt; in below sample code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd be using a check via metadata first as this will perform much better than anything else.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below some sample code for you to build upon.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data 
  cheat:  Not sorted with Metadata for Sorted but not Validated
  sort:   Sorted with Metadata for Sorted and Validated
  sort2:  Sorted but no Metadata
*/
data cheat(sortedby=id name) sort;
  set sashelp.class;
  id=ceil(ranuni(1)*100);
run;
proc sort data=sort out=sort;
  by id name;
run;
data sort2;
  set sort;
run;


%macro CheckSort(ds,sortvars);
  proc contents 
    data=&amp;amp;ds 
    out =_check(keep=libname memname name sorted sortedby)
    noprint;
  run;quit;

  %local sortvars_meta;
  %let sortvars_meta=;
  proc sql ;
    select upcase(name) into :sortvars_meta separated by ' '
    from _check
    where 
      sorted=1 /* 1 if sort validated */
      and not missing(sortedby) /* sequence of sort vars */
    order by sortedby
    ;
  quit;

  %if &amp;amp;sortvars_meta = %upcase(&amp;amp;sortvars) %then %put DS &amp;amp;DS is sorted;
  %else 
    %do;
      %put Verify sort order for DS &amp;amp;DS;
      data _null_;
        set &amp;amp;ds(keep=&amp;amp;sortvars);
        by &amp;amp;sortvars;
      run;
    %end;

%mend;

%CheckSort(sort,id name);
%CheckSort(sort2,id name);
%CheckSort(cheat,id name);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If time then consider to implement the data _null_ check step in a way that allows you to capture/reset the error if the source table isn't properly sorted so that it doesn't impact on further tests.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 02:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Validating-sort-order-without-metadata-or-proc-sort/m-p/665220#M78998</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-26T02:24:45Z</dc:date>
    </item>
  </channel>
</rss>

