BookmarkSubscribeRSS Feed
stancemcgraw
Obsidian | Level 7

Does anyone know the code to do a chart like this, in SAS 9.4? Where the survey questions are on the y axis and the survey scores are on the x-axis, divided up in the legend between pre and post, plus standard deviations.

 

See attached photo example 

2 REPLIES 2
BrunoMueller
SAS Super FREQ

SGPLOT can do this, but the bar segments are side by side (GROUPDISPLAY=CLUSTER)

 

If you want the stacked bar then you need to calculate the values up front and use VBARPARM together with the HIGHLOW plot statements.

 

Here is an example ussing SASHELP.CARS

 

/*
 * generate test data
 */
data cars;
  set sashelp.cars;
  where
    origin in ("Asia", "USA")
    and not (type = "Hybrid")
  ;
  /*
   * choose which var to use
   */
  value = horsepower;
  format value comma12.;
run;

/*
 * what you get for free
 */
ods graphics / width=1200 height=900;
proc sgplot data=cars;
  vbar type /
    group=origin
    response=value
    stat=mean
    limits=both
    limitstat=stddev
    groupdisplay=cluster
    seglabel
  ;
run;

/*
 * calculate mean and std
 */
proc  sql;
  create table cars_limits as
  select
    type
    , origin
    , mean(value) as mean_value format=comma12.
    , std(value) as std_value format=comma12.
  from
    cars
  group by
      type
    , origin
  order by
    type
    , origin
  ;
quit;

/*
 * compute values used with highlow stmt
 */
data cars_limits2;
  set cars_limits;
  by type;
  mean_value2 = lag1(mean_value);
  if origin = "Asia" then do;
    a_ll = mean_value - (1 * std_value);
    a_ul = mean_value + (1 * std_value);
  end;
  if origin = "USA" then do;
    b_ll = mean_value2 + ( mean_value - (1 * std_value) );
    b_ul = mean_value2 + ( mean_value + (1 * std_value) );
  end;
run;
   
/*
 * create graph
 */ 
proc sgplot data=cars_limits2;
  vbarparm category=type response=mean_value / group=origin groupdisplay=stack seglabel;
  highlow x=type low=a_ll high=a_ul / group=origin lowcap=serif highcap=serif lineattrs=(color=cx000000);
  highlow x=type low=b_ll high=b_ul / group=origin lowcap=serif highcap=serif lineattrs=(color=cx000000);
run;
BrunoMueller
SAS Super FREQ

Sorry I missed the point that this is all to be in Visual Analytics.

 

Have a look here https://communities.sas.com/t5/SAS-Visual-Analytics/Plot-of-Standard-Deviation-in-SAS-VA-Report-Desi... it is not a stacked bar chart, but might give you starting point.

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!

Tips for filtering data sources in SAS Visual Analytics

See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 675 views
  • 0 likes
  • 2 in conversation