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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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.

lady8506
Quartz | Level 8
Perfect! You are a genius!
Ksharp
Super User
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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 760 views
  • 2 likes
  • 3 in conversation