## How to divide plot into smaller equal segment and find area under the curve?

Solved
Frequent Contributor
Posts: 87

# How to divide plot into smaller equal segment and find area under the curve?

[ Edited ]

For the following data, what I am trying to learn are:

1. plot and find area under the curve (which is calculated using either 1. Simpson's rule (https://en.wikipedia.org/wiki/Simpson%27s_rule) or, 2. Trapezoidal rule (https://en.wikipedia.org/wiki/Trapezoidal_rule)  - Not sure if there are SAS's inbuilt functions for that.

2. Find the mid point on the plot, divide the plot into two 'A' and 'B' and then divide each of 'A' and 'B' in equal smaller segments (like black lines on the plot for section 'A' shown in the below pic, and calculate area for each segment (say Seg1 = xx  mm2, Seg1 = xx1  mm2 and so forth)

``````data sample;
infile cards truncover expandtabs;
input X Y;
cards;
29 21
18 23
28 24
16 26
3 27
18 29
2 33
3 37
26 39
2 42
25 47
9 54
13 57
17 58
29 60
5 63
23 66
4 69
3 72
17 73
7 73
12 72
8 69
20 66
12 63
8 60
28 58
3 57
18 54
11 47
21 42
8 39
1 37
16 29
3 27
17 22
3 19
6 17
19 14
18 10
;
run;``````

I was trying something like this...

``````data Trapezoidal;
set x_y end=last;
retain integral;
lag_x=lag(x); lag_y = lag(y);
if _N_ eq 1 then integral = 0;
else integral = integral + (x - lag_x) * (y + lag_y) / 2;
run;``````

Thanks.

Accepted Solutions
Solution
‎12-13-2016 06:48 AM
Super User
Posts: 10,848

## Re: How to divide plot into smaller equal segment and find area under the curve?

Because that would be messed up .

Really suggest you post it at ODS Graphic forum.

``````
data sample;
infile cards truncover expandtabs;
input X Y;
cards;
29 21
18 23
28 24
16 26
3 27
18 29
2 33
3 37
26 39
2 42
25 47
9 54
13 57
17 58
29 60
5 63
23 66
4 69
3 72
17 73
7 73
12 72
8 69
20 66
12 63
8 60
28 58
3 57
18 54
11 47
21 42
8 39
1 37
16 29
3 27
17 22
3 19
6 17
19 14
18 10
;
run;

proc sort data=sample ;
by x;
run;
proc sql;
select mean(x) into : x from sample ;
quit;
proc sgplot data=sample;
needle x=x y=y;
spline x=x y=y / lineattrs=graphdata4(thickness=4);
refline &x / axis=x lineattrs=(color=red thickness=4);
run;

data Trapezoidal;
set sample end=last;
dif_x=dif(x);
mean_y=mean(lag(y),y);
integral + (dif_x*mean_y);
if last then putlog 'area under curve is ' integral;
run;``````

All Replies
Super User
Posts: 10,848

## Re: How to divide plot into smaller equal segment and find area under the curve?

```
data sample;
infile cards truncover expandtabs;
input X Y;
cards;
29 21
18 23
28 24
16 26
3 27
18 29
2 33
3 37
26 39
2 42
25 47
9 54
13 57
17 58
29 60
5 63
23 66
4 69
3 72
17 73
7 73
12 72
8 69
20 66
12 63
8 60
28 58
3 57
18 54
11 47
21 42
8 39
1 37
16 29
3 27
17 22
3 19
6 17
19 14
18 10
;
run;

proc sort data=sample ;
by x;
run;

proc sgplot data=sample;
needle x=x y=y;
run;

data Trapezoidal;
set sample end=last;
dif_x=dif(x);
mean_y=mean(lag(y),y);
integral + (dif_x*mean_y);
if last then putlog 'area under curve is ' integral;
run;

```
Frequent Contributor
Posts: 87

## Re: How to divide plot into smaller equal segment and find area under the curve?

Thanks for your help KSharp. I am just wondering why needle chart used and not line plot. Also, can we plot a vertical line (on X axis, at Y=MaxVal) in the middle of the plot.
Solution
‎12-13-2016 06:48 AM
Super User
Posts: 10,848

## Re: How to divide plot into smaller equal segment and find area under the curve?

Because that would be messed up .

Really suggest you post it at ODS Graphic forum.

``````
data sample;
infile cards truncover expandtabs;
input X Y;
cards;
29 21
18 23
28 24
16 26
3 27
18 29
2 33
3 37
26 39
2 42
25 47
9 54
13 57
17 58
29 60
5 63
23 66
4 69
3 72
17 73
7 73
12 72
8 69
20 66
12 63
8 60
28 58
3 57
18 54
11 47
21 42
8 39
1 37
16 29
3 27
17 22
3 19
6 17
19 14
18 10
;
run;

proc sort data=sample ;
by x;
run;
proc sql;
select mean(x) into : x from sample ;
quit;
proc sgplot data=sample;
needle x=x y=y;
spline x=x y=y / lineattrs=graphdata4(thickness=4);
refline &x / axis=x lineattrs=(color=red thickness=4);
run;

data Trapezoidal;
set sample end=last;
dif_x=dif(x);
mean_y=mean(lag(y),y);
integral + (dif_x*mean_y);
if last then putlog 'area under curve is ' integral;
run;``````

☑ This topic is solved.