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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 2470 views
  • 0 likes
  • 2 in conversation