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

On SAS 9.3, I'm trying to plot the width of a custom confidence interval by year. For each year, I have three groups. Using the graph, I would like to visually inspect the width of the intervals by each group across year. 

 

I am trying to do this by creating a stacked bar chart that uses an associated attribute map. The space from 0 to the lowerRange, I want to fill with white and the space from the lowerRange to the upperRange, I'd like to fill with a non-white color (here, orange). For each year, I'll have three stacked bars, one for each group. 

 

I'm getting an error when specifying the same value for the group= option in the VBARPARM statement in PROC SGPLOT. 

 

Here is the data:

 

data conditionData;

length model_year 8. price_type $ 15. grade_2 grade_3 grade_4 8.;

input model_year price_type $ grade_2 grade_3 grade_4;

datalines;

2002  lowerRange  2300  3600  .

2002  upperRange 3000  4400  .

2003  lowerRange   .        4100  .

2003  upperRange  .        4900  .

2004  lowerRange   .        5200  6200

2004  upperRange  .        6200  7100

;

run;

 

The attribute map I created is here:

 

 

data attrmap;
length id $ 15. value $ 20. fillcolor $ 9;
/*retain linecolor "black";*/
input id $ value fillcolor $;
datalines;
grade_2  avg_lowerRange  white
grade_2  avg_upperRange  orange
grade_3  avg_lowerRange  white
grade_3  avg_upperRange  orange
grade_4  avg_lowerRange  white
grade_4  avg_upperRange  orange
;
run;

 

Finally, here is the PROC SGPLOT code where I get the error:

 

proc sgplot data=test_trp dattrmap=attrmap noautolegend;
yaxis label='Auction Price';
vbarparm category=yearproduction response=grade_2 / 
	group=price_type attrid=grade_2
	barwidth=0.2 groupdisplay=stack
	discreteoffset=-0.4;
vbarparm category=yearproduction response=grade_3 / 
	group=price_type attrid=grade_3 
	barwidth=0.2 groupdisplay=stack
	discreteoffset=0;
vbarparm category=yearproduction response=grade_4 / 
	group=price_type attrid=grade_4
	barwidth=0.2 groupdisplay=stack
	discreteoffset=0.4;
run;

SAS issues a warning that "only one attribute id can be assigned to the price_type variable." Because of that, the bars for the last two groups are filled with default colors.

 

Is there a way I can correct this? Or another approach altogether?

 

I would appreciate any suggestions. Thank you!

 

Dhrumil 

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Hi

 

You could create new variables for each VBARPARM statement having the same content. Based on the code you provided, I put together this sample program.

 

data test_trp;
  length model_year 8. price_type $ 15. grade_2 grade_3 grade_4 8.;
  input model_year price_type $ grade_2 grade_3 grade_4;
  price_type2 = price_type;
  price_type3 = price_type;
  datalines;
2002  lowerRange  2300  3600  .
2002  upperRange 3000  4400  .
2003  lowerRange   .        4100  .
2003  upperRange  .        4900  .
2004  lowerRange   .        5200  6200
2004  upperRange  .        6200  7100
;

data attrmap;
length id $ 15. value $ 20. fillcolor $ 9;

retain linecolor "black";
input id $ value fillcolor $;
datalines;
grade_2  lowerRange  white
grade_2  upperRange  orange
grade_3  lowerRange  white
grade_3  upperRange  orange
grade_4  lowerRange  white
grade_4  upperRange  orange
;

proc sgplot data=test_trp dattrmap=attrmap noautolegend;
  yaxis label='Auction Price';
  vbarparm category=model_year response=grade_2 / 
    group=price_type attrid=grade_2
    barwidth=0.2 groupdisplay=stack
    discreteoffset=-0.4;
  vbarparm category=model_year response=grade_3 / 
    group=price_type2 attrid=grade_3 
    barwidth=0.2 groupdisplay=stack
    discreteoffset=0;
  vbarparm category=model_year response=grade_4 / 
    group=price_type3 attrid=grade_4
    barwidth=0.2 groupdisplay=stack
    discreteoffset=0.4;
run;

Bruno

View solution in original post

2 REPLIES 2
BrunoMueller
SAS Super FREQ

Hi

 

You could create new variables for each VBARPARM statement having the same content. Based on the code you provided, I put together this sample program.

 

data test_trp;
  length model_year 8. price_type $ 15. grade_2 grade_3 grade_4 8.;
  input model_year price_type $ grade_2 grade_3 grade_4;
  price_type2 = price_type;
  price_type3 = price_type;
  datalines;
2002  lowerRange  2300  3600  .
2002  upperRange 3000  4400  .
2003  lowerRange   .        4100  .
2003  upperRange  .        4900  .
2004  lowerRange   .        5200  6200
2004  upperRange  .        6200  7100
;

data attrmap;
length id $ 15. value $ 20. fillcolor $ 9;

retain linecolor "black";
input id $ value fillcolor $;
datalines;
grade_2  lowerRange  white
grade_2  upperRange  orange
grade_3  lowerRange  white
grade_3  upperRange  orange
grade_4  lowerRange  white
grade_4  upperRange  orange
;

proc sgplot data=test_trp dattrmap=attrmap noautolegend;
  yaxis label='Auction Price';
  vbarparm category=model_year response=grade_2 / 
    group=price_type attrid=grade_2
    barwidth=0.2 groupdisplay=stack
    discreteoffset=-0.4;
  vbarparm category=model_year response=grade_3 / 
    group=price_type2 attrid=grade_3 
    barwidth=0.2 groupdisplay=stack
    discreteoffset=0;
  vbarparm category=model_year response=grade_4 / 
    group=price_type3 attrid=grade_4
    barwidth=0.2 groupdisplay=stack
    discreteoffset=0.4;
run;

Bruno

dhrumil_patel
Fluorite | Level 6

Thanks, Bruno. This works nicely.

 

Dhrumil

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!

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.

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
  • 2 replies
  • 853 views
  • 0 likes
  • 2 in conversation