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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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;

Histogram.png

PG

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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;

Histogram.png

PG
michan22
Quartz | Level 8

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?

PGStats
Opal | Level 21

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.

PG
michan22
Quartz | Level 8

Thank you! 

By SG you mean PROC SGPLOT? I will look into this procedure more

mariko5797
Pyrite | Level 9

Is there a way to do this in SGPLOT?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 5647 views
  • 0 likes
  • 3 in conversation