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

Hi All,

I'm using Proc Surveymeans to plot several curves. I'd like to eliminate the kernel and normal density plots that overlay the histogram by default. Does anyone have an example of syntax for doing this? I have to use proc surveymeans b/c it's complex survey data.

Many thanks in advance for any suggestions, syntax examples, or references you may have on how to do this!  

 

Shar

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Here are a few tips for you:

1. How to obtain the data values form an ODS graph

2. How to create and interpret a weighted histogram

 

To put these together, add an ODS OUTPUT statement to your PROC SURVEYMEANS statements. Then use PROC SGPLOT to plot the same weighted histogram that SURVEYMEANS produces. Here is an example, which uses an example from the SURVEYMEANS documentation

data IceCreamStudy;
   input Grade StudyGroup Spending @@;
   if (Spending < 10) then Group='less';
      else Group='more';
   datalines;
7  34  7     7  34  7    7 412  4     9  27 14
7  34  2     9 230 15    9  27 15     7 501  2
9 230  8     9 230  7    7 501  3     8  59 20
7 403  4     7 403 11    8  59 13     8  59 17
8 143 12     8 143 16    8  59 18     9 235  9
8 143 10     9 312  8    9 235  6     9 235 11
9 312 10     7 321  6    8 156 19     8 156 14
7 321  3     7 321 12    7 489  2     7 489  9
7  78  1     7  78 10    7 489  2     7 156  1
7  78  6     7 412  6    7 156  2     9 301  8
;

data StudyGroups;
   input Grade _total_ @@;
   datalines;
7 608 8 252 9 403
;

data IceCreamStudy;
   set IceCreamStudy;
   if Grade=7 then Prob=8/608;
   if Grade=8 then Prob=3/252;
   if Grade=9 then Prob=5/403;
   Weight=1/Prob;
run;

/*****************************************/

title1 'Analysis of Ice Cream Spending';
proc surveymeans data=IceCreamStudy total=StudyGroups plots(only)=histogram;
   strata Grade / list;
   cluster StudyGroup;
   var Spending Group;
   weight Weight;
   ods output Histogram=HistData;   /* <== ADD THIS LINE TO YOUR PROGRAM */
run;

/* ADD THESE STATEMENTS */
title "Weighted Histogram";
proc sgplot data=HistData;
   histogram Val1  / WEIGHT=Weight;
run;

:

 

View solution in original post

6 REPLIES 6
ballardw
Super User

It might help to provide a starting point such as the code you are currently submitting.

 

You may have to export the graph data and use a separate procedure such as SGPLOT to display the result.

If you use the option

PLOTS=Histogram to create just a histogram without the packed boxplot then adding

 

ods output histogram= <yourdatasetnamegoeshere>;

as part of the procedure call as separate statement will create a data set with the values used to create the plots. The variable names starting with BIN would be used in SGPLOT to display just a histogram. If you need/ want different bins you would need to add the NBINS= option to the histogram plot request.

 

If you have the other elements from the procedure then you can use the Plots(only) =histogram to only create the histogram plot and data without the tabular output.

SharonZS
Obsidian | Level 7

Thank you for your response.  When I run the syntax below, I do get only a histogram but the outlines for the kernel and normal densities are still retained and overlaid. I tried to remove them using "ods exclude.... " but haven't been able to get rid of them. Thank you for the tip about outputting my data into Proc SGPLOT. Didn't think I could use it b/c of complex sample design.   

Thank you!

 

ods graphics on ;
proc surveymeans data=fullsamp.Costs_All09 plots(only) = histogram;
  var acsum;
  weight final_wgt;
  strata strat;
  cluster psu;
   where _mult_=1;
run;
ods graphics off;

ballardw
Super User

@SharonZS wrote:

Thank you for your response.  When I run the syntax below, I do get only a histogram but the outlines for the kernel and normal densities are still retained and overlaid. I tried to remove them using "ods exclude.... " but haven't been able to get rid of them. Thank you for the tip about outputting my data into Proc SGPLOT. Didn't think I could use it b/c of complex sample design.   

Thank you!

 

ods graphics on ;
proc surveymeans data=fullsamp.Costs_All09 plots(only) = histogram;
  var acsum;
  weight final_wgt;
  strata strat;
  cluster psu;
   where _mult_=1;
run;
ods graphics off;


I did not say that would remove the extra bits. I said it would create a data set that you could plot using SGPLOT to create the plot without the other bits. You would have to have the ODS OUTPUT Histogram= as part of the code to create that data set.

SharonZS
Obsidian | Level 7

Sorry!  I was just showing you what I'd already tried (tried plot (only) before posting...)!  I just output the data set to read into PROC SGPLOT (which I've most unfortunately never used...).  The two variables that I need to plot curves for have very different ranges (0 - 440,000) and (-200 - 60,000). Would you suggest customizing the bins somehow using PROC SURVEYMEANS before reading that data into PROC SGPLOT?  I will next read chapters & white papers on PROC SGPLOT. Thank you again for your response!    

 

 

 
Rick_SAS
SAS Super FREQ

Here are a few tips for you:

1. How to obtain the data values form an ODS graph

2. How to create and interpret a weighted histogram

 

To put these together, add an ODS OUTPUT statement to your PROC SURVEYMEANS statements. Then use PROC SGPLOT to plot the same weighted histogram that SURVEYMEANS produces. Here is an example, which uses an example from the SURVEYMEANS documentation

data IceCreamStudy;
   input Grade StudyGroup Spending @@;
   if (Spending < 10) then Group='less';
      else Group='more';
   datalines;
7  34  7     7  34  7    7 412  4     9  27 14
7  34  2     9 230 15    9  27 15     7 501  2
9 230  8     9 230  7    7 501  3     8  59 20
7 403  4     7 403 11    8  59 13     8  59 17
8 143 12     8 143 16    8  59 18     9 235  9
8 143 10     9 312  8    9 235  6     9 235 11
9 312 10     7 321  6    8 156 19     8 156 14
7 321  3     7 321 12    7 489  2     7 489  9
7  78  1     7  78 10    7 489  2     7 156  1
7  78  6     7 412  6    7 156  2     9 301  8
;

data StudyGroups;
   input Grade _total_ @@;
   datalines;
7 608 8 252 9 403
;

data IceCreamStudy;
   set IceCreamStudy;
   if Grade=7 then Prob=8/608;
   if Grade=8 then Prob=3/252;
   if Grade=9 then Prob=5/403;
   Weight=1/Prob;
run;

/*****************************************/

title1 'Analysis of Ice Cream Spending';
proc surveymeans data=IceCreamStudy total=StudyGroups plots(only)=histogram;
   strata Grade / list;
   cluster StudyGroup;
   var Spending Group;
   weight Weight;
   ods output Histogram=HistData;   /* <== ADD THIS LINE TO YOUR PROGRAM */
run;

/* ADD THESE STATEMENTS */
title "Weighted Histogram";
proc sgplot data=HistData;
   histogram Val1  / WEIGHT=Weight;
run;

:

 

SharonZS
Obsidian | Level 7

Thank you both for your responses!  Tremendously appreciated! Plots complete!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1799 views
  • 1 like
  • 3 in conversation