<?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 tabulate order of rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763226#M241698</link>
    <description>&lt;P&gt;Yes. make a format like :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format ;
  value $x    
    '(1) other text' ='       (1) other_text'
    '(2) more text'  ='    (2) more_text'
    '(11) some text' ='  (11) some_text' ;
run;

...........
format var $x32.;&lt;/PRE&gt;
&lt;P&gt;Alternative way is just like John King (data _null_) said.&lt;/P&gt;
&lt;PRE&gt;proc format ;
  value $x  (notsorted)
    '(1) other text' ='(1) other_text'
    '(2) more text'  ='(2) more_text'
    '(11) some text' ='(11) some_text' ;
run;

...........
order=data
......
format var $x.;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Aug 2021 12:52:42 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-08-23T12:52:42Z</dc:date>
    <item>
      <title>Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763131#M241659</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I run proc tabulate with two-way structure (2 categorical variables: Z1 ,Z2).&lt;/P&gt;
&lt;P&gt;I want to control the order of variable Z1.&lt;/P&gt;
&lt;P&gt;The order that I get is&lt;/P&gt;
&lt;P&gt;(1) Unknown&lt;/P&gt;
&lt;P&gt;(10) 400-500&lt;/P&gt;
&lt;P&gt;(11) 500+&lt;/P&gt;
&lt;P&gt;(2) 0&lt;/P&gt;
&lt;P&gt;(3) 0-20&lt;/P&gt;
&lt;P&gt;(4) 20-50&lt;/P&gt;
&lt;P&gt;(5) 50-100&lt;/P&gt;
&lt;P&gt;(6) 100-150&lt;/P&gt;
&lt;P&gt;(7) 150-200&lt;/P&gt;
&lt;P&gt;(8) 200-300&lt;/P&gt;
&lt;P&gt;(9) 300-400&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The order that I want is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(1) Unknown&lt;BR /&gt;(2) 0&lt;BR /&gt;(3) 0-20&lt;BR /&gt;(4) 20-50&lt;BR /&gt;(5) 50-100&lt;BR /&gt;(6) 100-150&lt;BR /&gt;(7) 150-200&lt;BR /&gt;(8) 200-300&lt;BR /&gt;(9) 300-400&lt;/P&gt;
&lt;P&gt;(10) 400-500&lt;BR /&gt;(11) 500+&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What is the way to do it please?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I have tried all options:&amp;nbsp;&amp;nbsp;ORDER=INTERNAL&amp;nbsp; &amp;nbsp;&amp;nbsp;ORDER=FORMATTED&amp;nbsp; &amp;nbsp; &amp;nbsp;ORDER=UNFORMATTED&amp;nbsp;&lt;EM&gt;ORDER&lt;/EM&gt;=FREQ&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;but nothing helped to get desired result.&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=Have s=[just=c] out=Want;
label month_diff='Number of Months';
class Z1  Z2 /missing order=UNFORMATTED;
var lak;
table Z1='', (Z2=''*CustomerID=''*n='' all='TOTAL');
run;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 08:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763131#M241659</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-22T08:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763140#M241663</link>
      <description>&lt;PRE&gt;Did you try " order=data " ?

Or padding white blanks before it .Like :

               (1) Unknown
          (2) 0
      (3) 0-20
(4) 20-50&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Aug 2021 10:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763140#M241663</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-22T10:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763143#M241665</link>
      <description>&lt;P&gt;Also Order=Data&amp;nbsp; is not getting desired result.&lt;/P&gt;
&lt;P&gt;The categories already built in and I cannot change it (I get the data set as it is).&lt;/P&gt;
&lt;P&gt;What is the way to solve it?&lt;/P&gt;
&lt;P&gt;Maybe via proc informat?&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 10:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763143#M241665</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-22T10:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763144#M241666</link>
      <description>&lt;P&gt;The idea of putting (1) and (2) and (3) and so on is unnecessary. If you have set up your categories by creating custom formats 0-20 and 20-50 and 50-100 and so on, then you can have the categories you want, and they will sort in order using ORDER=INTERNAL (except for the UNKNOWN category, unless those are really missing values)&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763144#M241666</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-22T11:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763147#M241667</link>
      <description>&lt;P&gt;Thanks but still as I said the data set was not created by me and I need to live with its structure and its categories names.&lt;/P&gt;
