qifengwu
qifengwu

Reputation: 129

SAS Proc Template Bar Chart - Unwanted GROUP

I am trying to create a bar chart for different groups with percentage of each by VISIT. The full code is attached below.

While showing each count by group, there are some missing groups presenting which is not expected.

Could someone take a look to remove those unexisting groups?

For example, For VISIT 1, group A with N=4 and GROUP B with N=9 should not be showing up in here.

Another issue related to this is the 'ods escapechar' option does not work under Proc template.

enter image description here

    data datain;
    input VISIT $ GRP $ TEST $ PCT TOTAL_Value Number_of;
    datalines;
    VISIT1 GROUPA AAA% 20.49  15.7 7
    VISIT1 GROUPA BBB% 4.02   15.7 7
    VISIT1 GROUPA CCC% 15.51  15.7 7
    VISIT1 GROUPA DDD% 60.88  15.7 7

    VISIT1 GROUPB AAA% 30.02  21.4 5
    VISIT1 GROUPB BBB% 9.45   21.4 5
    VISIT1 GROUPB CCC% 25.55  21.4 5
    VISIT1 GROUPB DDD% 34.98  21.4 5

    VISIT2 GROUPA AAA% 20.00  28.6 4
    VISIT2 GROUPA BBB% 4.00   28.6 4
    VISIT2 GROUPA CCC% 16.00  28.6 4
    VISIT2 GROUPA DDD% 60.00  28.6 4

    VISIT2 GROUPB AAA% 30.00  35.9 9
    VISIT2 GROUPB BBB% 9.00   35.9 9
    VISIT2 GROUPB CCC% 26.00  35.9 9
    VISIT2 GROUPB DDD% 35.00  35.9 9
run;

data datain1;
     set datain;
     length GRP_ $20;
     GRP_=strip(GRP)||'`N='||STRIP(PUT(Number_of,2.));
run;

ods escapechar='`';
ODS LISTING CLOSE;
proc template;
    define statgraph barchart;
        begingraph / datacolors=(YELLOW ORANGE GREEN purple) border=false;
            layout datalattice columnvar=VISIT/ 
                headerlabeldisplay=value 
                headerlabelattrs=(weight=bold)
                rowaxisopts=( Label=' ' offsetmin=0) columnaxisopts=(offsetmin=0.2 offsetmax=0.2 display=(ticks tickvalues)
                             griddisplay=off linearopts=( viewmin=0 viewmax=62 tickvaluesequence=(start=0 end=62 increment=5))); 
                layout prototype /walldisplay=none; 

                BarChartParm X=GRP_ Y=PCT / Group=TEST barwidth=0.5;

                endlayout;
                endlayout;
                layout globallegend;
                endlayout;
        endgraph;
    end;
run;
proc sgrender data=datain1 template=barchart;
run;

ods rtf close;
ods listing;

Upvotes: 0

Views: 792

Answers (1)

Richard
Richard

Reputation: 27508

The DATALATTICE creates a uniform horizontal axis over all the crossings as a basis for simple visual comparison. That is why you get tick marks having zero count.

SGPANEL might be a simpler syntax that does some 'grunt' work you might have had to do yourself if doing just GTL. The timesaver is PANELBY / UNISCALE= option.

This chart is very close to what you want and might only need color tweaks.

proc sgpanel data=datain1;
  panelby visit / uniscale=row;
  vbar grp_ / group=test stat=sum response=pct;
run;

You also might want to change the GRP_ assignment to

 GRP_ = catx(' ', GRP, Cats('N=',Number_of));

Upvotes: 1

Related Questions