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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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.

View solution in original post

6 REPLIES 6
Reeza
Super User
You can control it somewhat using the different style templates but PROC UNIVARIATE doesn't offer much customization options.

If you're looking for a fully customized graph, I would highly suggest using SGPLOT histogram code instead.

https://blogs.sas.com/content/graphicallyspeaking/2017/04/30/getting-started-with-sgplot-histograms/

You would use FILL/FILLATTRS option on the HISTOGRAM statement to control the colours.
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=grstatproc&docsetTarget=n...
IChatterji
Fluorite | Level 6

Thank you. I will try my hand at SGPLOT. Are questions on SGPLOT best directed to the Graphics community?

 

Reeza
Super User
Yes, any graphics related questions are best posted there.
Rick_SAS
SAS Super FREQ

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.

IChatterji
Fluorite | Level 6

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?

 

Rick_SAS
SAS Super FREQ

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;

SAS Innovate 2025: Register Now

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!

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
  • 1893 views
  • 2 likes
  • 3 in conversation