<?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 REPORT order=data within levels of grouping variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865039#M341590</link>
    <description>&lt;PRE&gt;data test;
  input param $ value $;
  datalines;
param1 level1
param1 level2
param1 level3
param1 other
param2 level4
param2 level5
param2 level6
param2 other
;
run;

proc report data=test;
  columns param value n;
  define param / group;
  define value / group order=internal;
run;

/*or could try order=format , but you need create a format firstly*/&lt;/PRE&gt;</description>
    <pubDate>Sat, 18 Mar 2023 09:16:59 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-03-18T09:16:59Z</dc:date>
    <item>
      <title>PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/864994#M341557</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am creating a report with a count of each value for several different parameters. All levels of the "value" variable need to appear in the same order as found in the data. Unfortunately, the levels for two of the parameters each include "Other", so PROC REPORT encounters "Other" from the first parameter before any of the levels of the second parameter and "Other" shows up first instead of last for the second parameter. Any ideas on the easiest way to fix this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input param $ value $;
  datalines;
param1 level1
param1 level2
param1 level3
param1 other
param2 level4
param2 level5
param2 level6
param2 other
;
run;

proc report data=test;
  columns param value n;
  define param / group;
  define value / group order=data;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Have:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mtnbikerjoshua_0-1679087940824.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81736i4550E04E4A61351B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mtnbikerjoshua_0-1679087940824.png" alt="mtnbikerjoshua_0-1679087940824.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mtnbikerjoshua_1-1679087985210.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/81737i6D96C583C5994B23/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mtnbikerjoshua_1-1679087985210.png" alt="mtnbikerjoshua_1-1679087985210.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 21:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/864994#M341557</guid>
      <dc:creator>mtnbikerjoshua</dc:creator>
      <dc:date>2023-03-17T21:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/864995#M341558</link>
      <description>&lt;P&gt;Add another variable to your data set that has the absolute order that you want.&lt;/P&gt;
&lt;P&gt;Make that the column before the problem variable and define it as an Order variable and NOPRINT so that it does not appear in they output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;
  input param $ value $ realorder;
  datalines;
param1 level1 1
param1 level2 2
param1 level3 3
param1 other  4
param2 level4 5
param2 level5 6
param2 level6 7
param2 other  8
;
run;

proc report data=test;
  columns  param realorder value n;
  define param / group;
  define realorder / order noprint ;
  define value / group ;
run;&lt;/PRE&gt;
&lt;P&gt;If your data is in the display order you need then you can add the "realorder" variable in a data step as;&lt;/P&gt;
&lt;PRE&gt;data need;
   set have;
   realorder=_n_;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2023 21:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/864995#M341558</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-17T21:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865000#M341561</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/423694"&gt;@mtnbikerjoshua&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option might be to create a preliminary report &lt;EM&gt;dataset&lt;/EM&gt; (called REP below) using BY-group processing and then create the actual report from that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select none;
proc report data=test out=rep(drop=_:);
by param; /* add NOTSORTED option if necessary */
column value n;
define value / group order=data;
run;
ods select all;

proc report data=rep;
define param / order order=data;
define n / display;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2023 22:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865000#M341561</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-03-17T22:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865039#M341590</link>
      <description>&lt;PRE&gt;data test;
  input param $ value $;
  datalines;
param1 level1
param1 level2
param1 level3
param1 other
param2 level4
param2 level5
param2 level6
param2 other
;
run;

proc report data=test;
  columns param value n;
  define param / group;
  define value / group order=internal;
run;

/*or could try order=format , but you need create a format firstly*/&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Mar 2023 09:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865039#M341590</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-18T09:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865229#M341672</link>
      <description>This does, in fact, work for the test data I provided, but in the real data there are some categories that are not in alphabetical order, so I need to stick with order=data.</description>
      <pubDate>Mon, 20 Mar 2023 14:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865229#M341672</guid>
      <dc:creator>mtnbikerjoshua</dc:creator>
      <dc:date>2023-03-20T14:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT order=data within levels of grouping variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865238#M341677</link>
      <description>&lt;P&gt;This solution ended up working nicely for me after a bit of tweaking. My real report has an across variable, so I had to make sure the data was grouped by the param and value variables, then create my realorder variable like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
  set test;
  by param value notsorted;
  if _n_ = 1 then realorder = 0;
  if first.value then realorder + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also had to make sure to place the realorder variable immediately before the value variable in the report and make it a group variable, so that it wouldn't mess up my grouping.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 15:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-order-data-within-levels-of-grouping-variable/m-p/865238#M341677</guid>
      <dc:creator>mtnbikerjoshua</dc:creator>
      <dc:date>2023-03-20T15:23:53Z</dc:date>
    </item>
  </channel>
</rss>

