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

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

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

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.

View solution in original post

5 REPLIES 5
AncaTilea
Pyrite | Level 9

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.

Ashwini_uci
Obsidian | Level 7

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. Smiley Happy

Thanks a bunch again..

Regards,

Ashwini

AncaTilea
Pyrite | Level 9

My pleasure and best of luck!

Ashwini_uci
Obsidian | Level 7

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

AncaTilea
Pyrite | Level 9

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.

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2592 views
  • 0 likes
  • 2 in conversation