Hello,
I am using the following code to create Histograms that I will ultimately include in a presentation package. I need to format the titles and color bars so that it matches the rest of the presentation template. My 2 needs are :
- What is the code to specify the color that is filled in the histogram bar?
- What is the code to display the BY variables in the Plot Description? Currently the histogram description is "Distribution of PnL". I want to be able to say "Distribution of PnL - <Symbol>_<RiskProfile>"
Any suggestions would be greatly appreciated. If this question should be posted in Graphics community, please let me know.
ods select Histogram;
proc univariate data=Mstr_rule4a_analysis noprint;
by symbol riskprofile;
var PnL;
histogram/barlabel=count ;
where win_loss ne 'FEE' and year ge 2015 ;
inset n mean std="Std Dev" / pos = ne format = 5.2;
title 'IB One-5yrs-Histogram PnL';
run;
title;
For the title, use the ODSTITLE= option in conjunction with the #BYVAL keyword. If you aren't familiar with the #BYVAL keyword, see "How to use the #BYVAR and #BYVAL keywords."
options nobyline;
proc univariate ...;
ods select Histogram;
by symbol riskprofile;
var PnL;
histogram PnL / odstitle="Distribution of PnL - #ByVal1._#ByVal2";
...
run;
options byline;
Even if you switch to PROC SGPLOT (which I agree is a good idea), you can also use the #BYVAL keyword to control the TITLE statement used for your PROC SGPLOT graphs.
Thank you. I will try my hand at SGPLOT. Are questions on SGPLOT best directed to the Graphics community?
For the title, use the ODSTITLE= option in conjunction with the #BYVAL keyword. If you aren't familiar with the #BYVAL keyword, see "How to use the #BYVAR and #BYVAL keywords."
options nobyline;
proc univariate ...;
ods select Histogram;
by symbol riskprofile;
var PnL;
histogram PnL / odstitle="Distribution of PnL - #ByVal1._#ByVal2";
...
run;
options byline;
Even if you switch to PROC SGPLOT (which I agree is a good idea), you can also use the #BYVAL keyword to control the TITLE statement used for your PROC SGPLOT graphs.
Thank you Rick. The #ByVal<n> option works very well, I am a bit embarrassed that it was not known to me as this was available in SAS since 1998! I am attaching below my final code in case any reader wants to view the before/after code change.
options byline;
ods select Histogram;
proc univariate data=Mstr_rule4a_analysis noprint;
by symbol riskprofile;
var PnL;
histogram/barlabel=count odstitle="Distribution of PnL - #ByVal1._#ByVal2";
where win_loss ne 'FEE' and year ge 2015 ;
inset n mean std="Std Dev" / pos = ne format = 5.2;
title 'IB One-5yrs-Histogram PnL';
run;
title;
options nobyline;
Do you by any chance have a suggestion for how to change the colors in the histogram bars without going to SGPLOT?
The colors in SAS graphs are determined by the current ODS style. You can create a new style and specify the color that is used to fill bars (and other graph attributes). There is a whole chapter in the SAS/STAT documentation about how to understand and modify ODS styles.
Personally, I think it will be easier for you to use SGPLOT. Here is an example so you can see how it is done:
proc sort data=sashelp.cars(where=(type in ('Sedan' 'Wagon' 'SUV'))) out=cars;
by origin type;
run;
options nobyline;
title "Distribution of MPG City - #ByVal1._#ByVal2";
proc sgplot data=cars;
by origin type;
histogram MPG_City / fillattrs=(color=VeryLightYellow); /* or color=CXFAFAA0 */
run;
options byline;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.