<?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 summary-order of categories is not as I want in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651386#M195441</link>
    <description>&lt;P&gt;Your format with proper use of spaces before the numeric characters is a good example of what I called "jumping through hoops to make a character variable sort as numeric". Although it does work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But anyway, I would not encourage this use of formatting to create character variables, so that they can sort as numeric; it seems like unnecessary effort to me.&lt;/P&gt;</description>
    <pubDate>Thu, 28 May 2020 14:31:29 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-05-28T14:31:29Z</dc:date>
    <item>
      <title>proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651320#M195412</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the following report that is created by proc summary I want to control the order of the categories (levels) of&amp;nbsp; invoice&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;I want that the order will be same as mentioned in format&amp;nbsp;&lt;CODE class=" language-sas"&gt;$InvoiceorderFmt (in&amp;nbsp;right&amp;nbsp;side)&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;what&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&amp;nbsp;please?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value InvoiceFmt
0 &amp;lt;- 1000='0-1000'
1000 &amp;lt;- 5000='1,000-5,000'
5000 &amp;lt;- 10000='5,000-1,0000'
10000 &amp;lt;- 20000='1,0000-2,0000'
20000 &amp;lt;- 50000='20,000-50,000'
50000 &amp;lt;- 100000='50,000-100,000'
100000 &amp;lt;- high='100,000+';
value $InvoiceorderFmt
'0-1000'='1'
'1,000-5,000'='2'
'5,000-1,0000'='3'
'1,0000-2,0000'='4'
'20,000-50,000'='5'
'50,000-100,000'='6'
'100,000+'='7'
;
run;

Data cars;
set sashelp.cars;
Invoice_=put(Invoice,InvoiceFmt.);
run;

proc summary data=cars  nway missing ;
class Type Invoice_ ;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 07:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651320#M195412</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-05-28T07:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651323#M195414</link>
      <description>&lt;P&gt;Please try the completetypes and preloadfmt option&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=cars  nway missing completetypes;
format Invoice InvoiceFmt.;
class Type Invoice / preloadfmt;
var Invoice;
output out=summaryTbl2(drop=_type_) sum= mean= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 May 2020 08:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651323#M195414</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-05-28T08:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651326#M195417</link>
      <description>&lt;P&gt;Is there another way or just these 3 ways that I show here?&lt;/P&gt;
&lt;P&gt;Is it possible to use multilabel in proc format in order to get the desired order of rows in summary table?&lt;/P&gt;
&lt;P&gt;In Way3, Is it essential to use proc sort ?Can you please explain why?&lt;/P&gt;
&lt;P&gt;Can you please explain how proc summary sort the rows by default?&lt;/P&gt;
&lt;P&gt;why adding&amp;nbsp; the format&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;format oorder Invoice4Fmt.&amp;nbsp;helps&amp;nbsp;to&amp;nbsp;sort&amp;nbsp;the&amp;nbsp;rows?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Way1-Define the format in a way that will put the rows in order*/
proc format;
value Invoice1Fmt
0 &amp;lt;- 20000='(a) 0-20,000'
20000 &amp;lt;- 30000='(b) 20,000-30,000'
30000 &amp;lt;- 50000='(c) 30,000-50,000'
50000 &amp;lt;- high='(d) 50,000+';
Run;
Data cars;
set sashelp.cars;
Invoice_=put(Invoice,Invoice1Fmt.);
run;
proc summary data=cars  nway missing ;
class Type Invoice_ ;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;

/*Way2-padding blanks before it*/
proc format;
value Invoice2Fmt
0 &amp;lt;- 20000=    '   0-20,000'
20000 &amp;lt;- 30000='  20,000-30,000'
30000 &amp;lt;- 50000=' 30,000-50,000'
50000 &amp;lt;- high= '50,000+';
Run;
Data cars;
set sashelp.cars;
Invoice_=put(Invoice,Invoice2Fmt.);
run;
proc summary data=cars  nway missing ;
class Type Invoice_ ;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;

