<?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: proc sort num and char by multiple variables and set first and last in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583394#M166075</link>
    <description>&lt;P&gt;Instead of proc sql:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc_flagged;
  set abc;
  by id;
  
  flag = (first.id or last.id);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: This "solution" was posted before&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;&amp;nbsp; added the expected results to the original post. So the data step is somewhat useless. &lt;/P&gt;</description>
    <pubDate>Fri, 23 Aug 2019 05:57:26 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-08-23T05:57:26Z</dc:date>
    <item>
      <title>proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583392#M166073</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which as char and numeric values. I need to sort the data by all of those values and then assign the top most or the last most with a flag.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;acc(char)&lt;/TD&gt;&lt;TD&gt;time&lt;/TD&gt;&lt;TD&gt;money&lt;/TD&gt;&lt;TD&gt;id(char)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1067988&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2702.77&lt;/TD&gt;&lt;TD&gt;7101000002817&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;47339&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;17704.06&lt;/TD&gt;&lt;TD&gt;7101000002601&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90380&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;11164.95&lt;/TD&gt;&lt;TD&gt;7101002302817&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;112740&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;8162.46&lt;/TD&gt;&lt;TD&gt;7101000002026&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;223245&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;20578.68&lt;/TD&gt;&lt;TD&gt;7101002302817&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6431468&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;3418.59&lt;/TD&gt;&lt;TD&gt;7101000002601&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to sort this data first by id, then by descending time, then by descending money and then by descending acc. Then I need to assign the top and bottom acc for each id group by a flag.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code, and it's not sorting correctly. Some of these variables are char and some are num as you can see. Appreciate any help I can get&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA = abc SORTSEQ =LINGUISTIC (NUMERIC_COLLATION=ON);
BY id descending time descending money DESCENDING acc;
RUN;

proc sql;
alter table abc
add flag char(4); 
update abc
set flag = 'YES' where id=first.id;/*I'm pretty sure this is incorrect*/
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Expected output: As you can see we first sort by id, for each id we then sort by desc time, then for each time we sort by desc money and for each money we sort by desc acc value. And the one that is the highest for each id group, we mark them with a flag&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;acc(char)&lt;/TD&gt;&lt;TD&gt;time&lt;/TD&gt;&lt;TD&gt;money&lt;/TD&gt;&lt;TD&gt;id(char)&lt;/TD&gt;&lt;TD&gt;flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;112740&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;8162.46&lt;/TD&gt;&lt;TD&gt;7101000002026&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;47339&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;17704.06&lt;/TD&gt;&lt;TD&gt;7101000002601&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6431468&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;3418.59&lt;/TD&gt;&lt;TD&gt;7101000002601&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1067988&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2702.77&lt;/TD&gt;&lt;TD&gt;7101000002817&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;223245&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;20578.68&lt;/TD&gt;&lt;TD&gt;7101002302817&lt;/TD&gt;&lt;TD&gt;YES&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;90380&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;11164.95&lt;/TD&gt;&lt;TD&gt;7101002302817&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 05:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583392#M166073</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-23T05:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583393#M166074</link>
      <description>&lt;P&gt;So what does your desired result look like given the posted example data?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 05:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583393#M166074</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-23T05:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583394#M166075</link>
      <description>&lt;P&gt;Instead of proc sql:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc_flagged;
  set abc;
  by id;
  
  flag = (first.id or last.id);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: This "solution" was posted before&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;&amp;nbsp; added the expected results to the original post. So the data step is somewhat useless. &lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 05:57:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583394#M166075</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-23T05:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583395#M166076</link>
      <description>thank you for your reply, I added the expected data in the post.</description>
      <pubDate>Fri, 23 Aug 2019 05:55:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583395#M166076</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-23T05:55:21Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583396#M166077</link>
      <description>Thank you for your reply.&lt;BR /&gt;I need to sort by char and numeric values, how do I accomodate that?&lt;BR /&gt;Also my variable flag has a value 'YES' that needs to be assigned. How do I assign that once I identify which is first?</description>
      <pubDate>Fri, 23 Aug 2019 05:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583396#M166077</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-23T05:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583397#M166078</link>
      <description>&lt;P&gt;Ok. Then do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc;
input acc $ time money id :$20.;
datalines;
1067988 5 2702.77 7101000002817
47339 35 17704.06 7101000002601
90380 35 11164.95 7101002302817
112740 5 8162.46 7101000002026
223245 35 20578.68 7101002302817
6431468 5 3418.59 7101000002601
;

proc sort data = abc sortseq=linguistic (numeric_collation=on);
    by id descending time descending money descending acc;
run;

data want;
    set abc;
    by id;
    if first.id then flag='YES';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Aug 2019 05:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583397#M166078</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-23T05:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583398#M166079</link>
      <description>that's exactly how I'm trying to sort data, but it doesn't sort anything except my id.</description>
      <pubDate>Fri, 23 Aug 2019 05:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583398#M166079</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-23T05:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583399#M166080</link>
      <description>&lt;P&gt;It sure does. However with 6 obs there is not much to sort. But it does sort your data by id, by descending time and so on&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 06:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583399#M166080</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-23T06:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sort num and char by multiple variables and set first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583402#M166081</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You're talking about numeric and character variables. However, in your sample output ACC, TIME, and MONEY are all left-justified. Whatever SAS interface you're using to view the data shown here, it's a sure sign that these variables are stored as the character type. Thus in your sort, for example, 47339&amp;gt;1067988, 5&amp;gt;35, and so forth. Yet it surely appears as though you're interested in comparing them using the variables according to the &lt;EM&gt;numeric&lt;/EM&gt; values represented by their digits. Thus, either convert&amp;nbsp;ACC, TIME, and MONEY to the numeric data type using the INPUT function or right-justify them using the RIGHT function before the sorting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, revise what you know about proc SORT incredibly numerous options. I seem to recall that there's an option:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SORTSEQ=LINGUISTIC(NUMERIC_COLLATION=ON)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;that forces&amp;nbsp;a character string to be&amp;nbsp;ordered by the numeric values of integers present in there instead of their character value. An excellent reference to all these nifty proc SORT features are excellently aggregated in the excellent SGF 2018 paper by Derek Morgan:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2773-2018.pdf" target="_blank"&gt;https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2773-2018.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 06:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sort-num-and-char-by-multiple-variables-and-set-first-and/m-p/583402#M166081</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-23T06:49:35Z</dc:date>
    </item>
  </channel>
</rss>

