I have a report that dynamically compares two groups distribution on the most relevant variables.
ods graphics / NBINSMAX=100;
proc sgplot data=combo ;
histogram &var._1 / fillattrs=graphdata1 name='s' legendlabel='Group 1 transparency=0.5 ;
histogram &var._0 / fillattrs=graphdata2 name='d' legendlabel='Group 2' transparency=0.5 ;
keylegend 's' 'd' / location=inside position=topright across=1;
xaxis display=(nolabel);
run;
I want max 100 bins, but I want both groups to have the same number of bins, so that it is easy to compare the distributions. With the following code, the two distributions can get wildy different bin sizes, as shown in the example below.
As this is part of a macro, I cannot set the bin size manually for each plot, and just want each plot to have a uniform bin size for each group.
@Ullsokk wrote:
I really want to use proc sgplot, are you sure grouping is possible? Cant find example code for this in sgplot.
Yes
As for setting the binwidt, as I stated, I cant set this manually, as this is a macro that needs to show a sensible plot for any input data
If you are writing a general macro, you will know from the data the min and max values of the data, and so if you want k bins, you can figure out the width of each one of the k bins.
Use the BINWIDTH= option of the HISTOGRAM command.
Also, use one HISTOGRAM statement with a GROUP= option.
Also consider using the DENSITY command instead of the HISTOGRAM command.
I really want to use proc sgplot, are you sure grouping is possible? Cant find example code for this in sgplot.
As for setting the binwidt, as I stated, I cant set this manually, as this is a macro that needs to show a sensible plot for any input data
@Ullsokk wrote:
I really want to use proc sgplot, are you sure grouping is possible? Cant find example code for this in sgplot.
Yes
As for setting the binwidt, as I stated, I cant set this manually, as this is a macro that needs to show a sensible plot for any input data
If you are writing a general macro, you will know from the data the min and max values of the data, and so if you want k bins, you can figure out the width of each one of the k bins.
And consider using the DENSITY command instead of the HISTOGRAM command.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.