Help using Base SAS procedures

How to specify the order of by groups?

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

How to specify the order of by groups?

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?


Accepted Solutions
Solution
‎11-02-2011 10:47 AM
Valued Guide
Posts: 765

Re: How to specify the order of by groups?

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


All Replies
PROC Star
Posts: 7,356

How to specify the order of by groups?

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;

Super User
Posts: 9,662

How to specify the order of by groups?

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

Contributor
Posts: 44

How to specify the order of by groups?

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?

Respected Advisor
Posts: 2,655

How to specify the order of by groups?

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

Solution
‎11-02-2011 10:47 AM
Valued Guide
Posts: 765

Re: How to specify the order of by groups?

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 173 views
  • 6 likes
  • 5 in conversation