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
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.