<?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: How to calculate the sum of a conditional column values by use of PROC IML? in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/273925#M2799</link>
    <description>&lt;P&gt;One possibility might be to&amp;nbsp;construct&amp;nbsp;a matrix of zeros and ones (b) that gets the necessary sums by matrix multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a = {1 1 1, 0 1 1, 0 0 1};
b = block(a,a,a,a);
s = b * TRY[ ,3];
print b [format=1.0] s;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here the BLOCK function places&amp;nbsp;4 copies of the 3x3 matrix 'a' on the diagonal of a 12x12 matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 30 May 2016 14:55:34 GMT</pubDate>
    <dc:creator>IanWakeling</dc:creator>
    <dc:date>2016-05-30T14:55:34Z</dc:date>
    <item>
      <title>How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/273895#M2798</link>
      <description>&lt;P&gt;Hi, All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose I simulate a dataset via do loop and get a&amp;nbsp;matrix with below code.&lt;/P&gt;
&lt;P&gt;My question is: How could I get the sum of x, by&amp;nbsp; i and j values?&lt;BR /&gt;Exactly speaking, I want to this:&lt;/P&gt;
&lt;P&gt;denote sum as&amp;nbsp; a coloumn vector with dimention is 12*1, with sum[1]=sum(x1:x3) given i=j=1; sum[2]=sum(x2:x3) given i=1 and j=2;&lt;/P&gt;
&lt;P&gt;.....sum[10]=sum(x10:x12) given i=4 and j=1; sum[11]=sum(x11:x12) given i=4 and j=2; sum[12]=sum(x12) given i=4 and j=3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How should I program via PROC IML to reach such aim? Much appreciated for your help to a fresh to IML procedure.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
	do i=1 to 4;
		do j=1 to 3;
		call streaminit(i*j);
		x=rand("normal");
		output;
	end;
end;
run;

PROC IML;
USE test var {i j x};
read all var {i j x} into TRY;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 May 2016 12:08:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/273895#M2798</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-05-30T12:08:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/273925#M2799</link>
      <description>&lt;P&gt;One possibility might be to&amp;nbsp;construct&amp;nbsp;a matrix of zeros and ones (b) that gets the necessary sums by matrix multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a = {1 1 1, 0 1 1, 0 0 1};
b = block(a,a,a,a);
s = b * TRY[ ,3];
print b [format=1.0] s;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here the BLOCK function places&amp;nbsp;4 copies of the 3x3 matrix 'a' on the diagonal of a 12x12 matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2016 14:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/273925#M2799</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2016-05-30T14:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274001#M2800</link>
      <description>Dear Ian,

Very much appreciated for your creative idea. Yes, you are correct, this does work. However, if for a TRY with higher number of rows, like ten thousand, then b would be very huge, not sure there is any better solution to this. 

Many thanks,
Jack</description>
      <pubDate>Tue, 31 May 2016 02:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274001#M2800</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-05-31T02:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274005#M2801</link>
      <description>&lt;P&gt;Your logic is not clear for me .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not use Data Step ? since it is not about VECTOR operation .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	do i=1 to 4;
		do j=1 to 3;
		call streaminit(i*j);
		x=rand("normal");
		output;
	end;
end;
run;
PROC IML;
use test nobs nobs;
read all var {i j x} into TRY;
close;

start=1:nobs;
end=start+repeat({2 1 0},1,int(nobs/3));

sum=j(nobs,1,.);
do i=1 to nobs;
 sum[i]=sum(try[start[i]:end[i],3]);
end;

print try sum;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 May 2016 02:27:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274005#M2801</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-31T02:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274013#M2802</link>
      <description>&lt;P&gt;Dear Ian,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just checked the function of BLOCK in SAS web and found for the higher order of TRY, this method is not workable. I think I should go back to consider to the DATA step.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/viewer.htm#langref_sect32.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/imlug/59656/HTML/default/viewer.htm#langref_sect32.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Many thanks still for your creative idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jack&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 02:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274013#M2802</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-05-31T02:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274014#M2803</link>
      <description>&lt;P&gt;Dear KSharp,&lt;/P&gt;
&lt;P&gt;I am really appreciated for your reply at very late night for you. Sorry for unclear logic of my question, but really I have to day, you really produced the right result I am hunting for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I fully agree with you maybe should use data step, as I am not working on the vector but block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jack&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 02:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274014#M2803</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-05-31T02:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274017#M2804</link>
      <description>&lt;P&gt;It seems I can't set two solutions, but I have to admit I prefer yours as it is easy to expand for simulation. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 03:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274017#M2804</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-05-31T03:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274019#M2805</link>
      <description>Never mind. I really don't care about it.</description>
      <pubDate>Tue, 31 May 2016 03:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274019#M2805</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-31T03:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274039#M2806</link>
      <description>&lt;P&gt;Hi Jack,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did not realise that you wanted to scale it to&amp;nbsp;thousands of observations, so the 15 matrix limit on the BLOCK function is clearly a problem, and anyway you would not want to create and multiply zero/one matrices that large.&amp;nbsp;&amp;nbsp;&amp;nbsp;It is possible to vectorize the problem within IML by&amp;nbsp;realising that what you want is not far away from the cumulative sum of the data vector in reverse.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;n = 12;
m = 3;
c = cusum(TRY[n:1,3])[n:1];
s = c - (c[do(m+1, n-m+1, m)]//0) @ j(m, 1);
print try c s;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where n is the total number of obsertations and m the sub-group size.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ian.&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2016 07:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274039#M2806</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2016-05-31T07:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the sum of a conditional column values by use of PROC IML?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274268#M2812</link>
      <description>&lt;P&gt;Dear Ian,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Millionn thanks to your further code, yes, I think your logic is quite the same as Keshan's. Realy thank you both.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jack.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 03:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-calculate-the-sum-of-a-conditional-column-values-by-use/m-p/274268#M2812</guid>
      <dc:creator>Jack2012</dc:creator>
      <dc:date>2016-06-01T03:03:27Z</dc:date>
    </item>
  </channel>
</rss>

