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

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

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 

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

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 

ioannis
Fluorite | Level 6
Although this suggestions means I need to add another step (transpose the data), it definitely works. Thanks for your suggestion
ballardw
Super User

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

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;
ioannis
Fluorite | Level 6
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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2349 views
  • 4 likes
  • 4 in conversation