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

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.

Example.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Ullsokk wrote:

I really want to use proc sgplot, are you sure grouping is possible? Cant find example code for this in sgplot. 

 


Yes

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=grstatproc&docsetTarget=n0p7v...


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.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ullsokk
Pyrite | Level 9

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

PaigeMiller
Diamond | Level 26

@Ullsokk wrote:

I really want to use proc sgplot, are you sure grouping is possible? Cant find example code for this in sgplot. 

 


Yes

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=grstatproc&docsetTarget=n0p7v...


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.

--
Paige Miller
Ullsokk
Pyrite | Level 9
I have no idea how I missed that, thanks a bunch
PaigeMiller
Diamond | Level 26

And consider using the DENSITY command instead of the HISTOGRAM command.

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2108 views
  • 2 likes
  • 2 in conversation