<?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: double looping and multiplying between observations / matrix multiplication in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711008#M26847</link>
    <description>Will these approaches still work if i have 4 million observations instead of 100?&lt;BR /&gt;how long should it take to calculate for 4 million observations?</description>
    <pubDate>Wed, 13 Jan 2021 07:57:47 GMT</pubDate>
    <dc:creator>joshmazin</dc:creator>
    <dc:date>2021-01-13T07:57:47Z</dc:date>
    <item>
      <title>double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710798#M26821</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am familiar with SQL and VBA, and now trying to learn how to use SAS. I would appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that is two columns: column ID (set to 1 or 2) and column Value.&lt;/P&gt;&lt;P&gt;Let's say I have 100 observations.&lt;/P&gt;&lt;P&gt;Let's call this data set as array(x,y) where x is rows so goes from 1 to 100, and y is columns so goes from 1 to 2. array(x,1) is the ID column, and array(x,2) is the Value column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also need a correlation matrix between each of the 100 observations, say corr(i,j).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to produce the sum of the values of the 100 observations allowing for the correlations between them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can achieve this by doing matrix multiplication of array(x,2) with itself transposed and with corr(i,j), square-rooted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I was working in VBA I may do something like:&lt;/P&gt;&lt;P&gt;Total1 = 0&lt;/P&gt;&lt;P&gt;Total2 = 0&lt;/P&gt;&lt;P&gt;For a = 1 to 100&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;For b = 1 to 100&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; result = array(a,2) * array(b,2) * corr(a,b)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Total1 = Total1 + result&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Next b&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Total2 = Total2 + Total1&lt;/P&gt;&lt;P&gt;Next a&lt;/P&gt;&lt;P&gt;Total = Total2^(1/2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;corr(i,j) needs to be set..&lt;/P&gt;&lt;P&gt;- to 1 where i=j,&lt;/P&gt;&lt;P&gt;- to 0.5 where i &amp;lt;&amp;gt; j and array(i,1) &amp;lt;&amp;gt; array(j,1) i.e. different IDs,&lt;/P&gt;&lt;P&gt;- to 0 otherwise&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do this in SAS Enterprise? Is using a Do query the best approach or is there a direct matrix multiplication approach?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&lt;/P&gt;&lt;P&gt;Josh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 13:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710798#M26821</guid>
      <dc:creator>joshmazin</dc:creator>
      <dc:date>2021-01-12T13:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710826#M26826</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363629"&gt;@joshmazin&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering if Total1 in your VBA code doesn't need to be reset to 0 after each iteration of the outer loop. Assuming that this should be the case, the computation could be coded in SAS like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
do _n_=1 to 100;
  id=rand('table',0.5);
  value=rand('integer',1000);
  output;
end;
run;

/* Determine number of observations in HAVE */

data _null_;
call symputx('d',n);
stop;
set have nobs=n;
run;

/* Perform the computation */

data want(keep=total);
array v[&amp;amp;d,2] _temporary_;
set have end=last;
v[_n_,1]=id;
v[_n_,2]=value;
if last;
do i=1 to &amp;amp;d;
  do j=1 to &amp;amp;d;
    if i=j then total+v[i,2]*v[j,2];
    else if v[i,1] ne v[j,1] then total+v[i,2]*v[j,2]/2;
  end;
end;
total=sqrt(total);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last DATA step writes the IDs and values to a temporary array &lt;FONT face="courier new,courier"&gt;v&lt;/FONT&gt;, then computes the sum of all products of the form "value1*value2*corr" and finally writes the square root of that total to dataset WANT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 15:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710826#M26826</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-01-12T15:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710833#M26829</link>
      <description>&lt;P&gt;If you're looking for a good reference I highly recommend this one:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.amazon.ca/Simulating-Data-SAS-Rick-Wicklin/dp/1612903320" target="_blank"&gt;https://www.amazon.ca/Simulating-Data-SAS-Rick-Wicklin/dp/1612903320&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;His blog posts will also have a lot of good posts on simulating data in SAS. Typically, if you're doing that type of work you're usually using IML, not Base SAS.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/author/rickwicklin/" target="_blank"&gt;https://blogs.sas.com/content/author/rickwicklin/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363629"&gt;@joshmazin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am familiar with SQL and VBA, and now trying to learn how to use SAS. I would appreciate your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set that is two columns: column ID (set to 1 or 2) and column Value.&lt;/P&gt;
