## SAS sgplot - change scale when use group option

Hi,

I am trying to create a graph using the SGPLOT procedure, and combine it with the GROUP option (call them group1 and group2). My problem is that group1 and group2 have very different values (group1 values are much higher than group2) and so I'm trying to figure out how to modify my code to keep group1 values on the left hand y-axis and group2 values on the right hand y-axis. My original code is below. Thanks a lot in advance for your help!

proc sgplot data=data_have ;
series x = date  y= yvariable / group=treated ;
XAXIS LABEL = ' ' GRID VALUES = ('01jun2006'd TO '01sep2012'd BY 20) ;
YAXIS LABEL = 'Default Rate' /* GRID VALUES = (0 TO 10 BY 1)*/ ;
TITLE ' My title';
run;
quit;

1 ACCEPTED SOLUTION

Accepted Solutions  DanH_sas
SAS Super FREQ

## Re: SAS sgplot - change scale when use group option

To handle this situation, I would probably transpose the data such that the values for each group are in their own columns. Then, I would overlay two SERIES plots, assigning one series to the left axis and one series to the right axis (using the Y2AXIS option). That way, you can compare them independent of their ranges. The SGPLOT code would look something like this:

``````proc sgplot data=data_have ;
series x = date  y= group1 ;
series x = date  y= group2 / y2axis ;
XAXIS LABEL = ' ' GRID VALUES = ('01jun2006'd TO '01sep2012'd BY 20) ;
YAXIS LABEL = 'Default Rate' /* GRID VALUES = (0 TO 10 BY 1)*/ ;
TITLE ' My title';
run;
quit;
``````

Hope this helps!

Dan

6 REPLIES 6

## Re: SAS sgplot - change scale when use group option

Well, hard to say from that snippet.  What are your groups like?  If you number the groups, say small one as 1, and large one as 2, then apply a format based on that number it will put 1 first an 2 next.  Hard to tell if this is what your after though, provide test data (in the form of a datastep) and what the output should look like:

proc format;

value grp

1 = "Small group"

2 = "Big group";

run;  DanH_sas
SAS Super FREQ

## Re: SAS sgplot - change scale when use group option

To handle this situation, I would probably transpose the data such that the values for each group are in their own columns. Then, I would overlay two SERIES plots, assigning one series to the left axis and one series to the right axis (using the Y2AXIS option). That way, you can compare them independent of their ranges. The SGPLOT code would look something like this:

``````proc sgplot data=data_have ;
series x = date  y= group1 ;
series x = date  y= group2 / y2axis ;
XAXIS LABEL = ' ' GRID VALUES = ('01jun2006'd TO '01sep2012'd BY 20) ;
YAXIS LABEL = 'Default Rate' /* GRID VALUES = (0 TO 10 BY 1)*/ ;
TITLE ' My title';
run;
quit;
``````

Hope this helps!

Dan

## Re: SAS sgplot - change scale when use group option

Although this suggestions means I need to add another step (transpose the data), it definitely works. Thanks for your suggestion

## Re: SAS sgplot - change scale when use group option

I might also consider SGPANEL and Panel By your group variable. Use options to have the plots in one column to be one above the other and allow separate y axis values if needed.  DanH_sas
SAS Super FREQ

## Re: SAS sgplot - change scale when use group option

If you try the SGPANEL suggestion from ballardw, be sure to use the UNISCALE=COLUMN option on the PANELBY statement. Otherwise, the scales on the row axis will be unified, and you'll get the same scaling problem. The nice thing about this approach is that it requires no data manipulation, and there's no chance of confustion about which series belongs to which axis. The code should look something like this:

``````proc sgpanel data=data_have ;
panelby treated / uniscale=column layout=rowlattice;
series x = date  y= yvariable;
COLAXIS LABEL = ' ' GRID VALUES = ('01jun2006'd TO '01sep2012'd BY 20) ;
ROWAXIS LABEL = 'Default Rate' ;
TITLE ' My title';
run;
quit;``````

## Re: SAS sgplot - change scale when use group option

Thanks for the suggestion. SGPANEL is a cool procedure I may use in the future. But in this case I want to create the lines for each group in the same graph with two y axis in the same graph, instead of doing it in two separate panels. Thanks again!
Discussion stats
• 6 replies
• 2162 views
• 4 likes
• 4 in conversation