Using SAS 9.4
I am trying the code below to overlay a line on a bar graph:
proc format;
value impng_cat 0 - 19.99 = '0-19'
20 - 39.99 = '20-39'
40 - 59.99 = '40-59'
60 - 79.99 = '60-79'
80 - 99.99 = '80-99'
100 - 119.99 = '100-119'
120 - 139.99 = '120-139';
run;
proc sgplot data= check;
format impingment_point impng_cat.;
vbar impingment_point;
vline cum_pct;
xaxis label= '';
yaxis values=(0 5 10 15 20 25 30 35 40 45) label= 'Number of Patients';
label cum_pct = 'Cumulative Percent';
run;
However, I get the following error when I run the code:
"ERROR: The same category variable must be used for summarized plots."
Example of data:
data have;
input id impingment_point cum_pct delimiter= ',';
datalines;
1, 33, 2
2, 120, 100
3, 80, 80
4, 60, 59
5, 45, 19
;
Ideally, I would like
1. bar graph with a line of the cummulative percentage
2. Also, is it possible to show a category with 0 data point on a bar graph? The first category of 0-19.99 has 0 observations but I would like to be able to show this on the bar graph as well.
Thank you for any thoughts or suggestions
I'm still not sure I understand the structure of your data. However, I think you can use this as a template. It definitely contains all the building blocks to answer both question 1 and two.
Feel free to ask.
data have;
input id impingment_point cum_pct;
infile datalines dlm = ',';
datalines;
1, 33, 2
2, 120, 100
3, 80, 80
4, 60, 59
5, 45, 19
;
proc format;
value impng_cat
0 - 19.99 = '0-19'
20 - 39.99 = '20-39'
40 - 59.99 = '40-59'
60 - 79.99 = '60-79'
80 - 99.99 = '80-99'
100 - 119.99 = '100-119'
120 - 139.99 = '120-139';
run;
proc summary data=have nway completetypes;
class impingment_point / preloadfmt missing;
format impingment_point impng_cat.;
output out=temp;
run;
data plot;
do until (z1);
set temp end = z1;
s + _FREQ_;
end;
do until (z2);
set temp end = z2;
c = _FREQ_ / s;
cc + c;
output;
end;
run;
proc sgplot data=plot;
vbarparm category = impingment_point response = _FREQ_ / nozerobars;
series x = impingment_point y = cc / y2axis;
format impingment_point impng_cat. cc percent.;
run;
Hey @GS2 ,
is this what you are looking for?
data have;
infile datalines delimiter=',';
input id impingment_point cum_pct;
datalines;
1,33,2
2,120,100
3,80,80
4,60,59
5,45,19
;
run;
proc format;
value impng_cat 0 - 19.99 = '0-19'
20 - 39.99 = '20-39'
40 - 59.99 = '40-59'
60 - 79.99 = '60-79'
80 - 99.99 = '80-99'
100 - 119.99 = '100-119'
120 - 139.99 = '120-139';
run;
proc sgplot data= have;
format impingment_point impng_cat.;
vbar impingment_point;
vline impingment_point / response=cum_pct y2axis;
xaxis label= '' type=discrete;
yaxis values=(0 5 10 15 20 25 30 35 40 45) label= 'Number of Patients';
label cum_pct = 'Cumulative Percent';
run;
For posting code in the future please use the Insert SAS Code Button in the editor.
For the display of a bar without any values I have no solution other than to add a row per missing value, not ideal I know but maybe color the missing value bar with something like mentioned here: https://communities.sas.com/t5/Graphics-Programming/How-to-make-a-bar-chart-and-assign-the-bar-color...
Hope this helps and kind regards
David
Can you provide an outline of what your desired graph looks like? Makes the whole thing easier.
Attached is what excel can do in a PDF but with example data
This plot does not have anything to do with the sample data you posted, correct? Can you post the data behind this plot?
I'm still not sure I understand the structure of your data. However, I think you can use this as a template. It definitely contains all the building blocks to answer both question 1 and two.
Feel free to ask.
data have;
input id impingment_point cum_pct;
infile datalines dlm = ',';
datalines;
1, 33, 2
2, 120, 100
3, 80, 80
4, 60, 59
5, 45, 19
;
proc format;
value impng_cat
0 - 19.99 = '0-19'
20 - 39.99 = '20-39'
40 - 59.99 = '40-59'
60 - 79.99 = '60-79'
80 - 99.99 = '80-99'
100 - 119.99 = '100-119'
120 - 139.99 = '120-139';
run;
proc summary data=have nway completetypes;
class impingment_point / preloadfmt missing;
format impingment_point impng_cat.;
output out=temp;
run;
data plot;
do until (z1);
set temp end = z1;
s + _FREQ_;
end;
do until (z2);
set temp end = z2;
c = _FREQ_ / s;
cc + c;
output;
end;
run;
proc sgplot data=plot;
vbarparm category = impingment_point response = _FREQ_ / nozerobars;
series x = impingment_point y = cc / y2axis;
format impingment_point impng_cat. cc percent.;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.