BookmarkSubscribeRSS Feed
hellohere
Pyrite | Level 9

I am doing a plot, need set y2axis with y2ais=0,  range [0, x) 

The code does not complain. But the plot still without zero on y2axis. Anyone?!

 

				proc sgplot data=_dif_temp_coll; 
						series x=endrange_n y=ind_min_dif /  lineattrs=( color=blue thickness=2 pattern=solid);  
						series x=endrange_n y=ind_off25_min  / y2axis   lineattrs=( color=red thickness=2 pattern=solid);  
						refline  &endind./axis=y lineattrs=(color=lime thickness=1 pattern=solid); 
						refline  0/axis=x lineattrs=(color=lime thickness=1 pattern=solid); 
						y2axis offsetmin=0; 
				run;quit;

8436                  proc sgplot data=_dif_temp_coll;
8437                          series x=endrange_n y=ind_min_dif /  lineattrs=( color=blue thickness=2 pattern=solid);
8438                          series x=endrange_n y=ind_off25_min  / y2axis   lineattrs=( color=red thickness=2 pattern=solid);
8439                          refline  &endind./axis=y lineattrs=(color=lime thickness=1 pattern=solid);
8440                          refline  0/axis=x lineattrs=(color=lime thickness=1 pattern=solid);
8441                          y2axis offsetmin=0;
8442                  run;

8442!                     quit;
NOTE: PROCEDURE SGPLOT used (Total process time):
      real time           0.25 seconds
      cpu time            0.07 seconds

NOTE: There were 18 observations read from the data set WORK._DIF_TEMP_COLL.

plot.jpg

 

If change the code as below, y2axis min=0; The plot shows up with y2axis=0. But the min of y2axis could be

any value. Surely SQL select into: can make it. But like to skip if possible. 

 

				proc sgplot data=_dif_temp_coll; 
						series x=endrange_n y=ind_min_dif /  lineattrs=( color=blue thickness=2 pattern=solid);  
						series x=endrange_n y=ind_off25_min  / y2axis   lineattrs=( color=red thickness=2 pattern=solid);  
						refline  &endind./axis=y lineattrs=(color=lime thickness=1 pattern=solid); 
						refline  0/axis=x lineattrs=(color=lime thickness=1 pattern=solid); 
						y2axis min=0; 
				run;quit;

plot2.jpg

7 REPLIES 7
PaigeMiller
Diamond | Level 26

If change the code as below, y2axis min=0; The plot shows up with y2axis=0. But the min of y2axis could be

any value. 

Does this mean the min of y2axis could be negative? What if the min of y2axis is 75? Do you still want 0 to appear on the axis?

 

It helps if you explain clearly what you DO want, rather than stating it doesn't work in some situations. We need rules about what should appear in every situation, not examples of a few things that are wrong. Rules, not examples.

--
Paige Miller
hellohere
Pyrite | Level 9

Ye, I need y2axis=0 is always there wether the range is [-5, 20] or [5, 20].

PaigeMiller
Diamond | Level 26

I think you need a macro variable which contains the minimum of the data. From there you can determine how to set MIN=

--
Paige Miller
Quentin
Super User

I agree with Paige, I would calculate the minimum Y2 value in the data, and if that value is greater than 0, add the y2axis min=0 statement.

 

If you really don't want to do that, a possible hack would be to add an invisible (meaning 0 weight, or white) refline on the Y2axis at 0.  You would use the /noclip option on the refline to force the y2axis to extend to show the refline.  But that would feel like more work, and more obfuscated code, than conditionally generating the axis statement.

PaigeMiller
Diamond | Level 26

@Quentin wrote:

I would calculate the minimum Y2 value in the data, and if that value is greater than 0, add the y2axis min=0 statement.


thinking about it some more, you need both the min and the max of the data in separate macro variables. If they are both positive then you need MIN=0. If they are both negative, then you need MAX=0.

--
Paige Miller
Quentin
Super User

Here's an example of the refline hack I mentioned.  Looks like SGPLOT is happy to draw a 0 thickness refline.

 

proc sort data=sashelp.class out=class ;
  by age ;
run ;

proc sgplot data=class ;
  series x=age y=height ;
  series x=age y=weight /y2axis;
  refline 0 /axis=y2 lineattrs=(thickness=0) noclip; *force y2 axis to extend to 0 ;
run ;
PaigeMiller
Diamond | Level 26

@hellohere wrote:

I need y2axis=0 is always there wether the range is [-5, 20] or [5, 20].


This is why we need rules and not examples.

 

"I need y2axis=0 is always there" is a clear rule, hard to misunderstand.

 

"wether the range is [-5, 20] or [5, 20]" are examples which are misleading and incomplete. Both myself and Quentin have been misled by this into not considering the case where both the minimum and maximum are negative.

 

Now, there is a detail in the problem description which you have not mentioned. Do you have to have a tick mark and the value of 0 appear on the Y2AXIS? Or does the Y2AXIS simply have to cover zero, with perhaps tick marks at -15 -5 5 15 and no tick mark at zero?

 

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 133 views
  • 2 likes
  • 3 in conversation