&lt;P&gt;Let's say I have 100 observations.&lt;/P&gt;
&lt;P&gt;Let's call this data set as array(x,y) where x is rows so goes from 1 to 100, and y is columns so goes from 1 to 2. array(x,1) is the ID column, and array(x,2) is the Value column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also need a correlation matrix between each of the 100 observations, say corr(i,j).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to produce the sum of the values of the 100 observations allowing for the correlations between them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can achieve this by doing matrix multiplication of array(x,2) with itself transposed and with corr(i,j), square-rooted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I was working in VBA I may do something like:&lt;/P&gt;
&lt;P&gt;Total1 = 0&lt;/P&gt;
&lt;P&gt;Total2 = 0&lt;/P&gt;
&lt;P&gt;For a = 1 to 100&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;For b = 1 to 100&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; result = array(a,2) * array(b,2) * corr(a,b)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Total1 = Total1 + result&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Next b&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Total2 = Total2 + Total1&lt;/P&gt;
&lt;P&gt;Next a&lt;/P&gt;
&lt;P&gt;Total = Total2^(1/2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;corr(i,j) needs to be set..&lt;/P&gt;
&lt;P&gt;- to 1 where i=j,&lt;/P&gt;
&lt;P&gt;- to 0.5 where i &amp;lt;&amp;gt; j and array(i,1) &amp;lt;&amp;gt; array(j,1) i.e. different IDs,&lt;/P&gt;
&lt;P&gt;- to 0 otherwise&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I do this in SAS Enterprise? Is using a Do query the best approach or is there a direct matrix multiplication approach?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks in advance&lt;/P&gt;
&lt;P&gt;Josh&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 15:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710833#M26829</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-12T15:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710869#M26831</link>
      <description>&lt;P&gt;Or if you prefer an equivalent calculation in SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    sqrt(uss(a.value)/sqrt(count(a.value)) +
    sum(a.value*b.value*(a.id &amp;lt; b.id)))
from have as a, have as b;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: USS(x) is the same as SUM(x*x).&lt;/P&gt;
&lt;P&gt;Note: In SAS, logical expressions evaluate to 1 when true and 0 otherwise.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 17:37:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710869#M26831</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-01-12T17:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710884#M26832</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is ingenious! Somehow I didn't grasp the meaning of those "correlations" (too tired I guess) and thus overlooked the possible simplification. Likewise, the idea of a cross join escaped me. Kudos!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 18:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710884#M26832</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-01-12T18:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710942#M26836</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; . It helped that I could validate my results against yours.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 23:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710942#M26836</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-01-12T23:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710999#M26844</link>
      <description>&lt;P&gt;Thank you very much. I will try to apply this to my full code and will update you&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 06:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/710999#M26844</guid>
      <dc:creator>joshmazin</dc:creator>
      <dc:date>2021-01-13T06:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711000#M26845</link>
      <description>Thank you very much</description>
      <pubDate>Wed, 13 Jan 2021 06:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711000#M26845</guid>
      <dc:creator>joshmazin</dc:creator>
      <dc:date>2021-01-13T06:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711002#M26846</link>
      <description>Thank you very much. I will try to apply this to my full code.&lt;BR /&gt;Could you explain what you are doing at each stage of your calc as I am not familiar with some of these functions?&lt;BR /&gt;ALso I dont get the same result using your approach as the previous poster's post, despite your comment that you checked yours against theirs?&lt;BR /&gt;Many thanks</description>
      <pubDate>Wed, 13 Jan 2021 06:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711002#M26846</guid>
      <dc:creator>joshmazin</dc:creator>
      <dc:date>2021-01-13T06:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711008#M26847</link>
      <description>Will these approaches still work if i have 4 million observations instead of 100?&lt;BR /&gt;how long should it take to calculate for 4 million observations?</description>
      <pubDate>Wed, 13 Jan 2021 07:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711008#M26847</guid>
      <dc:creator>joshmazin</dc:creator>
      <dc:date>2021-01-13T07:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: double looping and multiplying between observations / matrix multiplication</title>
      <link>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711015#M26851</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363629"&gt;@joshmazin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Will these approaches still work if i have 4 million observations instead of 100?&lt;BR /&gt;how long should it take to calculate for 4 million observations?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;At the very least you should then take advantage of symmetry. In terms of my solution this could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=total);
array v[&amp;amp;d,2] _temporary_;
set have end=last;
v[_n_,1]=id;
v[_n_,2]=value;
if last;
do i=1 to &amp;amp;d;
  total+v[i,2]**2;
  do j=i+1 to &amp;amp;d;
    if v[i,1] ne v[j,1] then total+v[i,2]*v[j,2];
  end;
end;
total=sqrt(total);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it's well possible that you'll need to further improve efficiency, perhaps by switching to SAS/IML (which I don't have) as suggested by Reeza. In any case I would increase the number &lt;EM&gt;n&lt;/EM&gt; of observations gradually, observe the run time in each step and try to extrapolate (based on the assumption that it will be &lt;EM&gt;O&lt;/EM&gt;(&lt;EM&gt;n&lt;/EM&gt;²)).&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 10:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/double-looping-and-multiplying-between-observations-matrix/m-p/711015#M26851</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-01-13T10:29:14Z</dc:date>
    </item>
  </channel>
</rss>

