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.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

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.

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
  • 3 replies
  • 824 views
  • 2 likes
  • 3 in conversation