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

I have some data that I would like to print or plot.  I would like the output grouped by a certain variable.  I can do this like this:

data test;

proc print; by source;

proc means; by source;

In my example source can be a single letter 'A', 'B', 'C', or 'D'.  I would like to specify the order of the groups.  I don't want it to be

alphabetical.  I might want the order to be 'C', 'B', 'D', 'A'.  Is there a way to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

Hi ... never tried this before, but it appears to do what you asked ...

proc trantab table=ascii;

replace 'C' '1' ;

replace 'B' '2';

replace 'D' '3';

replace 'A' '4';

save table=test;

quit;

data x;

do _n_ = 1 to 1000;

   letter = char('ABCD',rantbl(999,.25,.25,.25));

   x = ceil(1000*ranuni(999));

   output;

end;

run;

proc sort data=x sortseq=test;

by letter;

run;

proc means data=x;

by letter;

run;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

There might be a more direct method I can't think of at the moment, but you can always create an informat, apply it, and then use it as your by variable.  e.g.:

proc format;

  invalue $names

  'Henry'=1

  'Alice'=2

  'James'=3

  'Jane'=4

  other=5;

run;

data test;

  set sashelp.class;

  sortnames=input(name,$names.);

run;

proc sort data=test;

  by sortnames;

run;

proc print data=test (

  drop=name rename=(sortnames=name));

  by name;

run;

Ksharp
Super User

Art. it should be

proc format;

  invalue names

Not include Dollar sign because it is numeric informat not character.

I know SAS passed this fragment code. But I recommend to not use $.

Ksharp

WesBarris
Obsidian | Level 7

Thanks Art.  However, the output of your example includes name=1, name=2, etc.  Is there a way to show the original names in the output instead of the value we are using to specify their order?

SteveDenham
Jade | Level 19

I don't know about "instead of", but I do know how to get "in addition to."

proc sort data=test;

  by sortnames name;

run;

proc print data=test ;

  by sortnames name;

run;

This should at least put both in the byvar header block.

Steve Denham

MikeZdeb
Rhodochrosite | Level 12

Hi ... never tried this before, but it appears to do what you asked ...

proc trantab table=ascii;

replace 'C' '1' ;

replace 'B' '2';

replace 'D' '3';

replace 'A' '4';

save table=test;

quit;

data x;

do _n_ = 1 to 1000;

   letter = char('ABCD',rantbl(999,.25,.25,.25));

   x = ceil(1000*ranuni(999));

   output;

end;

run;

proc sort data=x sortseq=test;

by letter;

run;

proc means data=x;

by letter;

run;

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2907 views
  • 6 likes
  • 5 in conversation