<?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: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2. in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935392#M45009</link>
    <description>&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&amp;nbsp; 9806666&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&amp;nbsp; &amp;nbsp;8909383&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trans ids are different&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jul 2024 19:21:55 GMT</pubDate>
    <dc:creator>bhca60</dc:creator>
    <dc:date>2024-07-10T19:21:55Z</dc:date>
    <item>
      <title>ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935358#M45004</link>
      <description>&lt;P&gt;I keep getting the following error:&lt;/P&gt;
&lt;DIV id="sasLogError1_1720625738347" class="sasError"&gt;ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;I pull my data then do a sort by the key and a couple other variables then try to pick the first input_dttm. The key can have multiple input_dttm values so maybe this is why it's happening.&amp;nbsp; How do I pick the latest input_dttm for each key? Any help is greatly appreciated.&amp;nbsp; Here is my log:&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 
83    data H_A_CASE (keep=key seq_id create_dttm admit_dt dschg_dt ntfy_dttm status_cd);
84    set rda.H_A_CASE_V;
85    where (datepart(create_dttm) between &amp;amp;start. and &amp;amp;end.) and status_cd not in ('5' '6') ;
SYMBOLGEN:  Macro variable START resolves to '01JUL2023'd
SYMBOLGEN:  Macro variable END resolves to '06JUL2024'd
86    run;
 

NOTE: There were 19264049 observations read from the data set rda.H_A_CASE_V.
      WHERE (DATEPART(create_dttm)&amp;gt;='01JUL2023'D and DATEPART(create_dttm)&amp;lt;='06JUL2024'D) and status_cd not in ('5', '6');
NOTE: The data set WORK.H_A_CASE has 19264049 observations and 7 variables.
NOTE: Compressing data set WORK.H_A_CASE increased size by 10.95 percent. 
      Compressed is 23540 pages; un-compressed would require 21216 pages.

      Block Output Operations           0
      
87    
88    
89    data H_A_TRANS_COMM (keep=key trans_id input_dttm);
90    set rda.H_A_TRANS_COMM_V ;
91    where (datepart(input_dttm) between &amp;amp;start. and &amp;amp;end.) ;
SYMBOLGEN:  Macro variable START resolves to '01JUL2023'd
SYMBOLGEN:  Macro variable END resolves to '06JUL2024'd
92    run;
 
 
NOTE: There were 152633956 observations read from the data set RDA.H_A_TRANS_COMM_V.
      WHERE (DATEPART(input_dttm)&amp;gt;='01JUL2023'D and DATEPART(input_dttm)&amp;lt;='06JUL2024'D);
NOTE: The data set WORK.H_A_TRANS_COMM has 152633956 observations and 3 variables.
NOTE: Compressing data set WORK.H_A_TRANS_COMM increased size by 31.91 percent. 
      Compressed is 123366 pages; un-compressed would require 93526 pages.

93    
94    
95    proc sort data=H_A_TRANS_COMM out=H_A_TRANS_COMM2;
96     by key trans_id descending input_dttm;
97    run;
NOTE: There were 152633956 observations read from the data set WORK.H_A_TRANS_COMM.
NOTE: SAS threaded sort was used.
NOTE: The data set WORK.H_A_TRANS_COMM2 has 152633956 observations and 3 variables.
NOTE: Compressing data set WORK.H_A_TRANS_COMM2 increased size by 31.91 percent. 
      Compressed is 123366 pages; un-compressed would require 93526 pages.
NOTE: PROCEDURE SORT used (Total process time):

98    
99    /*want max input_dttm*/
100   data out.trans_comm ;
101   set H_A_TRANS_COMM2 ;
102   by key trans_id input_dttm;
103   if first.input_dttm;
104  
105   run;
ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.
KEY=H-123456789 INPUT_DTTM=04OCT2023:23:42:53 TRANS_ID=. FIRST.KEY=0 LAST.KEY=1
FIRST.INPUT_DTTM=1 LAST.INPUT_DTTM=1 _ERROR_=1 _N_=4
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 16:24:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935358#M45004</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2024-07-10T16:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935363#M45005</link>
      <description>&lt;P&gt;These two BY statements are not the same&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by key trans_id descending input_dttm; /* As seen in PROC SORT */
