title: SAS bar graph measuring percentage for different variables (one variable measured on y axis and the other shown by shading within the bars)
body: <p>I have asked this question before but haven't found an answer yet. I am trying to create a bar group in SAS which shows the percentage of patients that received a test by category and within in bar, show the location where the tests were received (location). My dataset looks like this:</p>
  
  <pre><code>Category            Test        Test_location
  High Risk           1           Site 1
  Intermediate Risk   1           Site 2
  Low Risk            0           .
  Intermediate Risk   0           .
  High Risk           1           Site 3
  </code></pre>
  
  <p>Where each patient is listed with the risk classification they have been assigned to (variable 'Category'), an indicator variable that shows whether or not they received a test (variable 'test' where '1'=received test and '0'=did not receive test) and, if they received a test, where that test took place (variable 'test_location').</p>
  
  <p>I want to create a bar graph with the categories on the x axis and the yaxis showing the percentage of patients who got a test (test=1), and then each bar shaded to show the composition of patients who got a test in each category for location (ie: how many tests occurred in Site 1, 2 and 3).</p>
  
  <p>I have the below code, but it is not giving me the percentages that I want. It gives me a pct_col output of test*category, and I want pct_row. In other words, I want the y axis to measure the percentage of patients with testing out of the total number of patients in each category, not out of all patients who receiving testing in any category like it is giving me.</p>
  
  <p>Example of what I want: In the dummy dataset below, for high risk patients, for example, I want a bar that shows 75% (12 patients with tests out of the total 16 high risk patients) received tests, and then have the bar shaded to show 41.66% of those test were at Site 1, 33.34% at Site 2 and 25% at Site 3. And so on for the intermediate and low risk categories. If there is a way to label the subsections with the exact percentages, that would be great too.</p>
  
  <p>Dummy data set:</p>
  
  <pre><code>data test;
  infile datalines missover;
     input ID Category $ Test Test_location $;
     datalines;
  1 High 1 Site_1
  2 High 1 Site_1
  3 High 1 Site_1
  4 High 1 Site_1
  5 High 1 Site_1
  6 High 1 Site_2
  7 High 1 Site_2
  8 High 1 Site_2
  9 High 1 Site_2
  10 High 1 Site_3
  11 High 1 Site_3
  12 High 1 Site_3
  13 High 0
  14 High 0
  15 High 0
  16 High 0
  17 Intermediate 1 Site_1
  18 Intermediate 1 Site_1
  19 Intermediate 1 Site_2
  20 Intermediate 0
  21 Intermediate 0
  22 Intermediate 0
  23 Intermediate 0
  24 Intermediate 0
  25 Intermediate 0
  26 Low 1 Site_1
  27 Low 1 Site_1
  28 Low 1 Site_1
  29 Low 1 Site_2
  30 Low 1 Site_2
  31 Low 1 Site_2
  32 Low 1 Site_3
  33 Low 0
  34 Low 0
  35 Low 0
  36 Low 0
  37 Low 0
  38 Low 0
  ;
  </code></pre>
  
  <p>Thank you!</p>
  
  <p>EDIT;</p>
  
  <p>Here is a sample graph of what I am looking to output in SAS (using the dummy data above):
  <img src="https://i.stack.imgur.com/Hgyrh.png" alt="enter image description here"></p>
  
  <p>Using this code:</p>
  
  <pre><code>proc sgplot data=test pctlevel=graph;
      vbar category / response=test stat=percent
          group=test_location groupdisplay=stack datalabel;
      keylegend /title="Testing Location" position=bottom;
  quit;
  </code></pre>
  
  <p>I get this output:
  <img src="https://i.stack.imgur.com/jQSps.png" alt="enter image description here"></p>
  
  <p>So what I have is not giving my the correct denominators for my percents. I also couldn't figure out a way to label the individual subsections of the graph like I have in my sample figure.</p>
  
  <p>Thank you!</p>