BookmarkSubscribeRSS Feed
srinivaschary
Calcite | Level 5

Hi,

How to get data labels for all bars if i select grouping style is stack.

 

 

Srinivas

3 REPLIES 3
rogerjdeangelis
Barite | Level 11
SAS: Stacked Vertical Bar chart(histogram) with bar segmants labeled

for sqgplot see
https://dl.dropboxusercontent.com/u/15716238/pie_1511nov_bargrf.pdf

inspired by
https://goo.gl/vvLpFH
https://communities.sas.com/t5/SAS-Visual-Analytics/How-to-get-data-labels-for-all-bars-if-i-select-grouping-style/m-p/340760


HAVE DATA and Format
====================

Up to 40 obs from bargrf total obs=9

Obs    substatus    frequency    statusdes                   des

 1     BBB01          6.0005       New Students        PreK 3(1.8%)
 2     BBB11         37.0010       New Students        1st 37(22.3%)
 3     BBB21         11.0015       New Students        2nd 11(6.6%)
 4     BBB31         13.0020       New Students        3rd 13(7.8%)
 5     BBB41         13.0025       New Students        4th 13(7.8%)
 6     EEE16_40       4.0035        OutOfDistrict      1st-8th 1(0.6%)
 7     EEE1_15       12.0040        OutOfDistrict      2nd-8th 12(7.2%)
 8     ALLCHA        33.0030         Local Students    All 33(19.9%)
 9     ALLNA         43.0045          N/A              All 43(25.9%)


Thi sis the cntlin for proc formats

Up to 40 obs from barfmt total obs=9

Obs    fmtname    fuzz     start       end           label

 1     lbl2pnt      0      6.0005     6.0005    PreK 3(1.8%)
 2     lbl2pnt      0     37.0010    37.0010    1st 37(22.3%)
 3     lbl2pnt      0     11.0015    11.0015    2nd 11(6.6%)
 4     lbl2pnt      0     13.0020    13.0020    3rd 13(7.8%)
 5     lbl2pnt      0     13.0025    13.0025    4th 13(7.8%)
 6     lbl2pnt      0      4.0035     4.0035    1st-8th 1(0.6%)
 7     lbl2pnt      0     12.0040    12.0040    2nd-8th 12(7.2%)
 8     lbl2pnt      0     33.0030    33.0030    All 33(19.9%)
 9     lbl2pnt      0     43.0045    43.0045    All 43(25.9%)


WANT (example not the data)

frequency Sum

   |       BBBBBBB
   |       B     B
   |       B 28  B
20 +       B     B
   |       B     B       BBBBBBB
   |       BBBBBBB       B     B
   |       AAAAAAA       B 15  B
   |       A     A       B     B
10 +       A     A       B     B
   |       A 13  A       BBBBBBB
   |       A     A       AAAAAAA
   |       A     A       A 5   A
   |       AAAAAAA       AAAAAAA
   --------------------------------

       New Students  Out of District

Symbol substatus     Symbol substatus

   A   1st Grade           B  1nd Grade

WORKING CODE
============

proc sgplot data=bargrf noautolegend ;
vbar statusdes/group=substatus  response=frequency seglabel
      seglabelattrs=(family="Arial" size=12pt)  seglabelformat=lbl2pnt.;

FULL SOLUTION
=============

* macro on end;
proc datasets lib=work kill;
run;quit;

%let outpdf01=d:/pdf/bargrf.pdf;

%utlopts;
options orientation=landscape validvarname=v7;
%utl_pielan100(
     topmargin=.5in
    ,TitleFont=22pt
    ,docfont=20pt
    ,fixedfont=18pt
    ,rules=all
    ,frame=box
);

ods listing close;
ods pdf close;
ods path work.templat(update) sasuser.templat(update) sashelp.tmplmst(read);
%utlfkil(&outpdf01.);
ods noptitle;
ods escapechar='^';
ods listing close;
ods graphics on / width=10in  height=7in ;
ods pdf file="&outpdf01." style=utl_pielan100 notoc;

