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.
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;
So one X axis showing 2 variables side by side?
Yes!
What does the Y axis look like?
Y is a percentage.
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;
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.