<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Once again, row slope revisited in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290927#M60260</link>
    <description>&lt;P&gt;I see no problem with your code.&lt;/P&gt;
&lt;P&gt;I tried it, and it works. The 2 types of slope calculations give the same results.&lt;/P&gt;
&lt;P&gt;You probably have missing values in the&amp;nbsp;&lt;SPAN&gt;_505xx variables. That can cause differences.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; input _50501 _50502 _50503;&lt;BR /&gt;datalines;&lt;BR /&gt;3 4 5&lt;BR /&gt;7 7 7&lt;BR /&gt;3 2 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array ys(3) _50501 _50502 _50503;&lt;BR /&gt;array vals(3) _temporary_ (1 2 3);&lt;BR /&gt;xbar = mean(of vals(*));&lt;BR /&gt;ybar = mean(of ys(*));&lt;BR /&gt;do i=1 to dim(vals);&lt;BR /&gt;s_xy=sum(s_xy, i*ys(i));&lt;BR /&gt;s_y=sum(s_y, ys(i));&lt;BR /&gt;s_x2=sum(s_x2, vals(i)**2);&lt;BR /&gt;num=(vals(i)-xbar)*(ys(i)-ybar);&lt;BR /&gt;den=(vals(i)-xbar)**2;&lt;BR /&gt;num_tot=sum(num, num_tot);&lt;BR /&gt;den_tot=sum(den, den_tot);&lt;BR /&gt;end;&lt;BR /&gt;s_x=sum(of vals(*));&lt;BR /&gt;n1=dim(vals);&lt;BR /&gt;slope2 = (n1*s_xy - s_x*s_y)/(n1*s_x2 - s_x**2);&lt;BR /&gt;Slope_5050x_3 = num_tot/den_tot;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Aug 2016 09:24:21 GMT</pubDate>
    <dc:creator>gergely_batho</dc:creator>
    <dc:date>2016-08-11T09:24:21Z</dc:date>
    <item>
      <title>Once again, row slope revisited</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290894#M60249</link>
      <description>&lt;P&gt;Here are a few rows of data (actual dataset has over a million rows):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id c1 c2 c3&lt;BR /&gt;a 0.87396 2.00827 2.81477&lt;BR /&gt;b 0.97002 2.00064 2.81468&lt;BR /&gt;c 0.68026 1.86006 2.81403&lt;BR /&gt;d 0.58230 1.84271 2.81402&lt;BR /&gt;e 0.73355 1.59606 2.81368&lt;BR /&gt;f 1.20452 2.07169 2.81365&lt;BR /&gt;g 0.91387 1.19560 2.81352&lt;BR /&gt;h 1.19418 2.10696 2.81306&lt;BR /&gt;i -0.10435 1.34213 2.81296&lt;BR /&gt;j -0.06286 1.50670 2.81225&lt;BR /&gt;k 1.73225 2.37420 2.81185&lt;BR /&gt;l 0.53130 1.87948 2.81164&lt;BR /&gt;m 0.86563 1.65322 2.81151&lt;BR /&gt;n 1.05598 2.40835 2.81065&lt;BR /&gt;o 0.15833 1.55971 2.81035&lt;BR /&gt;p 0.00912 0.61380 2.81033&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The objective is to compute the slope for each row, and place that slope computation into column 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assume the X values are 1, 2, 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Y values are those given in each row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below is what I have been trying, but something seems incorrect, as the resulting slope computations don't appear correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please take a look at the code and tell me if you see any problem with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data nicholas.n_slope_means__7;
set nicholas.n_slope_means__7;
array ys(3) _50501 _50502 _50503;
array vals(3) (1 2 3);
xbar = mean(of vals(*));
ybar = mean(of ys(*));
do i=1 to dim(vals);
s_xy=sum(s_xy, i*ys(i));
s_y=sum(s_y, ys(i));
s_x2=sum(s_x2, vals(i)**2);
num=(vals(i)-xbar)*(ys(i)-ybar);
den=(vals(i)-xbar)**2;
num_tot=sum(num, num_tot);
den_tot=sum(den, den_tot);
end;
s_x=sum(of vals(*));
n1=dim(vals);
slope2 = (n1*s_xy - s_x*s_y)/(n1*s_x2 - s_x**2);
Slope_5050x_3 = num_tot/den_tot;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 07:06:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290894#M60249</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2016-08-11T07:06:23Z</dc:date>
    </item>
    <item>
      <title>Re: Once again, row slope revisited</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290909#M60251</link>
      <description>&lt;PRE&gt;
OK. You have three variables not two , So can you explain how to get row slope ?
What is your logic to get that slope (you mean PROC REG  to get that parameter(slope)?).


data nicholas.n_slope_means__7;
set nicholas.n_slope_means__7;
array ys(3) _50501 _50502 _50503;
array vals(3) (1 2 3);
xbar = mean(of vals(*));
ybar = mean(of ys(*));
do i=1 to dim(vals);
s_xy=sum(s_xy, i*ys(i));
s_y=sum(s_y, ys(i));
s_x2=sum(s_x2, vals(i)**2);
num=(vals(i)-xbar)*(ys(i)-ybar);
den=(vals(i)-xbar)**2;
num_tot=sum(num, num_tot);
den_tot=sum(den, den_tot);
end;
s_x=sum(of vals(*));
n1=dim(vals);
slope2 = (n1*s_xy - s_x*s_y)/(n1*s_x2 - s_x**2);
Slope_5050x_3 = num_tot/den_tot;
run;
 


