Dear SAS experts,
I am trying to create a histogram with PROC UNIVARIATE:
proc univariate data=data1 noprint;
class hiv47;
histogram depression_cat/ normal midpoints=0 1 2 3 4 5 nrows=2 barlabel=percent;
inset n='Number of participants' median mean std='SD'/ position=ne;
label hiv47="HIV"
depression_cat="depression categories";
run;
It created the histogram, but is it possible to relabel each bar category, so I can use text for each bar instead of 0 1 2 3 4 5?
For example, 0=no depression, 1=minimal depression, 2=mild depression, etc....
I checked options for proc univariate but did not seem to find a good solution.
Also, is it possible to color code each bar by gender using proc univariate? For example, female proportion appears red while male proportion appears blue in each bar?
Thank you all in advance for any advice!
Use a format, as in:
data heart;
set sashelp.heart;
agegrp = round(ageAtStart, 10);
run;
proc sql;
create table ageGrp as
select unique
"ageGrp" as fmtname,
ageGrp as start,
cats("*", ageGrp, "*") as label
from heart;
quit;
proc format cntlin=ageGrp; run;
proc univariate data=heart;
class sex;
var ageGrp;
format ageGrp ageGrp.;
histogram ageGrp / normal midpoints=30 40 50 60 barlabel=percent;
run;
Use a format, as in:
data heart;
set sashelp.heart;
agegrp = round(ageAtStart, 10);
run;
proc sql;
create table ageGrp as
select unique
"ageGrp" as fmtname,
ageGrp as start,
cats("*", ageGrp, "*") as label
from heart;
quit;
proc format cntlin=ageGrp; run;
proc univariate data=heart;
class sex;
var ageGrp;
format ageGrp ageGrp.;
histogram ageGrp / normal midpoints=30 40 50 60 barlabel=percent;
run;
Thank you @PGStats for your wonderful advice as always.
So I specified the format in PROC FORMAT instead of SQL and the labels were not under the correct bar (see attached graph). Is this problem due to the length of characters for each category?
The labels look placed correctly but they have been thinned out. With UNIVARIATE, your only option is to make them shorter (abbreviations). SG procedures are more flexible, they let you specify a different label fit policy in such situations.
Thank you!
By SG you mean PROC SGPLOT? I will look into this procedure more
Is there a way to do this in SGPLOT?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.