<?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: Cumulative sum by byvar in the order of rank variable in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776053#M5713</link>
    <description>&lt;P&gt;Nice try, Ksharp. Another solution given in addition to your SQL solution.&lt;/P&gt;</description>
    <pubDate>Sat, 23 Oct 2021 19:28:45 GMT</pubDate>
    <dc:creator>Macro</dc:creator>
    <dc:date>2021-10-23T19:28:45Z</dc:date>
    <item>
      <title>Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775554#M5706</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create a new dataset from an old dataset, so that the new dataset will have cumulative sum of variables from the old dataset. This should be done by byvar variables in the old dataset.&amp;nbsp; An old data example is like this temp dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data temp;
input Factor1 $  Factor2 $  rank x y;
datalines;
A  B  1  1  2
A  B  2  2  4
A  B  3  3  6
A  C  1  1  7
A  C  2  3  8
A  C  3  5  9
;
run
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The new dataset should be like this temp2 dataset:&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data temp2;
input Factor1 $  Factor2 $  rank x y csum_x  csum_y;
datalines;
A  B  1  1  2  1  2
A  B  2  2  4  3  6
A  B  3  3  6  6  12
A  C  1  1  7  1  7
A  C  2  3  8  4  15
A  C  3  5  9  9  24
;
run&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The cumulative sum of x and y are done by Factor1 and Factor2 combination, and along with the rank variable rank. The general factor variable combination could be&amp;nbsp; more than 2 factor variables. The old dataset temp could potentially has other variables not listed here. The final result should be a SAS dataset like temp2. Is there an IML way to do this? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 04:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775554#M5706</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-21T04:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775566#M5707</link>
      <description>&lt;P&gt;Why do you want to do this in IML? Seems the right tool is the data step?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 06:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775566#M5707</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-21T06:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775582#M5708</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;suggested, use data step, assuming TEMP is sorted by &lt;CODE class=" language-sas"&gt;Factor1, Factor2,&lt;/CODE&gt;&amp;nbsp;and&amp;nbsp;&lt;CODE class=" language-sas"&gt;rank&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set temp;
 by Factor1 Factor2;
  if first.Factor2 then call missing(cum_x, cum_y);
  cum_x + x;
  cum_y + y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 07:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775582#M5708</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-10-21T07:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775596#M5709</link>
      <description>&lt;P&gt;This appears to be the same question as&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775553" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775553&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;but the OP is requesting to do it in IML. Those who have non-IML solutions can post them to the other thread.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sure, you can do this in IML if you want. I notice you've posted several similar questions to the IML community, so I assume you are trying to learn IML.&amp;nbsp;I'll outline the general idea and give you a chance to experiment with the ideas and improve your IML programming skills.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Read the data into IML matrices. Read the factors into a BY-group matrix, &lt;STRONG&gt;G&lt;/STRONG&gt;,&amp;nbsp; and the variables into a data matrix, &lt;STRONG&gt;X&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;2. Assuming that the input data are already sorted by the factors, &lt;A href="https://blogs.sas.com/content/iml/2011/11/07/an-efficient-alternative-to-the-unique-loc-technique.html" target="_self"&gt;use the UNIQUEBY function to find the rows&lt;/A&gt; of X that represent the first row for each BY&amp;nbsp; group. As shown in the article, you can append nrow(G) to the vector to make subsequent processing easier.&lt;/P&gt;
&lt;P&gt;3. Loop over the BY-group rows. Call the CUSUM function for each variable in X.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. At the end of the loop over the BY-groups, you will have a matrix (call it &lt;STRONG&gt;C&lt;/STRONG&gt;) that contains the cumulative sums of each variable for each BY-group. If you need that in a SAS data set, create a data set from &lt;STRONG&gt;C&lt;/STRONG&gt; and merge it with the original data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try to program it yourself. If you get stuck, post what you have and others will help.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 10:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775596#M5709</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-10-21T10:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775613#M5710</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input Factor1 $  Factor2 $  rank x y;
datalines;
A  B  1  1  2
A  B  2  2  4
A  B  3  3  6
A  C  1  1  7
A  C  2  3  8
A  C  3  5  9
A  D  1  1  7
A  D  2  3  8
A  D  3  5  9
;
run;
proc iml;
use temp nobs nobs;
read all var{Factor1  Factor2} into groups;
read all var{Factor1  Factor2 rank x y};
close;
start_idx=uniqueby(groups,(1:2));
end_idx=remove(start_idx,1)-1||nobs;
do i=1 to nrow(start_idx);
 csum_x=csum_x//cusum(x[start_idx[i]:end_idx[i]]);
 csum_y=csum_y//cusum(y[start_idx[i]:end_idx[i]]);
end;

create want var{Factor1  Factor2 rank x y csum_x csum_y};
append;
close;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 11:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775613#M5710</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-21T11:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775629#M5711</link>
      <description>&lt;P&gt;I was hoping to give the OP a chance to think about this problem before providing the solution. But since KSharp wrote the program according to my outline, here is my version:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
use temp;
read all var {Factor1 Factor2} into G;  /* BY groups */
read all var {x y} into X[c=varNames];  /* the data for each BY group */
close;

/* if the input data is already sorted by Factors, you can find the 
   rows that separate the BY groups */
byRows = uniqueby(G, 1:ncol(G));
byRows = byRows // nrow(G);    /* append the last rows for easy processing */ 

/* loop over the BY groups and compute the cumulative sums */
S = j(nrow(G), ncol(X), .);   /* store the cumulative sums here */
do i = 1 to nrow(byRows)-1;
   rowStart = byRows[i]; rowEnd = byRows[i+1]; 
   do j = 1 to ncol(X);
      S[rowStart:rowEnd, j] = cusum(X[rowStart:rowEnd, j]);
   end;
end;

print G X[c=varNames] S[c=('c'+varNames)];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 12:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/775629#M5711</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2021-10-21T12:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776053#M5713</link>
      <description>&lt;P&gt;Nice try, Ksharp. Another solution given in addition to your SQL solution.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 19:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776053#M5713</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-23T19:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776054#M5714</link>
      <description>&lt;P&gt;Thanks Rick. This is a situation that proc means cannot handle directly and other means may be necessary. In particular, this is a cusum, an output of a column not a scalar summary statistic. Though the datastep using missing routine and cusum statement provides the simpler solution, the IML way provides an alternative solution and an opportunity of practice. While Ksharp uses the exact index, start and end (by the way, Ksharp also has a nice sql solution), you overwrite the last element of cusum result for each level of the factor combination (this initially puzzled me a little bit). In addition, pre-allocation of memory and assignment of components of a matrix, instead of repeated concatenation, make your code more efficient. Well done. I learned a lot from your blog,&amp;nbsp; and of course, your book as well.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 19:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776054#M5714</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-23T19:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum by byvar in the order of rank variable</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776070#M5715</link>
      <description>"you overwrite the last element of cusum result for each level of the factor combination (this initially puzzled me a little bit). "&lt;BR /&gt;&lt;BR /&gt;Me too. But Rick get right result .</description>
      <pubDate>Sun, 24 Oct 2021 09:53:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Cumulative-sum-by-byvar-in-the-order-of-rank-variable/m-p/776070#M5715</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-24T09:53:41Z</dc:date>
    </item>
  </channel>
</rss>

