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

I'm trying to make a bar chart where I can specify the order of X_A X_B and so on.

It looks like the default is alphabetical order. 

 

I would also like to reorder the bars as well; such as the order of E, Q, R, T, U, W.

Screen Shot 2021-09-12 at 10.12.45 AM.png

ods graphics / width=10in height=5in;

title '[P]in X';
proc sgplot data=Narwhal.Narwhal_Data (where=(Dependant_V Contains 'X'));
  vbar Dependant_V / response=N_Concentration group=Experiment_Group groupdisplay=cluster 
    stat=mean limitstat=clm;
  xaxis /*display=(nolabel noticks)*/ label="Package";
  yaxis grid label="Concentration (pines/apples)";
run;

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello @Kedarnath_M ,

 

On the xaxis - statement you need the

  • VALUES=(“string-list” ) specifies tick values for discrete axes. The values can be character or numeric.

option.

 

Or on the VBAR statement you need the 

  • CATEGORYORDER=RESPASC(ending) | RESPDESC(ending) specifies the order in which the categories are arranged.

option.

 

Good luck,

Koen

View solution in original post

6 REPLIES 6
sbxkoenk
SAS Super FREQ

Hello @Kedarnath_M ,

 

On the xaxis - statement you need the

  • VALUES=(“string-list” ) specifies tick values for discrete axes. The values can be character or numeric.

option.

 

Or on the VBAR statement you need the 

  • CATEGORYORDER=RESPASC(ending) | RESPDESC(ending) specifies the order in which the categories are arranged.

option.

 

Good luck,

Koen

Kedarnath_M
Obsidian | Level 7
Sweet thank you Koen👍
I tried the Categoryorder= option and its pretty cool I may use that as well, but is there a way to specify the order of the categories manually?
sbxkoenk
SAS Super FREQ

Hello,

with the VALUES=(“string-list”) option on the xaxis statement:

proc sgplot data=sashelp.heart;
  vbar smoking_status /
    response=ageatdeath
    stat=mean;
run;

proc sgplot data=sashelp.heart;
  vbar smoking_status /
    response=ageatdeath
    stat=mean
    datalabel=Smoking_Status;
xaxis values=("Moderate (6-15)" "Non-smoker" "Heavy (16-25)" "Very Heavy (> 25)");
run;
/* end of program */

Koen

Kedarnath_M
Obsidian | Level 7

My bad should have specified; I got the first part with the Value= option

 

title '[P]in X';
proc sgplot data=Narwhal.Narwhal_Data (where=(Location Contains 'X'));
  vbar Dependant_V / response=N_Concentration group=Experiment_Group groupdisplay=cluster 
    stat=mean limitstat=clm alpha=.05 ;
  xaxis /*display=(nolabel noticks)*/ label="Package" 
  values=('X_A' 'X_C' 'X_D' 'X_E' 'X_B' 'X_F');
  yaxis grid label="Concentration (pines/apples)";
run;

I meant for the second part where I can control the order of Q W E R T U Experimental_Group Variable order manually. This is without using the Categoryorder= option since that option orders the groups by itslef.

Screen Shot 2021-09-12 at 4.09.04 PM.png the part circled in red

Ksharp
Super User

Padding some blank before it.

data have;
length Dependant_V $ 80 ;
set Narwhal.Narwhal_Data;
if Dependant_V='E' then Dependant_V ='          E';
if Dependant_V='Q' then Dependant_V ='      Q';
if Dependant_V='R' then Dependant_V ='   R';
run;
title '[P]in X';
proc sgplot data=have (where=(Dependant_V Contains 'X'));
Kedarnath_M
Obsidian | Level 7

Thank you Koen and KSharp! that worked 

At first I was confused because the letters wouldn't show up in the work.have data set at first. The length statement should have allowed there to be more characters in the column. When I ran proc contents I saw the format allowed only one character and so I had to go in and change that. I'm still new to SAS😅.

data work.have;
length Experiment_Group $ 10;
format Experiment_Group $ char10.;
set Narwhal.Narwhal_Data;
if Experiment_Group='Q' then Experiment_Group='     Q';
if Experiment_Group='W' then Experiment_Group='    W';
if Experiment_Group='E' then Experiment_Group='   E';
if Experiment_Group='R' then Experiment_Group='  R';
if Experiment_Group='T' then Experiment_Group=' T';
if Experiment_Group='U' then Experiment_Group='U';
run;

PROC CONTENTS DATA=have;

/* Open the LISTING destination and assign the LISTING style to the graph */
*ods listing style=listing;
ods graphics / width=10in height=5in;

title '[P]in X';
proc sgplot data=work.have (where=(Dependant_V Contains 'X'));
  vbar Dependant_V / response=N_Concentration group=Experiment_Group groupdisplay=cluster 
    stat=mean limitstat=clm;
  xaxis /*display=(nolabel noticks)*/ label="Package"
  values=('X_A' 'X_C' 'X_D' 'X_E' 'X_B' 'X_F');
  yaxis grid label="Concentration (pines/apples)";
run;

Screen Shot 2021-09-14 at 9.55.50 AM.png

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