Hi.
I am trying to overlap 2 histograms with the same scale. The best I could do so far is to get them adjacently placed but I am not sure how to overlap/superimpose them.
Any advise/help would be highly appreciated.
here is the code for how i placed them next to each other. and attaching the figure for reference.
proc sgpanel data=diabet.EAT_DMNONDM;
panelby dm ;
histogram eatcm;
density eatcm;
title ' EATs compared between DM and NonDM';
run;
Thanks much,
Ashwini
Hi Ashwini.
I think that you need to rearrange your data a bit (a sample data would have helped) to have two different columns (1) eatcm for DM = 1 (something like eatcm_DM) and (2) eatcm for DM = 0 (or eatcm_nonDM)
Then you can use the SGPLOT procedure to do a histogram for eatcm_DM and eatcm_nonDM.
Below is a code that uses the sashelp.class data. In this data set there is a sex variable (M/F) and height.
I want to create an overlay histogram of height for both Male and Females.
I hope this code helps you to adjust your code:
*create a height for males and height for females variables;
data temp;
set sashelp.class;
if sex = "F" then ht_F = height;
if sex = "M" then ht_M = height;
run;
title 'Height Distribution';
proc sgplot data=temp;
histogram ht_M / fillattrs=graphdata1 name='M' legendlabel='Height for Males' transparency=0.5;
histogram ht_F / fillattrs=graphdata2 name='F' legendlabel='Height for Females' transparency=0.5;
keylegend 'M' 'F' / location=inside position=topright across=1;
xaxis display=(nolabel);
run;
Best of luck!
Anca.
Hi Ashwini.
I think that you need to rearrange your data a bit (a sample data would have helped) to have two different columns (1) eatcm for DM = 1 (something like eatcm_DM) and (2) eatcm for DM = 0 (or eatcm_nonDM)
Then you can use the SGPLOT procedure to do a histogram for eatcm_DM and eatcm_nonDM.
Below is a code that uses the sashelp.class data. In this data set there is a sex variable (M/F) and height.
I want to create an overlay histogram of height for both Male and Females.
I hope this code helps you to adjust your code:
*create a height for males and height for females variables;
data temp;
set sashelp.class;
if sex = "F" then ht_F = height;
if sex = "M" then ht_M = height;
run;
title 'Height Distribution';
proc sgplot data=temp;
histogram ht_M / fillattrs=graphdata1 name='M' legendlabel='Height for Males' transparency=0.5;
histogram ht_F / fillattrs=graphdata2 name='F' legendlabel='Height for Females' transparency=0.5;
keylegend 'M' 'F' / location=inside position=topright across=1;
xaxis display=(nolabel);
run;
Best of luck!
Anca.
Hi Anca,
Thank you very much for your elaboarate response and the code explaining the histograms overlay.
I rearranged my data and used the code and I could the get a nice histograms overlay.
That was a great help!! I needed it for my presentation.
Thanks a bunch again..
Regards,
Ashwini
My pleasure and best of luck!
Hi Anca,
One more question regarding this post.
I am wondering if you have any idea about how to add or highlight the mean value for EATCM under each histogram?
Thanks much,
Ashwini
Hi.
If you see this post:
it should do exactly what you need.
You first need to calculate the mean, save the mean values in a macro variable (unless you want to hard code them), and use an INSET statement to display the averages.
You could also insert the averages in your legend key, like this:
proc means data = temp;
var ht_m ht_f;
output out = avg mean = ;
run;
*save the means in a macro variable that you can later recall;
data _NULL_;
set avg;
call symput ("avg_M", put(ht_m,5.1));
call symput ("avg_F", put(ht_f,5.1));
run;
title 'Height Distribution';
proc sgplot data=temp;
histogram ht_M / fillattrs=graphdata1 name='M' legendlabel='Avg. Height for Males: &avg_M.' transparency=0.5;
histogram ht_F / fillattrs=graphdata2 name='F' legendlabel='Avg. Height for Females: &avg_F' transparency=0.5;
keylegend 'M' 'F' / location=inside position=topright across=1;
xaxis display=(nolabel);
run;
Good luck!
Anca.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.