Data visualization with SAS programming

reversing order of character variable in sgplot

Reply
Frequent Contributor
Posts: 77

reversing order of character variable in sgplot

I have a character variable on the y axis, and need to reverse the order.

I created a new numeric ordinal  id variable in the excel version of the dataset, imported it to SAS, and reversed the order with proc sort.

It looked fine in proc print but in proc sgplot we did not have the desired order of the discrete variables.

 

Any ideas?

 

Also, if I want to use sgpanel, and have two variations of these figures side by side (1 row, 2 columns), how to I arrange for a y axis table to be on the right of each figure? Would also like to only label one of the y axes?

 

 

SAS Super FREQ
Posts: 1,265

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

The default order for discrete values on a category axis (like this one) will be sorted in ascending alphabetic order.  You can change that by setting the DISCRETEORDER option on the x-axis.  Use DISCRETEORDER=DATA if you want to preserve the order of the values in the data set.

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to Sanjay_SAS

Thank you Sanjay:

Yes, I did try that after Iooked at :

http://support.sas.com/documentation/cdl/en/grstatproc/65235/HTML/default/viewer.htm#n0n6uml63c6h8dn...

 

DISCRETEORDER= DATA | FORMATTED | UNFORMATTEDspecifies the order in which discrete tick values are placed on the axis. Specify one of the following values:

DATA

places the values in the order in which they appear in the data.
FORMATTED
sorts the formatted values in ascending character order.
UNFORMATTED
sorts the unformatted values in ascending character order.
Default:UNFORMATTED
Restriction:This option affects only box plots, dot plots, bar charts, and line plots, or for any axis where TYPE=DISCRETE.
 
 
I specifically wanted to reverse the direction, as the Y axis had these discrete values plotted from the bottom upwards, which is different in orientation to the order in the dataset.
I tried applying a "reverse" statement here but this didn't work. 
I also created an additional ordinal numeric ID variable and used proc sort,  so the order was reversed in the SAS dataset; Even though the order was reversed by proc sort, the graph still came out the same.
 
Only thing that worked was reversing the order in an Excel version of this dataset.
 
So.... is there not a way in SAS to plot a categorical axis in reverse orientation, but maintaining the same order?
In this case maintaining the order had clinical importance.
 
Thanks,
 
Christos

 

SAS Super FREQ
Posts: 1,265

Re: reversing order of character variable in sgplot

[ Edited ]
Posted in reply to ChristosK

With SGPLOT, HBAR (and HBOX, DOT) displays the values from top to bottom.  This is different from other plots, where y axis is bottom to top.  By default, the values are sorted.  You can set DISCRETEORDER=DATA to get them in the same order as the data, top to bottom.  REVERSE is not required.  If you use REVERSE, the values will be bottom to top.

 

This is different from GTL, where the default direction for y-axis is bottom to top and DATA order by default.

 

data test;
  input Name $ Value;
  datalines;
B  10
A   5
D  12
C   9
;
run;

proc sgplot data=test;
  hbar name / response=value nostatlabel;
  yaxis discreteorder=data /*reverse*/;
run;

 

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to Sanjay_SAS

Thank you Sanjay.

 

Initially it seemed as though I would only be able to chart 95% CI's for the OR by using a scatter command , but it looks like HBOX is able to do this automatically. My OR and CI's are values that I have calculated from the study database and recorded in a secondary database, so OR is not a continuous variable that I can plot with the choices that "seem" to be available for HBAR and DOT.

 

Scatter has "scatter x=OR y=Vp / xerrorupper=UCL xerrorlower=LCL", so plotting those is easy enough. I cant see how to do this in HBAR and DOT.

 

If I have groups pdf variables that are "clustered" in terms of clinical significance, is there a way to create a space between the groups, or to have them indented (but starting from a left margin), with a subheading in the row above and indented more to the left?

 

 

 

SAS Super FREQ
Posts: 1,265

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

it would be easier to help if you provide your code and some sample (non confidential) data to illustrate what you want or are doing.  All plot statements (Scatter, HBox, Dot) support GROUP= option.  The group values can be clustered using GROUPDISPLAY=cluster.  Category values can be discrete or interval.

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to Sanjay_SAS

Here is the latest code I used and the output. Have made some progress.

How do you change the order of the table columns so the p values for mild and severe end up adjacent to their respective. Ors (CI)?

Will send the database in the next message .

 

 

 

proc template;
define style mystyle;
parent=styles.sasweb;
class graphwalls /
frameborder=off;
class graphbackground /
color=white;
end;
run;

ods listing close;
ods html style=mystyle path='.' file='newgraph.html';
ods graphics / reset=all border=off width=800 height=900;
/* sd OR's */
proc sgplot data =HEERDT.VP_Table2b2 nowall noborder ;
Title "OR (95% CI) for covariates across Vasoplegia Severity" ;
styleattrs datacontrastcolors=(CX18577A CX30CC6E ) ;
scatter x=sdOR y=Name / xerrorupper=sdUCL xerrorlower=sdLCL noerrorcaps markerattrs=(symbol=circlefilled size =5 )
group=Severity groupdisplay=cluster clusterwidth=0.6;
yaxis DISCRETEORDER= DATA reverse ;
xaxis display =(noticks nolabel noline) VALUEATTRS=(Color=Black Family=Arial Size=8 Style=Normal Weight=Normal)type =log logbase =10;
yaxis display =(noticks nolabel noline) VALUEATTRS=(Color=Black Family=Arial Size=8 Style=Normal Weight=Normal)
valueshalign=left colorbands=odd colorbandsattrs=(transparency=0.7);
refline 1.0 / axis=x discretethickness=1 lineattrs=(color=cxd0e0f0);

