- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!
By SG you mean PROC SGPLOT? I will look into this procedure more
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there a way to do this in SGPLOT?