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

Is it possible to print two histograms on a graph using proc sgplot? 
My code has been:

proc sgplot data=me;
where num_days=:'Days 100 - 200';
histogram obese/ fillattrs=(color=pink) nbins=5  showbins;
run;

The above code me just one histogram. How do I add a histogram of a second variable "sex" alongside that of "obese" on the same graph. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Here is a (very ugly) way. There might be better ways.

data PLOT;
  set SASHELP.CLASS;
  length X $10;
  X=cats(AGE); PCT=AGE/2; output;
  X=SEX      ; PCT + 3;   output;
  if _N_=1 then do; X='A'; PCT=0; output; end;
run;

proc format ; value $xaxis 'A '='  ';
 
ods graphics on / width=1000px height=400px;
proc sgplot data=PLOT; 
  vbar X / response=PCT attrid=X;
  format X $xaxis.;
  xaxis label= "%sysfunc(repeat(%str( ),50)) Age %sysfunc(repeat(%str( ),120)) Sex" ;
run; 

ChrisNZ_0-1627276355352.png

 

 

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

So one X axis showing 2 variables side by side?

ChuksManuel
Pyrite | Level 9

Yes!

ChrisNZ
Tourmaline | Level 20
ChuksManuel
Pyrite | Level 9

Y is a percentage.

ChrisNZ
Tourmaline | Level 20

Here is a (very ugly) way. There might be better ways.

data PLOT;
  set SASHELP.CLASS;
  length X $10;
  X=cats(AGE); PCT=AGE/2; output;
  X=SEX      ; PCT + 3;   output;
  if _N_=1 then do; X='A'; PCT=0; output; end;
run;

proc format ; value $xaxis 'A '='  ';
 
ods graphics on / width=1000px height=400px;
proc sgplot data=PLOT; 
  vbar X / response=PCT attrid=X;
  format X $xaxis.;
  xaxis label= "%sysfunc(repeat(%str( ),50)) Age %sysfunc(repeat(%str( ),120)) Sex" ;
run; 

ChrisNZ_0-1627276355352.png

 

 

ChuksManuel
Pyrite | Level 9
Thanks!

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
  • 6 replies
  • 1481 views
  • 0 likes
  • 2 in conversation