## Overlapping 2 histograms, using the same scale.

Solved
Regular Contributor
Posts: 189

# Overlapping 2 histograms, using the same scale.

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

Accepted Solutions
Solution
‎12-17-2012 08:01 AM
Super Contributor
Posts: 543

## Re: Overlapping 2 histograms, using the same scale.

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.

*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.

All Replies
Solution
‎12-17-2012 08:01 AM
Super Contributor
Posts: 543

## Re: Overlapping 2 histograms, using the same scale.

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.

*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.

Regular Contributor
Posts: 189

## Re: Overlapping 2 histograms, using the same scale.

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

Super Contributor
Posts: 543

## Re: Overlapping 2 histograms, using the same scale.

My pleasure and best of luck!

Regular Contributor
Posts: 189

## Re: Overlapping 2 histograms, using the same scale.

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

Super Contributor
Posts: 543

## Re: Overlapping 2 histograms, using the same scale.

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.

🔒 This topic is solved and locked.