Help using Base SAS procedures

SGPLOT Bar chart Xaxis labeling error

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

SGPLOT Bar chart Xaxis labeling error

[ Edited ]

I am trying to create a bar chart with the correct data labels. If I write my code like this:

DATA AgeChart; SET BCMasterSet2;
FORMAT AgeGroup;
IF missing(Age) then delete;
IF Age < 40 then AgeGroup = 39; ELSE IF (Age > 39 & Age < 50) then AgeGroup = 49;
ELSE IF (Age > 49 & Age < 60) then AgeGroup = 59; ELSE IF (Age > 59 & Age < 70) then AgeGroup = 69;
ELSE AgeGroup = 70;
KEEP Age AgeGroup;
RUN;

PROC SGPLOT DATA = AgeChart; 
/*xaxis values = ("<40" "40-49" "50-59" "60-69" "70>");*/
VBAR AgeGroup;
run;

I get the following chart: 

chart.jpg

 

But I don't want the data labels as they are. I want them to say "<40", "40-49", "50-59", "60-69", and "70>".

 

So I typed in the above code in order to get that, removing the comment feature out of the xaxis values statement, and this is what happens to my bar chart: 

messedupchart.jpg

 

Where did all my data go? Can someone help me fix this problem? Thank you!


Accepted Solutions
Solution
‎06-20-2017 04:29 PM
Super User
Posts: 10,476

Re: SGPLOT Bar chart Xaxis labeling error

See if this gets you close:

proc format library=work;
value AgeGroup
0 - 39 = " <40"
40 - 49= "40-49"
50 - 59= "50-59"
60 - 69= "60-69"
70 - high="70>"
;
run;

proc sgplot data=Bcmasterset2;
   vbar age ;
   format age agegroup.;
   label age= 'Age Group';
run;

You can use custom formats to create groups of values as well as supply text labels for them. I use them a lot because I can have multiple similar formats and apply as needed without creating addtional variables.

 

 

The issue is that XAXIS values you provided were character and did not match values of the actual age group variable.

View solution in original post


All Replies
Solution
‎06-20-2017 04:29 PM
Super User
Posts: 10,476

Re: SGPLOT Bar chart Xaxis labeling error

See if this gets you close:

proc format library=work;
value AgeGroup
0 - 39 = " <40"
40 - 49= "40-49"
50 - 59= "50-59"
60 - 69= "60-69"
70 - high="70>"
;
run;

proc sgplot data=Bcmasterset2;
   vbar age ;
   format age agegroup.;
   label age= 'Age Group';
run;

You can use custom formats to create groups of values as well as supply text labels for them. I use them a lot because I can have multiple similar formats and apply as needed without creating addtional variables.

 

 

The issue is that XAXIS values you provided were character and did not match values of the actual age group variable.

Contributor
Posts: 20

Re: SGPLOT Bar chart Xaxis labeling error

Perfect! You are a genius!
Super User
Posts: 9,671

Re: SGPLOT Bar chart Xaxis labeling error

proc sgplot data=sashelp.class;
vbar age;
xaxis values=('11' '12' '13' '14' '15' '16')  VALUESDISPLAY=("<40" "40-49" "50-59" "60-69" "70>" "sds");
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 124 views
  • 1 like
  • 3 in conversation