&lt;P&gt;The question is to to order it using proc tabulate using these categories names&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:12:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763147#M241667</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-22T11:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763148#M241668</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks but still as I said the data set was not created by me and I need to live with its structure and its categories names.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Absolutely not, you can still remove the (1) and (2) and so on, and then apply your own custom formats. Or are you saying that the raw data for Z1 and Z2 is not included?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And for anyone else reading along, do not start your category names with (1) and (2) and so on&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:22:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763148#M241668</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-22T11:22:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763149#M241669</link>
      <description>&lt;P&gt;I am looking for solution without changing categories names, thanks&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763149#M241669</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-08-22T11:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763150#M241670</link>
      <description>&lt;P&gt;I don't think you'll find an &lt;STRONG&gt;&lt;EM&gt;easy and better&lt;/EM&gt; &lt;/STRONG&gt;solution without using the original numeric values. The category names are character, they will sort alphabetically. If the values were numeric, you could make them sort numerically, which seems to be what you want.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 11:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763150#M241670</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-22T11:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763153#M241672</link>
      <description>&lt;P&gt;You need to add the NOTSORTED option to your format.&amp;nbsp; Then use the PRELOADFMT and ORDER=DATA options in PROC TABULATE.&amp;nbsp; If you include some useable sample data we can make example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 13:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763153#M241672</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-08-22T13:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763155#M241673</link>
      <description>&lt;P&gt;Since Z1 (and Z2?) are character variables, they will default to lexicographic order&amp;nbsp; (i.e.&amp;nbsp; '11' falls between '1" and '2').&amp;nbsp; &amp;nbsp;Now if you were able to sort your dataset such that '1' is followed by '2' and later on by '11', then order = data would work.&amp;nbsp; But you also have Z2, so you would need to order your data considering every combination of Z1 and Z2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Order=Formatted won't work because the lexicographic ordering of the formatted values would reproduce the problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;&amp;nbsp;has identified a solution:&amp;nbsp; (1) make a format using the "notsorted" option, (2) call that format in the proc tabulate, &lt;EM&gt;&lt;STRONG&gt;but use order=data and the preloadfmt options&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; Very nice.&amp;nbsp; I had never bothered to learn that preloadfmt modifies how order=data would work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I got curious, so here's an example using one variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length Z1 $15;
  do Z1='(11) some text','(2) more text','(1) other text';
    output;
  end;
run;

proc print data=have;
  title "Original Unformatted Values, in DATASET order";
run;

proc format ;
  value $x  (notsorted)
    '(1) other text' ='(1) other_text'
    '(2) more text'  ='(2) more_text'
    '(11) some text' ='(11) some_text' ;
run;

proc print data=have;
  title "Formatted Values, in DATASET order";
  format Z1 $x. ;
run;

proc tabulate data=have order=data ;
  class Z1 / preloadfmt ;
  tables Z1,n;
  format Z1 $x.;
  title "TABULATE with PRELOADFMT and ORDER=DATA, using a NOTSORTED format";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, the real solution to this problem is to use internal numeric values with formats specified to show the labels you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the formatted value has an underscore replace on of the blanks, so that I could be certain I was seeing formatted values instead of original values.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 14:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763155#M241673</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-08-22T14:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763159#M241674</link>
      <description>&lt;P&gt;This becomes an easy problem if you are willing to do a little programming.&amp;nbsp; The three steps:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Add a new variable that indicates the proper order&lt;/LI&gt;
&lt;LI&gt;Format the new variable so it prints the way you want to see it.&lt;/LI&gt;
&lt;LI&gt;Run PROC TABULATE using your new variable.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If this would be acceptable as a solution, here is what it would look&amp;nbsp; like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if z1="(1) Unknown" then newvar=1;
   else if z1="(2) 0" then newvar=2;
   else if z1="(3) 0-20" then newvar=3;
   else if z1="(4) 20-50" then newvar=4;
   else if z1="(5) 50-100" then newvar=5;
   else if z1="(6) 100-150" then newvar=6;
   else if z1="(7) 150-200" then newvar=7;
   else if z1=="(8) 200-300" then newvar=8;
   else if z1="(9) 300-400" then newvar=9;
   else if z1="(10) 400-500" then newvar=10;
   else if z1="(11) 500+" then newvar=11;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   value relabel  1="(1) Unknown"
     2="(2) 0"
     3="(3) 0-20"
     4="(4) 20-50"
     5="(5) 50-100"
     6="(6) 100-150"
     7="(7) 150-200"
     8="(8) 200-300"
     9="(9) 300-400"
     10="(10) 400-500"
     11="(11) 500+"
      ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 3:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=want s=[just=c] out=Want_final;
label month_diff='Number of Months';
class newvar  Z2 /missing order=internal;
var lak;
table newvar='', (Z2=''*CustomerID=''*n='' all='TOTAL');
format newvar relabel.;
run;    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's not clear why you want an output data set from PROC TABULATE, but it was in your original program so I left it in the program.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Aug 2021 15:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763159#M241674</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-08-22T15:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763226#M241698</link>
      <description>&lt;P&gt;Yes. make a format like :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format ;
  value $x    
    '(1) other text' ='       (1) other_text'
    '(2) more text'  ='    (2) more_text'
    '(11) some text' ='  (11) some_text' ;
run;

...........
format var $x32.;&lt;/PRE&gt;
&lt;P&gt;Alternative way is just like John King (data _null_) said.&lt;/P&gt;
&lt;PRE&gt;proc format ;
  value $x  (notsorted)
    '(1) other text' ='(1) other_text'
    '(2) more text'  ='(2) more_text'
    '(11) some text' ='(11) some_text' ;
run;

...........
order=data
......
format var $x.;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Aug 2021 12:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763226#M241698</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-23T12:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Proc tabulate order of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763272#M241712</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks but still as I said the data set was not created by me and I need to live with its structure and its categories names.&lt;/P&gt;
&lt;P&gt;The question is to to order it using proc tabulate using these categories names&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If that is organization policy then that is one thing, and then the responsibility for providing structures that will allow the desired output is on them.&lt;/P&gt;
&lt;P&gt;However, what happens in creating a report should not matter if the result is as desired and can be maintained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many people spend lots of time creating character variables without realizing that sort orders can be critical for later output and either make them incorrectly for the desired reporting purpose or realizing that single numeric values, such as a score range are better done with a format.&lt;/P&gt;
&lt;P&gt;With a basic numeric format and the MISSING option for the class variable to insure the Unknown (as missing) is included in the output that order would be trivial with a format. Plus remember that when the user comes back and want to see "what happens if we break the values at 25, 175 and 350 then the only change that would be needed to the Proc tabulate code would be to use a different format. When you bodge together a character range then you &lt;STRONG&gt;must&lt;/STRONG&gt; modify the data to add a new variable and change all the code to use the new variable. Where with a format the new report table could be changed by modifying a statement like:&lt;/P&gt;
&lt;P&gt;Format score Scorefmt.;&lt;/P&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;P&gt;Format score ScoreAlternateFmt.;&lt;/P&gt;
&lt;P&gt;Plus the boundaries are likely going to be much easier to set in Proc Format than an ugly If/then/else block in a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS. Not a good idea to ask questions about something using &lt;STRONG&gt;DATA=FORMATTED &lt;/STRONG&gt;if you do not actually provide the definition of the format.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Aug 2021 15:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-tabulate-order-of-rows/m-p/763272#M241712</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-23T15:34:33Z</dc:date>
    </item>
  </channel>
</rss>

