For a randomized normal dataset, When drawing histogram (histogram value/ scale = percent) and density (density value / type = normal scale = density) separately, their y-axes scales are different. I wanna draw a histogram and density together with histogram percentage on the left y-aix and probability density on the right y-axis. But once I have combined them, the two y-axes have the same scale with histogram percentage.
Create a 2nd variable that's a copy of the first one, like this:
data normal_distribution;
do i=1 to 1000;
value=rand('normal')*20+100;
val2=value; **** COPY OF VALUE ***** ;
output;
end;
run;
proc means data=normal_distribution min p1 p5 p95 p99 max;
var value;
run;
proc sgplot data = normal_distribution;
histogram value
/ scale = percent legendlabel = 'Histogram'
fillattrs=(color=blue) name = 'hist';
density val2 / type = normal scale = density /* USE VAL2 INSTEAD OF VALUE */
y2axis
lineattrs=(color=red thickness=2)
legendlabel='Probability Density'
name = 'dens';
keylegend 'hist' 'dens' /
linelength= 10
location = outside
position = bottom;
yaxis label = 'Histogram Percentage (%)';
y2axis label = 'Probability Density';
run;
Create a 2nd variable that's a copy of the first one, like this:
data normal_distribution;
do i=1 to 1000;
value=rand('normal')*20+100;
val2=value; **** COPY OF VALUE ***** ;
output;
end;
run;
proc means data=normal_distribution min p1 p5 p95 p99 max;
var value;
run;
proc sgplot data = normal_distribution;
histogram value
/ scale = percent legendlabel = 'Histogram'
fillattrs=(color=blue) name = 'hist';
density val2 / type = normal scale = density /* USE VAL2 INSTEAD OF VALUE */
y2axis
lineattrs=(color=red thickness=2)
legendlabel='Probability Density'
name = 'dens';
keylegend 'hist' 'dens' /
linelength= 10
location = outside
position = bottom;
yaxis label = 'Histogram Percentage (%)';
y2axis label = 'Probability Density';
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.