<?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: How to sort numeric variables based on parts of the values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639370#M190207</link>
    <description>&lt;P&gt;This should do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x @@;
datalines;
185 186 285 286 385 390
;

proc sql;
   create table want as
   select x from have
   order by mod(x, 100),
           int(x / 100);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 12 Apr 2020 21:45:32 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-04-12T21:45:32Z</dc:date>
    <item>
      <title>How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639364#M190202</link>
      <description>&lt;P&gt;I have a data set made up of many 3 digit values which represent dates, where the first digit is the term in a school year (Assuming there are only 6 terms in a year) and the 2nd two digits are the year: 185 = 1st semester of 1985:&lt;/P&gt;&lt;P&gt;185, 186, 285, 286, 385, 390, etc.&lt;/P&gt;&lt;P&gt;I want to sort them by the last 2 digits of each number (year), then the first digit (semester):&lt;/P&gt;&lt;P&gt;185, 285, 385, 186, 286, 390, etc.&lt;/P&gt;&lt;P&gt;How can I sort the data/dates to be ordered like that?&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 21:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639364#M190202</guid>
      <dc:creator>Greg_P</dc:creator>
      <dc:date>2020-04-12T21:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639368#M190205</link>
      <description>&lt;P&gt;Are these numeric or character values?&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 21:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639368#M190205</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-12T21:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639369#M190206</link>
      <description>&lt;P&gt;Numeric&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 21:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639369#M190206</guid>
      <dc:creator>Greg_P</dc:creator>
      <dc:date>2020-04-12T21:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639370#M190207</link>
      <description>&lt;P&gt;This should do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x @@;
datalines;
185 186 285 286 385 390
;

proc sql;
   create table want as
   select x from have
   order by mod(x, 100),
           int(x / 100);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Apr 2020 21:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639370#M190207</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-12T21:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639372#M190209</link>
      <description>&lt;P&gt;You can use Proc SQL to reorder data according to a custom sort key&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;&amp;nbsp;sort them by the last 2 digits of each number (year), then the first digit (semester) "&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In this example the ORDER BY uses:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;first key: mod(number,100) to compute the yy part&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;second key: the number itself -- no need to perform int(number/100) because that computation would be the same order as the number&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;data have;
  do semester = 1 to 4;
  do year = 1985 to 2020;
    num = semester * 100 + mod(year,100);
    output;
  end;
  end;
  keep num;
run;

proc sql;
  create table have as
  select * from have
  order by mod(num,100), num
  ;
quit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Apr 2020 21:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639372#M190209</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-04-12T21:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639375#M190211</link>
      <description>&lt;P&gt;To avoid a return of the Y2K problem, you may want to add a suitable shift to the values, e.g.&lt;/P&gt;
&lt;PRE&gt;order by mod(x&lt;FONT color="#00CCFF"&gt;&lt;STRONG&gt;+50&lt;/STRONG&gt;&lt;/FONT&gt;,100), x&lt;/PRE&gt;
&lt;P&gt;for a date range from 1950 through 2049.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 22:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639375#M190211</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-04-12T22:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to sort numeric variables based on parts of the values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639397#M190226</link>
      <description>&lt;P&gt;Why not first re-code the values into something that is both easier to read and easier to sort?&lt;/P&gt;
&lt;P&gt;So if you old variable was named NUM you could make a new variable called CHAR.&amp;nbsp; So take the last two digits and convert it into an actual year (always represent years with four digits to avoid confusion).&amp;nbsp; Then you can append the first digit as a suffix.&amp;nbsp; So now the values should sort and be less confusing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input num @@;
cards;
185 186 285 286 385 390
;

data want;
  set have ;
  length char $6 ;
  year = mod(num,100);
  if year &amp;lt; 30 then year=2000+year;
  else year=1900+year;
  char=catx('-',year,int(NUM/100));
  drop year;
run;

proc sort;
  by char;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    num     char

 1     185    1985-1
 2     285    1985-2
 3     385    1985-3
 4     186    1986-1
 5     286    1986-2
 6     390    1990-3&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Apr 2020 03:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-numeric-variables-based-on-parts-of-the-values/m-p/639397#M190226</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-13T03:28:13Z</dc:date>
    </item>
  </channel>
</rss>

