BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mtnbikerjoshua
Obsidian | Level 7

Hi,

 

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?

 

Example:

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;

Have:

mtnbikerjoshua_0-1679087940824.png

Want:

mtnbikerjoshua_1-1679087985210.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Add another variable to your data set that has the absolute order that you want.

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.

 

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;

If your data is in the display order you need then you can add the "realorder" variable in a data step as;

data need;
   set have;
   realorder=_n_;
run;

View solution in original post

5 REPLIES 5
ballardw
Super User

Add another variable to your data set that has the absolute order that you want.

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.

 

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;

If your data is in the display order you need then you can add the "realorder" variable in a data step as;

data need;
   set have;
   realorder=_n_;
run;
mtnbikerjoshua
Obsidian | Level 7

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:

 

data test2;
  set test;
  by param value notsorted;
  if _n_ = 1 then realorder = 0;
  if first.value then realorder + 1;
run;

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.

FreelanceReinh
Jade | Level 19

Hi @mtnbikerjoshua,

 

Another option might be to create a preliminary report dataset (called REP below) using BY-group processing and then create the actual report from that.

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;
Ksharp
Super User
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*/
mtnbikerjoshua
Obsidian | Level 7
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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1569 views
  • 1 like
  • 4 in conversation