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
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
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
Thanks, Bruno. This works nicely.
Dhrumil
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.