<?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 get sub-sums in a matrix, not dataset? in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134838#M1094</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe you mean this :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;proc iml;
use sashelp.class;
summary class{age} var{weight} stat{mean std var} opt{noprint save};
x=age||weight;
create want from x[c={'age' 'weight_mean' 'weight_std' 'weight_var'}] ;
append from x;
close;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 24 Jan 2015 10:28:34 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2015-01-24T10:28:34Z</dc:date>
    <item>
      <title>How to get sub-sums in a matrix, not dataset?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134835#M1091</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi~~&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm a stat-centric risk manager working on lots of financial models. Gladly IML community caught my eyes because I've got many questions with respect to IML usages.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm dealing with a very big matrix whose size is 1,000,000-by-20 (row-by-column). For ease of explanation, the matrix name is XXX. 1st column is number of defaults, 2nd column is grade(1,2,3~,20), 3rd column is region number(100, 200, 300, 400), 4th column is corporate size(1: large company, 2:Small and Medium, 3: Small etc)...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The matrix format in display is like this;&lt;/P&gt;&lt;P&gt;8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 300&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/P&gt;&lt;P&gt;15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 200&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'd like to get sums of defaults by each of grade, region number, corporate size, which is usually done by proc means, proc summary procedures with "data sets". For example, &lt;/P&gt;&lt;P&gt;proc summary data=XXX;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var defaults&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class grade region_number corporate_size;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output out=getsum sum=;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then I get many sub-sums for various combination of class variables. I found an iml equivalent "summary" statement which functions almost same as proc summary. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But the problem is that in order to use "summary" statement, I need to make the matrix XXX a data set and then I can use the "summary" statement.&lt;/P&gt;&lt;P&gt;My question is, Is there any other way that without making the matrix XXX a data set, I can get sub-sums of first column by each combination of 2, 3, 4 column values. The point is that huge number of temporary big matrices are generated and sub-sumbs are extactred and big matrices are discarded. In the meantime, big time loss occurs during making data sets. I could tolerate if the number of those operations small, but the number could be up to tens of thousand, which is considered to be no solution at all.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've been trying to find possible solutions for it, searching through internet and in vain....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you in advance~&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 06:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134835#M1091</guid>
      <dc:creator>StatOnGlobe</dc:creator>
      <dc:date>2015-01-23T06:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to get sub-sums in a matrix, not dataset?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134836#M1092</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are asking for BY-group processing. There are several ways to do this. For a large number of rows I recommend the UNIQUEBY technique, which is described in this blog post: &lt;A href="http://blogs.sas.com/content/iml/2011/11/07/an-efficient-alternative-to-the-unique-loc-technique/" title="http://blogs.sas.com/content/iml/2011/11/07/an-efficient-alternative-to-the-unique-loc-technique/"&gt; An efficient alternative to the UNIQUE-LOC technique - The DO Loop&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For 1million obs, this approach takes a few seconds. The blog article explains the details, so I'll just give the solution here:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* create sample data for testing */&lt;BR /&gt;data Have;&lt;BR /&gt;call streaminit(123);&lt;BR /&gt;do i = 1 to 1e6;&amp;nbsp; /* use 50 for debugging */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Defaults = rand("Poisson", 5);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Grade = ceil(5*rand("Uniform"));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Region = 100 * ceil(4*rand("Uniform"));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Size = ceil(2*rand("Uniform"));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Use PROC MEANS to compute the correct answer */&lt;BR /&gt;proc means data=Have sum;&lt;BR /&gt;class Grade Region Size;&lt;BR /&gt;var Defaults;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* obtain the same answer in PROC IML by using the UNIQUEBY technique */&lt;BR /&gt;proc iml;&lt;BR /&gt;varName = {"Defaults" "Grade" "Region" "Size"};&lt;BR /&gt;use Have;&lt;BR /&gt;read all var varName into XXX;&lt;BR /&gt;close Have;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;call sort(XXX, 2:4);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 1. Sort or use PROC SORT in Base SAS */&lt;BR /&gt;by = uniqueby(XXX, 2:4);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 2. Rows for 1st obs in each level. */&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sum = j(nrow(by),1);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 3. Allocate vector to hold results */&lt;BR /&gt;b = by // (nrow(XXX)+1);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Trick: append (n+1) to end of by */&lt;BR /&gt;do i = 1 to nrow(b)-1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 4. For each level... */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; idx = b&lt;I&gt;:(b[i+1]-1);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 5. Find observations in level */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; sum&lt;I&gt; = sum(XXX[idx, 1]); /* 6. Compute statistic on those values */&lt;BR /&gt;end;&lt;BR /&gt;*lbl = rowcat( char(XXX[by,2:4]) ); /* Opitonal: create labels for BY groups */&lt;BR /&gt;*print sum[rowname=lbl];&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 14:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134836#M1092</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-01-23T14:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to get sub-sums in a matrix, not dataset?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134837#M1093</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If sounds like you need NWAY option.&amp;nbsp; I think you are saying you want summary of each level of &lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;grade*region_number*corporate_size&amp;nbsp; but not the extra "ways" that PROC SUMMARY provides by default.&amp;nbsp; That can be done by specifying NWAY on the PROC SUMMARY statement.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jan 2015 14:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134837#M1093</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2015-01-23T14:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to get sub-sums in a matrix, not dataset?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134838#M1094</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe you mean this :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;proc iml;
use sashelp.class;
summary class{age} var{weight} stat{mean std var} opt{noprint save};
x=age||weight;
create want from x[c={'age' 'weight_mean' 'weight_std' 'weight_var'}] ;
append from x;
close;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 24 Jan 2015 10:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-to-get-sub-sums-in-a-matrix-not-dataset/m-p/134838#M1094</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-01-24T10:28:34Z</dc:date>
    </item>
  </channel>
</rss>