proc sql;
create table bargrf(substatus varchar(10), frequency float,
  statusdes varchar(18), des varchar(24));
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BBB01', 6.0005, '  New Students', 'PreK 3(1.8%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BBB11', 37.001, '  New Students', '1st 37(22.3%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BBB21', 11.0015, '  New Students', '2nd 11(6.6%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BBB31', 13.002, '  New Students', '3rd 13(7.8%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BBB41', 13.0025, '  New Students', '4th 13(7.8%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('EEE16_40', 4.0035, '   OutOfDistrict', '1st-8th 1(0.6%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('EEE1_15', 12.004, '   OutOfDistrict', '2nd-8th 12(7.2%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('ALLCHA', 33.003, '    Local Students', 'All 33(19.9%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('ALLNA', 43.0045, '     N/A', 'All 43(25.9%)');
quit;

data barfmt;
 retain fmtname "lbl2pnt" fuzz 1e-10;
 set bargrf;
 start=frequency;
 end=start;
 label=des;
 keep fmtname fuzz start end label;
;run;quit;

proc format cntlin=barfmt;
run;quit;

* output the graph;
proc sgplot data=bargrf noautolegend ;
title "Minority Percentages for Local Schoold(fake data)";
vbar statusdes/group=substatus  response=frequency seglabel
      seglabelattrs=(family="Arial" size=12pt)  seglabelformat=lbl2pnt.;
yaxis labelattrs=(size=15pt) valueattrs=(size=13pt)
      label="Number of Students"  DISCRETEORDER=Data;;
xaxis labelattrs=(size=12pt) valueattrs=(size=12pt) display=(nolabel) ;
run;

ods graphics off;
ods pdf close;
ods listing;


%Macro utl_pielan100
    (
      style=utl_pielan100
      ,frame=box
      ,TitleFont=13pt
      ,docfont=13pt
      ,fixedfont=12pt
      ,rules=ALL
      ,bottommargin=.25in
      ,topmargin=.25in
      ,rightmargin=.25in
      ,leftmargin=.25in
      ,cellheight=13pt
      ,cellpadding = 2pt
      ,cellspacing = .2pt
      ,borderwidth = .2pt
    ) /  Des="SAS PDF Template for PDF";

ods path work.templat(update) sasuser.templat(update) sashelp.tmplmst(read);

Proc Template;

   define style &Style;
   parent=styles.rtf;

        replace body from Document /

               protectspecialchars=off
               asis=on
               bottommargin=&bottommargin
               topmargin   =&topmargin
               rightmargin =&rightmargin
               leftmargin  =&leftmargin
               ;

        replace color_list /
              'link' = blue
               'bgH'  = _undef_
               'fg'  = black
               'bg'   = _undef_;

        replace fonts /
               'TitleFont2'           = ("Arial, Helvetica, Helv",&titlefont,Bold)
               'TitleFont'            = ("Arial, Helvetica, Helv",&titlefont,Bold)

               'HeadingFont'          = ("Arial, Helvetica, Helv",&titlefont)
               'HeadingEmphasisFont'  = ("Arial, Helvetica, Helv",&titlefont,Italic)

               'StrongFont'           = ("Arial, Helvetica, Helv",&titlefont,Bold)
               'EmphasisFont'         = ("Arial, Helvetica, Helv",&titlefont,Italic)

               'FixedFont'            = ("Courier New, Courier",&fixedfont)
               'FixedEmphasisFont'    = ("Courier New, Courier",&fixedfont,Italic)
               'FixedStrongFont'      = ("Courier New, Courier",&fixedfont,Bold)
               'FixedHeadingFont'     = ("Courier New, Courier",&fixedfont,Bold)
               'BatchFixedFont'       = ("Courier New, Courier",&fixedfont)

               'docFont'              = ("Arial, Helvetica, Helv",&docfont)

               'FootFont'             = ("Arial, Helvetica, Helv", 9pt)
               'StrongFootFont'       = ("Arial, Helvetica, Helv",8pt,Bold)
               'EmphasisFootFont'     = ("Arial, Helvetica, Helv",8pt,Italic)
               'FixedFootFont'        = ("Courier New, Courier",8pt)
               'FixedEmphasisFootFont'= ("Courier New, Courier",8pt,Italic)
               'FixedStrongFootFont'  = ("Courier New, Courier",7pt,Bold);

        replace GraphFonts /
               'GraphDataFont'        = ("Arial, Helvetica, Helv",&fixedfont)
               'GraphValueFont'       = ("Arial, Helvetica, Helv",&fixedfont)
               'GraphLabelFont'       = ("Arial, Helvetica, Helv",&fixedfont,Bold)
               'GraphFootnoteFont'    = ("Arial, Helvetica, Helv",8pt)
               'GraphTitleFont'       = ("Arial, Helvetica, Helv",&titlefont,Bold)
               'GraphAnnoFont'        = ("Arial, Helvetica, Helv",&fixedfont)
               'GraphUnicodeFont'     = ("Arial, Helvetica, Helv",&fixedfont)
               'GraphLabel2Font'      = ("Arial, Helvetica, Helv",&fixedfont)
               'GraphTitle1Font'      = ("Arial, Helvetica, Helv",&fixedfont);

        style Graph from Output/
                outputwidth = 100% ;

        style table from table /

                protectspecialchars=on
                asis=on
                background = colors('tablebg')
                frame=&frame
                rules=&rules
                cellheight  = &cellheight
                cellpadding = &cellpadding
                cellspacing = &cellspacing
                bordercolor = colors('tableborder')
                borderwidth = &borderwidth;

         replace Footer from HeadersAndFooters

                / font = fonts('FootFont')  just=left asis=on protectspecialchars=off ;

                replace FooterFixed from Footer
                / font = fonts('FixedFootFont')  just=left asis=on protectspecialchars=off;

                replace FooterEmpty from Footer
                / font = fonts('FootFont')  just=left asis=on protectspecialchars=off;

                replace FooterEmphasis from Footer
                / font = fonts('EmphasisFootFont')  just=left asis=on protectspecialchars=off;

                replace FooterEmphasisFixed from FooterEmphasis
                / font = fonts('FixedEmphasisFootFont')  just=left asis=on protectspecialchars=off;

                replace FooterStrong from Footer
                / font = fonts('StrongFootFont')  just=left asis=on protectspecialchars=off;

                replace FooterStrongFixed from FooterStrong
                / font = fonts('FixedStrongFootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooter from Footer
                / font = fonts('FootFont')  asis=on protectspecialchars=off just=left;

                replace RowFooterFixed from RowFooter
                / font = fonts('FixedFootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooterEmpty from RowFooter
                / font = fonts('FootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooterEmphasis from RowFooter
                / font = fonts('EmphasisFootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooterEmphasisFixed from RowFooterEmphasis
                / font = fonts('FixedEmphasisFootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooterStrong from RowFooter
                / font = fonts('StrongFootFont')  just=left asis=on protectspecialchars=off;

                replace RowFooterStrongFixed from RowFooterStrong
                / font = fonts('FixedStrongFootFont')  just=left asis=on protectspecialchars=off;

                replace SystemFooter from TitlesAndFooters / asis=on
                        protectspecialchars=off just=left;
    end;
run;
quit;

%Mend utl_pielan100;

Example histogram

proc sql;
create table bargrf(substatus varchar(10), frequency float,
  statusdes varchar(18), des varchar(24));
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BB1', 13.002, 'New Students', '3rd 13(7.8%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BB2', 13.0025, 'New Students', '4th 13(7.8%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BB1', 4.0035, 'OutOfDistrict', '1st-8th 1(0.6%)');
Insert into bargrf(substatus, frequency, statusdes, des)
Values('BB2', 12.004, 'OutOfDistrict', '2nd-8th 12(7.2%)');
quit;

proc chart data=bargrf ;
  vbar statusdes/ sumvar=frequency subgroup=substatus;
run;quit;


Rick_SAS
SAS Super FREQ

Recent releases of PROC SGPLOT support the SEGLABEL option on the VBAR statement.  By default, labels are "thinned" if a bar is extremely small, but you can use SEGLABELFITPOLIFY=NONE to force labels for all bars:

 

proc sgplot data=sashelp.cars;
vbar type / group=origin groupdisplay=stack
            seglabel seglabelfitpolicy=none;
run;
snoopy369
Barite | Level 11

If you don't have a new enough version of SAS to use SEGLABEL, there are several options - annotate, GTL, High-Low plots, etc.

 

I cover some in my paper Labelling Without the Hassle: http://www.mwsug.org/proceedings/2014/DV/MWSUG-2014-DV04.pdf

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
  • 3 replies
  • 1334 views
  • 1 like
  • 4 in conversation