Search code examples
group-bysassgplot

SAS: Merge two plots into one by group


I have two plots that I would like to merge into one. Each plot represents the proportion of present / not-present observations by their corresponding cumulative test results for the year

So on the plot I would like to see bars, side by side for groups of test scores but counting number of present to not-present

To represent this problem, this is what I have currently:

data test_scores;
    do i = 1 to 200;
        score = ranuni(200);
        output;
    end;
    drop i;
run;

data test_scores_2;
    set test_scores;
    if _n_ le 100 then flag = 0;
    else flag = 1;
run;

data test_scores_2_0 test_scores_2_1;
    set test_scores_2;
    if flag = 0 then output test_scores_2_0;
            else if flag = 1 then output test_scores_2_1;
run;

PROC GCHART 
    DATA=test_scores_2_0 
    ;
    VBAR 
    score
    /
    CLIPREF
    FRAME   
    LEVELS=20
    TYPE=PCT
    COUTLINE=BLACK
    RAXIS=AXIS1
    MAXIS=AXIS2
;
RUN;
QUIT;

PROC GCHART 
    DATA=test_scores_2_1
    ;
    VBAR 
    score
    /
    CLIPREF
    FRAME   
    LEVELS=20
    TYPE=PCT
    COUTLINE=BLACK
    RAXIS=AXIS1
    MAXIS=AXIS2
;
RUN;
QUIT;

bars should sum up to 100% for present bars should sum up to 100% for non-present

TIA


Solution

  • proc sgplot to the rescue. Use the group= option to specify two separate groups. Set the transparency to 50% so one histogram does not cover the other.

    proc sgplot data=test_scores_2;
        histogram score / group=flag transparency=0.5 binwidth=.05;
    run;
    

    enter image description here