BookmarkSubscribeRSS Feed
timeless
Fluorite | Level 6

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

 

Example:

IDv1v2v3v4v5slope
a246810?
b607095106120?
c      
d      
e      

Thanks

 

SAS 6.1

2 REPLIES 2
ballardw
Super User

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.

Reeza
Super User

 

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;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2710 views
  • 1 like
  • 3 in conversation