yaxistable pdp2_char sdORCI /class =Severity classdisplay =stack position=right location=outside
VALUEATTRS=(Color=Black Family=Arial Size=8 Weight=Normal ) VALUEHALIGN=CENTER VALUEJUSTIFY= CENTER CLASSORDER=DATA
COLORGROUP=Severity;
xaxis display=(nolabel) values=(0.4 1.0 1.5 2.0 2.5 5 6);
run;

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

Here is the SAS database.

I've done most things, except:

 

1) Create a space between sets of clinical variables.

(eg preoperative conditions should be separated from bypass time, volume of blood products etc.)

 

2) Possibly changing the shading so sets of variables are all shaded o e colour.

 

3) Place a subtitle above and slightly indented to the left of each of these sets.

 

4) p values in table for severe should be next to their OR (CI), not alternately.  I would probably remove the table lable colours.

 

Thanks,

 

Christos

Attachment
SAS Super FREQ
Posts: 1,265

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

The data set "VP_Table2b.sas7bdat" does not have the variable  PDP2_Char.

Some of the domain specific terminology you are using is not familiar to me.

It seems you may want something like a Subgrouped Forest Plot

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to Sanjay_SAS

OMG.

 

Sorry about that! 

Thats pretty embarrassing, here is the right database.

Have looked at the full code at the link you forwarded, but still unsure how to make subgroups. Will have to percolate on this overnight.

How can you create a space between potential subgroups?

Attachment
SAS Super FREQ
Posts: 1,265

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

I assume you want to create gaps between certain groups of observations (rows) in the table.  Please see the idea in this article.  Same can be done for y-axis too. 

 

In the "Subgrouped Forest Plot" each group is merely another observation with missing odds ratio and statistics.  These also have a different text color.  The subgroup values (in each group) are indented using the YAxisTable option for indentation.  The presence of the group headers provides the appearance of gaps.  The article explains all this.

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to Sanjay_SAS

Sanjay:

 

Sorry about the late response. I have been trying to make this work, but am not able to get rid of the spaces yet on the categorical axis .

Maybe its an error that Im doing in formatting ?

 

I never did see how you can change the order of the columns though. I wanted to have two columns of p values and two for OR (CI), but to have the "Mild:" response first, and then the p columns  and OR(CI) for the "Severe Response).

 

 

Frequent Contributor
Posts: 77

Re: reversing order of character variable in sgplot

Posted in reply to ChristosK

 

OK, so here is what I tried too do.

Relevant code in red, attachments demonstrate the output, and what is going wrong....

 

 

proc format;
value $Namefmt
"Age" ="Age"
..........etc....

"A" ="."
"B" ="."
"C" ="."
;
run;

 

 

proc template;
define style mystyle;
parent=styles.sasweb;
class graphwalls /
frameborder=off;
class graphbackground /
color=white;
end;
run;

ods listing close;
ods html style=mystyle path='.' file='newgraph.html';
ods graphics / reset=all border=off width=800 height=900;


proc sgplot data = HEERDT.Vasoplegia_space nowall noborder ;
Title "OR (95% CI) for covariates across Vasoplegia Severity" ;
styleattrs datacontrastcolors=(CX18577A CX30CC6E ) ;
scatter x=sdOR y=Name / xerrorupper=sdUCL xerrorlower=sdLCL noerrorcaps markerattrs=(symbol=circlefilled size =5 )
group=Severity groupdisplay=cluster clusterwidth=0.6;
yaxis DISCRETEORDER= DATA reverse valueformat= $Namefmt.;
xaxis display =(noticks nolabel noline) VALUEATTRS=(Color=Black Family=Arial Size=8 Style=Normal Weight=Normal)type =log logbase =10;
yaxis display =(noticks nolabel noline) VALUEATTRS=(Color=Black Family=Arial Size=8 Style=Normal Weight=Normal)
valueshalign=left colorbands=odd colorbandsattrs=(transparency=0.7);
refline 1.0 / axis=x discretethickness=1 lineattrs=(color=cxd0e0f0);

yaxistable pdp2_char sdORCI /class =Severity classdisplay =stack position=right location=outside
VALUEATTRS=(Color=Black Family=Arial Size=8 Weight=Normal ) VALUEHALIGN=CENTER VALUEJUSTIFY= CENTER CLASSORDER=DATA
COLORGROUP=Severity;
xaxis display=(nolabel) values=(0.4 1.0 1.5 2.0 2.5 5 6);
run;

 

 

Ask a Question
Discussion stats
  • 12 replies
  • 363 views
  • 6 likes
  • 2 in conversation