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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 5511 views
  • 0 likes
  • 3 in conversation