/*Way3*/
proc format;
value Invoice3Fmt
0 &amp;lt;- 20000='1'
20000 &amp;lt;- 30000='2'
30000 &amp;lt;- 50000='3'
50000 &amp;lt;- high='4';
value Invoice4Fmt
1='0-20,000'
2='20,000-30,000'
3='30,000-50,000'
4='50,000+';
run;
Data cars;
set sashelp.cars;
oorder =input(put(Invoice,Invoice3Fmt.),2.);
run;
/*proc sort data=cars; by oorder; run;*/
proc summary data=cars  nway missing ;
class Type oorder ;
format oorder Invoice4Fmt.;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 May 2020 08:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651326#M195417</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-05-28T08:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651345#M195427</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;Hello&lt;/P&gt;
&lt;P&gt;In the following report that is created by proc summary I want to control the order of the categories (levels) of&amp;nbsp; invoice&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;I want that the order will be same as mentioned in format&amp;nbsp;&lt;CODE class=" language-sas"&gt;$InvoiceorderFmt (in&amp;nbsp;right&amp;nbsp;side)&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;what&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&amp;nbsp;please?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value InvoiceFmt
0 &amp;lt;- 1000='0-1000'
1000 &amp;lt;- 5000='1,000-5,000'
5000 &amp;lt;- 10000='5,000-1,0000'
10000 &amp;lt;- 20000='1,0000-2,0000'
20000 &amp;lt;- 50000='20,000-50,000'
50000 &amp;lt;- 100000='50,000-100,000'
100000 &amp;lt;- high='100,000+';
value $InvoiceorderFmt
'0-1000'='1'
'1,000-5,000'='2'
'5,000-1,0000'='3'
'1,0000-2,0000'='4'
'20,000-50,000'='5'
'50,000-100,000'='6'
'100,000+'='7'
;
run;

Data cars;
set sashelp.cars;
Invoice_=put(Invoice,InvoiceFmt.);
run;

proc summary data=cars  nway missing ;
class Type Invoice_ ;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&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 you create a character variable INVOICE_, then SAS will use alphabetical order to order the levels. That's how SAS treats character variables, it will always put them in alphabetical order. Since the alphabetical order puts anything starting with a 0 first, followed by all levels starting with 1s next, you get an undesired ordering, one that doesn't make sense for numeric values such as Invoice amount.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you create a NUMERIC variable INVOICE_ (and format it), then SAS keeps the levels in NUMERIC order. That's how SAS treats numeric variables, it will always put them in numerical order (regardless of formatting). Then the first level is 0-1000 and the next level is 1000-5000 and the next level is 5000-10000 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this is a case where you want numeric levels to be in a numeric variable. In fact, its hard to think of a case where you would want numeric 1000 to be converted to character 1000. So don't do it, leave your numeric variables as numerical variables (with whatever formatting you want), and then you won't have to jump through hoops to force character variables to sort as if they were numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data cars;
set sashelp.cars;
Invoice_=Invoice; /* Invoice_ is a numeric variable */
format invoice_ invoicefmt.;
run;

proc summary data=cars  nway missing ;
class Type Invoice_ ;
var Invoice;
output out=summaryTbl(drop=_type_) sum= mean= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 11:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651345#M195427</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-28T11:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651384#M195440</link>
      <description>&lt;P&gt;While &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s solution is likely best the bit about the character values and sort order does offer another solution. Define your format so that the character sort order works as desired. Spaces will sort before numerals, so introducing enough spaces in the right places will change the sort order of the formatted values.&lt;/P&gt;
&lt;PRE&gt;proc format;
value InvoiceFmt
     0 &amp;lt;- 1000   ='  0-1000'
  1000 &amp;lt;- 5000   ='  1,000-5,000'
  5000 &amp;lt;- 10000  ='  5,000-10,000'
 10000 &amp;lt;- 20000  =' 10,000-20,000'
 20000 &amp;lt;- 50000  =' 20,000-50,000'
 50000 &amp;lt;- 100000 =' 50,000-100,000'
