BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

7 REPLIES 7
DavidHD
SAS Employee

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

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
Thank you for the help but this isn't exactly what I want. I tried this previously and this provides the percentage for each column so the line moves up and down. I would like a cumulative percentage line, in other words a line that goes up the entire graph from 0 to 100%
PeterClemmensen
Tourmaline | Level 20

Can you provide an outline of what your desired graph looks like? Makes the whole thing easier.

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Attached is what excel can do in a PDF but with example data

PeterClemmensen
Tourmaline | Level 20

This plot does not have anything to do with the sample data you posted, correct? Can you post the data behind this plot?

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
The plot is just an example of how I want the graph to look in the end. It is the same as type data as I post originally, just applying a format to the numbers
PeterClemmensen
Tourmaline | Level 20

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1615 views
  • 1 like
  • 3 in conversation