Percentage of SAS histogram for different variables (one variable measured along the y-axis and the other by shading in bars)

I've asked this question before but haven't found an answer yet. I am trying to create a group of bars in SAS that shows the percentage of patients who passed the test by category and inside the bar shows where the tests were received (location). My dataset looks like this:

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

      

If each patient is listed with a risk classification, it is assigned a value (Category variable), an indicator variable indicating whether they received the test (Test variable, where 1 = Test received, 0 '= Test did not receive), and if they got a test where this test passed (variable 'test_location').

I want to create a histogram with categories on the x axis and yaxis shows the percentage of patients who received the test (test = 1) and then each bar is shaded to show the composition of patients who received the test in each category for the location (i.e. how many tests were conducted on sites 1, 2 and 3).

I have the code below, but it doesn't give me the percentages I want. This gives me pct_col the output of the category test * and I want pct_row. In other words, I want the y-axis to measure the percentage of patients tested out of the total number of patients in each category, not all patients who received testing in any category, as it gives me.

An example of what I want: In the dummy dataset below for high-risk patients, for example, I need a bar that shows 75% (12 patients with trials out of 16 high-risk patients) that passed the tests, and then obscure the bar. to show that 41.66% of these trials were on Site 1, 33.34% on Site 2 and 25% on Site 3. And so on for the intermediate and low risk categories. If there is a way to label subsections with precise percentages, that's great too.

Dummy dataset:

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
;

      

Thank!

EDIT;

Here's a rough graph of what I'm looking for output to SAS (using the dummy data above): enter image description here

Using this 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;

      

I am getting this output: enter image description here

So what I have is that I am not giving me the correct denominators for my percentages. I also could not find a way to mark the individual subsections of the graph like in my example.

Thank!

+3


source to share


1 answer


You can get exactly what you want with a data step bit and some formatting. This will be slightly different from your working code. As others have pointed out, there are many helpful examples on the Robert Ellison Site.

I would use the simple solution below, this is almost what you asked for and very close to your working code. The main difference is that missing values ​​are their own category.

The key lines are:

  • Use pctlevel=group

  • Use missing



Here is the code:

proc sgplot data = test 
    pctlevel = group
;
    vbar category / 
        stat       = percent
        group      = test_location 
        grouporder = data
        missing
        seglabel
    ;
    keylegend / 
        title    = "Testing Location" 
        position = bottom
    ;
quit;

      

I get:

enter image description here

+2


source







All Articles