<?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: Data Not in Alphabetic or Numeric order in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717651#M221960</link>
    <description>&lt;P&gt;You can use it when you want to treat the data as grouped by that variable, but the values are not either monotonically increasing nor monotonically decreasing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One useful place to use this is when you have data with some type of STATUS variable that is recorded at various points in time.&amp;nbsp; If you wanted to collapse to distinct contiguous intervals of the same status you could sort by the DATETIME variable but process it by the STATUS variable.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data intervals ;
  set have ;
  by id status notsorted ;
  if first.status then start=datetime;
  retain start ;
  if last.staus;
  stop=datetime;
  keep id status start stop;
  format start stop datetime19. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Feb 2021 17:05:24 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-02-08T17:05:24Z</dc:date>
    <item>
      <title>Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717638#M221955</link>
      <description>&lt;P&gt;So I'm reading this (below) in the documentation...&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&lt;FONT color="#3366FF"&gt;In BY-group processing, you can use data that is arranged in an order other than alphabetic or numeric, such as by calendar month or by category. To do this, use the NOTSORTED option in a BY statement when you use a SET statement. The NOTSORTED option in the BY statement tells SAS that the data is not in alphabetic or numeric order, but that it is arranged in groups by the values of the BY variable. You cannot use the NOTSORTED option with the MERGE statement, the UPDATE statement, or when the SET statement lists more than one data set.&lt;/FONT&gt;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&lt;FONT color="#000000"&gt;I'm trying to wrap my head around the idea of when to use NOTSORTED.&lt;/FONT&gt;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&lt;FONT color="#000000"&gt;So, is NOTSORTED literally only used when you haven't sorted your data-set so you are indicating that the values themselves dictate grouping not the order?&lt;/FONT&gt;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 16:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717638#M221955</guid>
      <dc:creator>edasdfasdfasdfa</dc:creator>
      <dc:date>2021-02-08T16:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717645#M221958</link>
      <description>&lt;P&gt;When you are analyzing data via some PROC, and the &lt;EM&gt;data is grouped together by some variable&lt;/EM&gt; (this is a requirement), but not sorted by that variable, then the BY statement with the NOTSORTED option works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;product       cost
red              10
red               20
red               11
blue              22
blue              19
green            7
green            8&lt;/PRE&gt;
&lt;P&gt;Then you can use NOTSORTED in the BY statement in whatever PROC to get the results for RED and the results for BLUE and the results for GREEN. (Of course, you could sort it and then not worry about it, but if you have a large data set, NOTSORTED will work faster).&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 16:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717645#M221958</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-02-08T16:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717648#M221959</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/201537"&gt;@edasdfasdfasdfa&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a $ x y;
cards;
A 2 21
A 2 22
B 1 11
C 3 31
C 3 32
C 3 33
D 2 23
D 2 24
;

data want1;
set have;
by a;
if last.a;
run;

data want2;
set have;
by x notsorted;
if last.x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The results of the last two DATA steps are equal, but &lt;EM&gt;different&lt;/EM&gt;&amp;nbsp;(not only regarding sort order) from what you would get if you sorted dataset HAVE by X first and &lt;EM&gt;then&lt;/EM&gt; created dataset WANT2 (with or without the &lt;FONT face="courier new,courier"&gt;notsorted&lt;/FONT&gt; option). Do you see why?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 16:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717648#M221959</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-08T16:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717651#M221960</link>
      <description>&lt;P&gt;You can use it when you want to treat the data as grouped by that variable, but the values are not either monotonically increasing nor monotonically decreasing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One useful place to use this is when you have data with some type of STATUS variable that is recorded at various points in time.&amp;nbsp; If you wanted to collapse to distinct contiguous intervals of the same status you could sort by the DATETIME variable but process it by the STATUS variable.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data intervals ;
  set have ;
  by id status notsorted ;
  if first.status then start=datetime;
  retain start ;
  if last.staus;
  stop=datetime;
  keep id status start stop;
  format start stop datetime19. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 17:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717651#M221960</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-08T17:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717677#M221974</link>
      <description>&lt;P&gt;I guess the fact that the grouping is different for each DATA STEP?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 17:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717677#M221974</guid>
      <dc:creator>edasdfasdfasdfa</dc:creator>
      <dc:date>2021-02-08T17:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: Data Not in Alphabetic or Numeric order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717697#M221981</link>
      <description>&lt;P&gt;With the original dataset HAVE the grouping in the "&lt;FONT face="courier new,courier"&gt;data want1&lt;/FONT&gt;" and "&lt;FONT face="courier new,courier"&gt;data want2&lt;/FONT&gt;" steps is the same because variables &lt;FONT face="courier new,courier" size="4"&gt;a&lt;/FONT&gt; and &lt;FONT face="courier new,courier" size="4"&gt;x&lt;/FONT&gt; change their values in exactly the same observations. After sorting HAVE by &lt;FONT face="courier new,courier" size="4"&gt;x&lt;/FONT&gt;, however, the two formerly separated BY groups with &lt;FONT face="courier new,courier" size="4"&gt;a='A'&lt;/FONT&gt;&amp;nbsp;and &lt;FONT face="courier new,courier" size="4"&gt;a='D'&lt;/FONT&gt;&amp;nbsp;form a single BY group with respect to variable &lt;FONT face="courier new,courier" size="4"&gt;x&lt;/FONT&gt;&amp;nbsp;(four consecutive observations with &lt;FONT face="courier new,courier" size="4"&gt;x=2&lt;/FONT&gt;). Hence the "&lt;FONT face="courier new,courier"&gt;data want2&lt;/FONT&gt;" step would select only one observation (with &lt;FONT face="courier new,courier"&gt;y=24&lt;/FONT&gt;) from that new group, not two (&lt;FONT face="courier new,courier"&gt;y=22&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;y=24&lt;/FONT&gt;) as before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if the original grouping is what you want, &lt;EM&gt;but no variable like &lt;/EM&gt;&lt;FONT face="courier new,courier" size="4"&gt;a&lt;/FONT&gt;&lt;EM&gt;&amp;nbsp;exists&lt;/EM&gt;, then you're in a situation where the NOTSORTED option (applied to &lt;FONT face="courier new,courier" size="4"&gt;x&lt;/FONT&gt;) comes to the rescue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 18:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Not-in-Alphabetic-or-Numeric-order/m-p/717697#M221981</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-02-08T18:40:15Z</dc:date>
    </item>
  </channel>
</rss>