&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Aug 2016 08:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290909#M60251</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-11T08:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: Once again, row slope revisited</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290917#M60255</link>
      <description>&lt;PRE&gt;
OK. Assuming you want that Beta parameter of PROC REG. 
The following is two kind of code , one is data step, another is IML. notice there is some difference.


data have;
input id $ c1 c2 c3;
cards;
a 0.87396 2.00827 2.81477
b 0.97002 2.00064 2.81468
c 0.68026 1.86006 2.81403
d 0.58230 1.84271 2.81402
e 0.73355 1.59606 2.81368
f 1.20452 2.07169 2.81365
g 0.91387 1.19560 2.81352
h 1.19418 2.10696 2.81306
i -0.10435 1.34213 2.81296
j -0.06286 1.50670 2.81225
k 1.73225 2.37420 2.81185
l 0.53130 1.87948 2.81164
m 0.86563 1.65322 2.81151
n 1.05598 2.40835 2.81065
o 0.15833 1.55971 2.81035
p 0.00912 0.61380 2.81033
;
run;
proc transpose data=have out=temp;
by id;
var c:;
run;
data temp;
 set temp;
 by id;
 if first.id then x=0;
 x+1;
 drop _name_;
run;
proc reg data=temp outest=wantt noprint;
 by id;
 model col1=x ; 
/* model col1=x /noint;  &amp;lt;-- Which  is the same as IML code*/
quit;

 








data have;
input id $ c1 c2 c3;
cards;
a 0.87396 2.00827 2.81477
b 0.97002 2.00064 2.81468
c 0.68026 1.86006 2.81403
d 0.58230 1.84271 2.81402
e 0.73355 1.59606 2.81368
f 1.20452 2.07169 2.81365
g 0.91387 1.19560 2.81352
h 1.19418 2.10696 2.81306
i -0.10435 1.34213 2.81296
j -0.06286 1.50670 2.81225
k 1.73225 2.37420 2.81185
l 0.53130 1.87948 2.81164
m 0.86563 1.65322 2.81151
n 1.05598 2.40835 2.81065
o 0.15833 1.55971 2.81035
p 0.00912 0.61380 2.81033
;
run;
proc iml;
use have;
read all var _num_ into y[r=id c=vnames];
close;
x={1 ,2 ,3};
beta=j(nrow(y),1,.);
do i=1 to nrow(y);
 beta[i]=solve(x`*x,x`*y[i,]`);
end;
want=y||beta;
create want from want[r=id c=(vnames||'Beta')];
append from want[r=id];
close;
quit;

 



&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Aug 2016 08:43:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290917#M60255</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-11T08:43:28Z</dc:date>
    </item>
    <item>
      <title>Re: Once again, row slope revisited</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290927#M60260</link>
      <description>&lt;P&gt;I see no problem with your code.&lt;/P&gt;
&lt;P&gt;I tried it, and it works. The 2 types of slope calculations give the same results.&lt;/P&gt;
&lt;P&gt;You probably have missing values in the&amp;nbsp;&lt;SPAN&gt;_505xx variables. That can cause differences.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; input _50501 _50502 _50503;&lt;BR /&gt;datalines;&lt;BR /&gt;3 4 5&lt;BR /&gt;7 7 7&lt;BR /&gt;3 2 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array ys(3) _50501 _50502 _50503;&lt;BR /&gt;array vals(3) _temporary_ (1 2 3);&lt;BR /&gt;xbar = mean(of vals(*));&lt;BR /&gt;ybar = mean(of ys(*));&lt;BR /&gt;do i=1 to dim(vals);&lt;BR /&gt;s_xy=sum(s_xy, i*ys(i));&lt;BR /&gt;s_y=sum(s_y, ys(i));&lt;BR /&gt;s_x2=sum(s_x2, vals(i)**2);&lt;BR /&gt;num=(vals(i)-xbar)*(ys(i)-ybar);&lt;BR /&gt;den=(vals(i)-xbar)**2;&lt;BR /&gt;num_tot=sum(num, num_tot);&lt;BR /&gt;den_tot=sum(den, den_tot);&lt;BR /&gt;end;&lt;BR /&gt;s_x=sum(of vals(*));&lt;BR /&gt;n1=dim(vals);&lt;BR /&gt;slope2 = (n1*s_xy - s_x*s_y)/(n1*s_x2 - s_x**2);&lt;BR /&gt;Slope_5050x_3 = num_tot/den_tot;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 09:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/290927#M60260</guid>
      <dc:creator>gergely_batho</dc:creator>
      <dc:date>2016-08-11T09:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Once again, row slope revisited</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/291567#M60410</link>
      <description>&lt;P&gt;Been having some computer problems. &amp;nbsp;Thanks very much for your continued assistance with this slope matter. &amp;nbsp;I'll try and use your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2016 10:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Once-again-row-slope-revisited/m-p/291567#M60410</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2016-08-14T10:21:56Z</dc:date>
    </item>
  </channel>
</rss>