by key trans_id input_dttm; /* As seen in the DATA step */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 16:43:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935363#M45005</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-07-10T16:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935368#M45006</link>
      <description>&lt;P&gt;Thank you. How would I pick the first input_dttm for each key?&amp;nbsp; Because I still get multiple input_dttm values listed for the same key values when I just need the latest for each key.&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 16:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935368#M45006</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2024-07-10T16:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935370#M45007</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you. How would I pick the first input_dttm for each key?&amp;nbsp; Because I still get multiple input_dttm values listed for the same key values when I just need the latest for each key.&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What happened to TRANS_ID? Can you provide more details about what you are asking for?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 17:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935370#M45007</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-07-10T17:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935374#M45008</link>
      <description>&lt;P&gt;Not clear what you are asking for but the code you posted was NOT picking the first datetime value.&amp;nbsp; It was making sure the datetime values where unique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the maximum datetime value, within some other set of key variables, then do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by a b datetime_var;
run;
data want;
  set have;
  by a b datetime_var;
  if last.b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you get one observation per A*B combination and it has the maximum value of DATETIME_VAR.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 17:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935374#M45008</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-10T17:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935392#M45009</link>
      <description>&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&amp;nbsp; 9806666&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&amp;nbsp; &amp;nbsp;8909383&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trans ids are different&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 19:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935392#M45009</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2024-07-10T19:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935397#M45010</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&amp;nbsp; 9806666&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&amp;nbsp; &amp;nbsp;8909383&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trans ids are different&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And so&amp;nbsp; WHAT does that mean in terms of your actual problem?&lt;/P&gt;
&lt;P&gt;Do you want the last observation per KEY value?&amp;nbsp; If so do you want the values sorted only be the datetime value?&amp;nbsp; Or do you want them sorted by the TRANS_ID value?&amp;nbsp; Or both? If both which takes precedence in the ordering?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a little dataset where it makes a difference which variables you sort by.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input key $ input_dttm :datetime. trans_id $ ;
  format input_dttm datetime19.;
cards;
A 03JUL2023:12:00:33 ZZZZ
A 06JUL2023:11:09:45 AAAA
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's try it both ways and see how the results differ.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by key input_dttm trans_id;
run;

data test1;
  set have;
  by key input_dttm trans_id;
  if last.key;
run;

proc sort data=have;
  by key trans_id input_dttm;
run;

data test2;
  set have;
  by key trans_id input_dttm;
  if last.key;
run;

proc compare data=test1 compare=test2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1720640568987.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/98305iF653F785E99157BE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1720640568987.png" alt="Tom_0-1720640568987.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So which observation do you want to select??&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 19:43:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935397#M45010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-10T19:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935398#M45011</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 03JUL2023 12:00:33&amp;nbsp; 9806666&lt;/P&gt;
&lt;P&gt;123456789&amp;nbsp; &amp;nbsp; 06JUL2023 11:09:45&amp;nbsp; &amp;nbsp;8909383&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trans ids are different&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp; I specifically asked in my previous post "Can you provide more details about what you are asking for?" but you didn't answer that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So where does that leave us? You asked a question about sorting which was answered. Then you went ahead and gave us a different problem with the same (or similar) data, that has nothing to do with your sorting problem, and you gave us a minimal description of this different problem. It's essentially a new problem, please take some time to describe the problem fully and describe in detail what you want.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 20:58:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935398#M45011</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-07-10T20:58:22Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR: BY variables are not properly sorted on data set WORK.H_A_TRANS_COMM2.</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935404#M45013</link>
      <description>Yes, I am trying to get the latest dttm for each key and remove the dttm values that are earlier. &lt;BR /&gt;HAVE:&lt;BR /&gt;key                  trans_id     input_dttm&lt;BR /&gt;123456789    435986     03JUL2024: 12:13:30&lt;BR /&gt;123456789    567943     06JUL2024:11:14:58&lt;BR /&gt;WANT:&lt;BR /&gt;key                   trans_id         input_dttm&lt;BR /&gt;123456789    567943   06JUL2024:11:14:58</description>
      <pubDate>Thu, 11 Jul 2024 00:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/ERROR-BY-variables-are-not-properly-sorted-on-data-set-WORK-H-A/m-p/935404#M45013</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2024-07-11T00:56:58Z</dc:date>
    </item>
  </channel>
</rss>

