turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- slope across variables in each row

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-04-2016 05:10 PM

How do I calculate the slope of few variables for each row assuming delta X between variables is =1

Example:

ID | v1 | v2 | v3 | v4 | v5 | slope |

a | 2 | 4 | 6 | 8 | 10 | ? |

b | 60 | 70 | 95 | 106 | 120 | ? |

c | ||||||

d | ||||||

e |

Thanks

SAS 6.1

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to timeless

11-04-2016 05:30 PM

Do you mean by "delta x between variables" that you have the equivalent of coordinates such as (1,2), (2,4), (3,6), (4,8) and (5,10)?

And do you want the slope of a best fit line through the points? The first line has a constant slop of 2 between any two pairs but the second row has different slopes between different pairs so a request for a single slope needs some clarification.

If the slope of a "best fit" line is desired I would transform the data into actual x,y coordinates and then run the result through proc reg with the Id as a by variable to get the result for each group:

```
data have;
input ID $ v1 v2 v3 v4 v5 ;
datalines;
a 2 4 6 8 10
b 60 70 95 106 120
;
run;
data trans;
set have;
array v v1-v5;
do x= 1 to 5;
y= v[x];
output;
end;
keep id x y;
run;
proc reg data=trans;
by id;
model y=x;
run;
```

The Parameter estimate for X is the slope you may want.

If you want to merge that back onto the original research the options for Prog Reg on output to get a data set and then merge by ID.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to timeless

11-04-2016 05:43 PM - edited 11-04-2016 05:44 PM

This assumes a linear trend, with your X's being 1-5 in your case and **does not deal with missing values.**

In this case there are 6 obs but changing it to 5 is a trivial task.

Overall, I would recommend a transpose and proc reg instead as well.

```
data test;
input x1 x2 x3 x4 x5 x6;
datalines;
-0.069965723 0.492749371 0.955245597 1.346963522 1.701118239 2.523141195
;
run;
data slope;
set test;
array ys(6) x1-x6;
array vals(6) (1 2 3 4 5 6);
xbar = mean(of vals(*));
ybar = mean(of ys(*));
do i=1 to dim(vals);
num=sum(num, (vals(i)-xbar)*(ys(i)-ybar));
den=sum(den, (vals(i)-xbar)**2);
end;
slope = num/den;
run;
```