kstats9pt3
kstats9pt3

Reputation: 873

SAS GChart: Controlling Order of Group and Subgroup in Horizontal Chart

As an example, I have two groups with four animals each. The group is the variable group and the subgroup variable is animal, and the subjects need to appear in the chart as they appear in the dataset. However, gchart keeps putting the subgroup variable in ascending order. Is there a simple way to control this?

Below is the mock dataset and code:

data test;
    input animal $ group ttd;
    datalines;
    A157    1   840
    A164    1   840
    A163    1   840
    A118    1   840
    A113    2   125
    A109    2   230
    A175    2   840
    A304    2   50
    ;
run;

axis1 label=(f=arial h=1.5 'Time to Death (Hours Post-Exposure)') value=(f=arial h=1) order=(0 to 840 by 120) minor=(n=5) offset=(1,1);
axis2 label=(f=arial h=1.2 j=r 'Group') value=(f=arial h=1);
axis3 label=(f=arial h=1.2 j=c 'Animal') value=(f=arial h=1);
PATTERN1 C=BLACK;

proc gchart data=test;
  hbar animal /group=group nozero nostats sumvar=ttd type=sum
              width=0.4 space=1 raxis=axis1 gaxis=axis2 maxis=axis3;
run; quit;

Notice the resulting graphic has the subgroup animals in ascending order, rather than the correct order in the dataset:

Notice the resulting graphic has the animals in ascending order, rather than the correct order in the dataset

Upvotes: 1

Views: 1661

Answers (2)

kstats9pt3
kstats9pt3

Reputation: 873

I figured it out. I basically create an order variable, name that variable the animal names, and then use that ordered variable to create the chart rather than the animals themselves. Below is the quick process:

Create an order variable

data test1; set test;
    temp=_n_;
    char_animal=put(temp,3.);
run;

Put your sequence of animals in a list

proc sql;
    select distinct quote(animal)
    into: animallist separated by " "
    from test1 order by temp;
quit;

%put &animallist.;

In the axis3 statement, use the macro list in the value statement to "name" the order variable the animal names

axis1 label=(f=arial h=1.5 'Time to Death (Hours Post-Exposure)') value=(f=arial h=1) order=(0 to 840 by 120) minor=(n=5) offset=(1,1);
axis2 label=(f=arial h=1.2 j=r 'Group') value=(f=arial h=1);
axis3 label=(f=arial h=1.2 j=c 'Animal') value=(f=arial h=1 &animallist.);
PATTERN1 C=BLACK;

proc gchart data=test1;
  hbar char_animal /group=group nozero nostats sumvar=ttd type=sum
              width=0.4 space=1 raxis=axis1 gaxis=axis2 maxis=axis3;
run; quit;

Notice the correct order in the graphic now:

enter image description here

Upvotes: 1

Joe
Joe

Reputation: 63424

I don't think you can get it in the dataset order. (Technically, by the way, this is ascending order of the value of Animal - while it goes 'down' it is still considered ascending from the point of view of the proc; see how the ASCENDING/DESCENDING option order things.)

From my understanding, gplot for HBAR allows (default=ascending order of values, ASCENDING=ascending order of bars, DESCENDING=descending order of bars).

You can use SGPLOT (which has grouporder=data option, at least by 9.4).

proc sgplot data=test;
  hbar group/ response=ttd group=animal groupdisplay=cluster grouporder=reversedata;
run;

I use reversedata, as the ascending/descending thing is flipped in hbars compared to what seems logical, but you can also flip the axis itself if you prefer that (or if reversedata is not available in your version).

Upvotes: 0

Related Questions