Data visualization with SAS programming

proc gchart annotate target for each bar

Reply
Regular Contributor
Posts: 237

proc gchart annotate target for each bar

Dear,

I have several countries on my x-axis.

On the y-axis there is the value.

I need to set a different target for each country.

For example: FRANCE: min 10: bonus: 30

                    GERMANY: min 20: bonus: 50

I have this now:

proc gchart data=final_table;
  
   vbar country / sumvar=Value raxis=axis1
               nozero anno=anno
                DISCRETE ;

run;
Super User
Super User
Posts: 7,994

Re: proc gchart annotate target for each bar

Hi,

I answered a very similar question in this thread:

Simple answer - move to Graph Template Language.  You can then have dynamic variables in the template and specify them at run time.

The other method is to generate the code.

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

RW9, thanks for your answer.

I have created a second graph with the graph template. Now it looks like my Barchart. In my dataset i have SL, and WL. Which are 2 reference lines, but they are different for each element.

Which should i do next?

proc template;

    define statgraph barchart;

    begingraph;

      entrytitle "Average Mileage by Vehicle Type";

      layout overlay;

        barchart x=element  y=Conc /

          stat=sum orient=vertical ;

      endlayout;

      endgraph;

    end;

proc sgrender data=txrf_final_table template=barchart;

run;

Super User
Super User
Posts: 7,994

Re: proc gchart annotate target for each bar

Should be as simple as:

proc template;

    define statgraph barchart;

    begingraph;

      entrytitle "Average Mileage by Vehicle Type";

      layout overlay;

        barchart x=element  y=Conc /

          stat=sum orient=vertical ;

  refline sl / lineattrs=(pattern=dash);

  refline wl / lineattrs=(pattern=solid);

      endlayout;

      endgraph;

    end;

run;

proc sgrender data=txrf_final_table template=barchart;

run;

If you need reference on anything graph orientated, I would recommend looking at this blog: http://blogs.sas.com/content/graphicallyspeaking/

It has examples of pretty much everything, examples given in both sgplot and GTL in most cases.

SAS Super FREQ
Posts: 1,171

Re: proc gchart annotate target for each bar

RW9 has provided the code needed for this case using GTL.  I cannot see the result since the data is not included.  The REFLINE statements are SGPLOT versions and will not work in GTL.  With GTL, you need:

   Referenceline y=sl / lineattrs=(pattern=solid);

One reason to use GTL in this case would be the TARGET option.  See documentation.

Alternatively, you can also use SGPLOT procedure to do the same.  I used sashelp.cars to see some results. 

Refline values can be numbers, macro variables or columns in the data.

title  "Average Mileage by Vehicle Type";

proc sgplot data=sashelp.cars;

vbar type / response=mpg_city stat=mean;

refline 10 / lineattrs=(pattern=dash);

refline 20 / lineattrs=(pattern=solid);

run;

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

Posted in reply to Sanjay_SAS

Thanks for your answers but this is not what I need.

I need to draw a small line on each bar , one SL, one WL. The SL and WL is different for each BAR! Actually it is more lixe a boxplot, but without the boxes? Smiley Happy

SAS Super FREQ
Posts: 1,171

Re: proc gchart annotate target for each bar

In that case, you need to overlay two statements, one to draw the bar, one to draw the target.

Which version of SAS are you using (with maintenance level)?  Do you have SAS 9.3 or higher?

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

Posted in reply to Sanjay_SAS

We have SAS 9.2

For Base Product ...

   Custom version information: 9.21_M3

For SAS/STAT ...

   Custom version information: 9.22

For SAS/GRAPH ...

   Custom version information: 9.21_M2

SAS Super FREQ
Posts: 1,171

Re: proc gchart annotate target for each bar

You have less options with SAS 9.2, and may need to use GTL as suggested by RW9.  I would use an overlay of bars and scatter.

To get a line over the bar, you may need some more code.  Easier with SAS 9.3 or later.  :-)

proc means data=sashelp.cars;

  class type;

  var mpg_city mpg_highway;

  output out=cars(where=(_type_ > 0))

    mean (mpg_city mpg_highway)=city highway;

run;

proc template;

  define statgraph bars;

    begingraph;

      entrytitle  "Average Mileage by Vehicle Type";;

      layout overlay / yaxisopts=(offsetmin=0);

        barchart x=type y=city / name='a';

        scatterplot x=type y=highway / name='b';

        discretelegend 'a' 'b';

   endlayout;

  endgraph;

  end;

run;

proc sgrender data=cars template=bars;

run;

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

Posted in reply to Sanjay_SAS

thanks Sanjay, this is already more the way they want it to be. I will look further now to customize and change the icons.

SAS Super FREQ
Posts: 1,171

Re: proc gchart annotate target for each bar

Time to upgrade.  Smiley Happy  Anything higher than SAS 9.2 will provide you more options, including annotation.

I came into this thread a bit late because I figured GCHART with annotate would be the right solution, especially with SAS 9.2. 

Why did that not work?   

Super User
Super User
Posts: 7,994

Re: proc gchart annotate target for each bar

Just from my side here, I would do this kind of thing for normal ranges.  First I would take a distinct list and in that dataset have the higher lower for each parameter as this will differ.  From that dataset I then generate the code:

data refs;

     param="ABC"; higher=23; lower=4; output;

     param="DEF"; higher=12; lower=8; output;

run;

proc template;

     ...

     dynamic _h _l;

     ...

     Referenceline y=_h / lineattrs=(pattern=solid) name="UpperRange";

     Referenceline y=_l / lineattrs=(pattern=solid) name="LowerRange";

...

run;

data _null_;

     set refs;

     call execute(cats('proc sgrender data=have (where=(param="',param,'")) template=...; dynamic _h=',put(higher,best.),' _l=',put(lower,best.),'; run;');

run;

In this way the sgrender is called once per parameter and the dynamic variable is set to the respective range.

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

Yes, but the reference line can NOT be drawn over the whole graph. Only on the bar where it should be.

Regular Contributor
Posts: 237

Re: proc gchart annotate target for each bar

Hello all: i got this now:

proc template;

  define statgraph barchart;

    begingraph;

      entrytitle  "&wafersource_list";

      layout overlay /

  yaxisopts=(offsetmin=0

             label="Conc (E10A/cm2)"

  tickstyle=across);

        barchart x=element y=Conc / name='a'  fillattrs=(color=cxFFFF66)  ;

        scatterplot x=element y=WL / name='b' markerattrs=(symbol=HomeDown);

        scatterplot x=element y=SL / name='c' markerattrs=(symbol=HomeDownFilled);

        discretelegend 'a' 'b' 'c';

   endlayout;

  endgraph;

  end;

run;

What i would like to now is:

1: remove the border around the graph

2: put more ticks on the y ax

any ideas?

Super User
Super User
Posts: 7,994

Re: proc gchart annotate target for each bar

Well, to remove the border it should be as straightforward as:

ods graphics / reset=all height=15cm width=19cm noborder;

For more tickaxis values:

yaxisopts=(offsetmin=0 label="Conc (E10A/cm2)" tickstyle=across tickvaluesequence=(start=0 end=100 increment=10));

                                                                                                                                         ^         ^                     ^

Change these to your ranges.  You could also put a list in there and specify exactly what ticks you want.

Ask a Question
Discussion stats
  • 16 replies
  • 1028 views
  • 6 likes
  • 4 in conversation