saslabelsgplot

Different label for each bar in a bar chart using Proc sgplot


I'm trying to add the number of each group into the labels of groups on xaxis using Proc sgplot in SAS. Here is the data and the graph I'd like to have. I want to have sample of each bar on xaxis (hand writen parts). Your help is very much appreciated!

Data have ;
input type  sex $  n n_total percent ;
datalines;
0  F  6    29  20.7 
1  F  387  496 78.2  
0  M  4    15  26.6
1  M  264  305 86.5
;
Run; 

proc sgplot data=have ;
vbarparm category= type  response=percent /group=sex groupdisplay=cluster datalabel;
run; 

Graph that I want to create: enter image description here


Solution

  • You can compute a bar data label that shows the percent value and the text n=<N>

    Example:

    Data have ;
    input type  sex $  n n_total percent ;
    datalines;
    0  F  6    29  20.7 
    1  F  387  496 78.2  
    0  M  4    15  26.6
    1  M  264  305 86.5
    ;
    Run; 
    
    data plot;
      set have;
      barlabel = cats(percent) || ' %N/(n=' || cats(n_total, ')');
    run;
    
    proc sgplot data=plot;
      vbarparm 
        category=type  
        response=percent 
      / group=sex 
        groupdisplay=cluster 
        datalabel=barlabel datalabelfitpolicy=splitalways splitchar='/'
      ;
      label percent = 'Percent N having some attribute';
    run; 
    

    enter image description here