100000 &amp;lt;- high   ='100,000+';
run;&lt;/PRE&gt;
&lt;P&gt;Note that your as posted definition for the 5000 to 10000 range didn't really make sense with the comma placements, &lt;STRONG&gt;which will affect sort order also&lt;/STRONG&gt;, so I fixed that above.&lt;/P&gt;
&lt;P&gt;And formatting code helps as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s solution is more flexible as with the two numeric variables you can create new groups with a changed format. Adding a character variable means that you would have to recreate or add an additional character variable if you want to see what the result changes might be with a different grouping.&lt;/P&gt;
&lt;P&gt;Also, the data step does not understand a multilabel format. So adding a character value using multilabel format would only have the highest level of the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 14:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651384#M195440</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-28T14:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651386#M195441</link>
      <description>&lt;P&gt;Your format with proper use of spaces before the numeric characters is a good example of what I called "jumping through hoops to make a character variable sort as numeric". Although it does work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But anyway, I would not encourage this use of formatting to create character variables, so that they can sort as numeric; it seems like unnecessary effort to me.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 14:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651386#M195441</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-28T14:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651392#M195444</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Your format with proper use of spaces before the numeric characters is a good example of what I called "jumping through hoops to make a character variable sort as numeric". Although it does work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But anyway, I would not encourage this use of formatting to create character variables, so that they can sort as numeric; it seems like unnecessary effort to me.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Agree. I did note that the character variable is not the best approach.&lt;/P&gt;
&lt;P&gt;I have found that sometimes care with the format definitions can alleviate a lot of other hoops for getting desired appearance order. The restrictions on using PRELOADFMT, in the places that is allowed, sometimes get other undesired appearance. The format definition also can address things with character values as well where the options like Order=internal that may work with numeric values would not help. And unless you use a style override for ASIS=on those leading spaces tend to disappear in the output most of the time.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 14:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/651392#M195444</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-28T14:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/655793#M196740</link>
      <description>&lt;P&gt;Thanks .&lt;/P&gt;
&lt;P&gt;I want to ask a few questions please:&lt;/P&gt;
&lt;P&gt;1-By general I&amp;nbsp; knew that when we use same variable in class statement and in VAR statement (column invoice in our example) then we need to create another variable.&lt;/P&gt;
&lt;P&gt;for example:&lt;/P&gt;
&lt;P&gt;data cars;&lt;/P&gt;
&lt;P&gt;set sashelp.cars;&lt;/P&gt;
&lt;P&gt;invoice_=invoice;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I saw in your solution that you use same variable in class statement&amp;nbsp; and VAR&amp;nbsp; statement and it is working very well. I want to ask how is it working?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should you add any option to proc format when using completetypes+preloadfmt?&lt;/P&gt;
&lt;P&gt;How the options&amp;nbsp;completetypes+preloadfmt helping to create the table correctly?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value  InvoiceFmt
0&amp;lt;-20000='0-20k'
20000&amp;lt;-30000='20-30k'
30000&amp;lt;-50000='30-50k'
50000&amp;lt;-high='50k+'
;
run;
/*Results are good*/
proc summary data=sashelp.cars  nway missing completetypes;
format Invoice InvoiceFmt.;
class Type Invoice / preloadfmt;
var Invoice;
output out=summaryTbl2(drop=_type_) sum= mean= /autoname;
run;&lt;BR /&gt;
/*Results are not good: Invoice_sum and invoice_mean are not calculated!*/
proc summary data=sashelp.cars  nway missing;
format Invoice InvoiceFmt.;
class Type Invoice ;
var Invoice;
output out=summaryTbl2(drop=_type_) sum= mean= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 03:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/655793#M196740</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-06-10T03:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/657340#M196985</link>
      <description>When we create table reports, most of the time we encounter a missing category or a missing treatment in our data.To handle missing categories or groups efficiently, we can use format and means with options completetypes and preloadfmt. Using these options help us to display missing values with zeros and display a missing treatment or group in the table output.</description>
      <pubDate>Thu, 11 Jun 2020 09:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/657340#M196985</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-06-11T09:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: proc summary-order of categories is not as I want</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/657430#M196994</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;When we create table reports, most of the time we encounter a missing category or a missing treatment in our data.To handle missing categories or groups efficiently, we can use format and means with options completetypes and preloadfmt. Using these options help us to display missing values with zeros and display a missing treatment or group in the table output.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand this. The question was not about missing values or missing categories, the question was about the order of the categories in the output. Are you saying that completetypes and preloadfmt affect the order of the categories?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;When I run this code, without completetypes and preloadfmt, the categories are still sorted in the desired order, but the variables invoice_sum and invoice_mean are formatted, which I don't think is desired. Whereas the solution I provided above doesn't have this drawback of having the invoice_sum and invoice_mean formatted.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jun 2020 11:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-summary-order-of-categories-is-not-as-I-want/m-p/657430#M196994</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-11T11:08:04Z</dc:date>
    </item>
  </channel>
</rss>

