<?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 create special matrix with proc iml in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22467#M96</link>
    <description>Rick-&lt;BR /&gt;
&lt;BR /&gt;
thanks for the code. it works great. this is exactly what i wanted. just plug in n and get the matrix&lt;BR /&gt;
&lt;BR /&gt;
cheers

Message was edited by: trekvana</description>
    <pubDate>Tue, 26 Oct 2010 16:39:36 GMT</pubDate>
    <dc:creator>trekvana</dc:creator>
    <dc:date>2010-10-26T16:39:36Z</dc:date>
    <item>
      <title>how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22463#M92</link>
      <description>hello all -&lt;BR /&gt;
&lt;BR /&gt;
i would like to create matrix that looks like this given the number of columns n. suppose n=4 for now&lt;BR /&gt;
&lt;BR /&gt;
1 1 1 1 1 1&lt;BR /&gt;
1 2 1 1 1 1&lt;BR /&gt;
1 3 1 1 1 1 &lt;BR /&gt;
1 4 1 1 1 1&lt;BR /&gt;
2 2 0 1 1 1&lt;BR /&gt;
2 3 0 1 1 1&lt;BR /&gt;
2 4 0 1 1 1&lt;BR /&gt;
3 3 0 0 1 1 &lt;BR /&gt;
3 4 0 0 1 1&lt;BR /&gt;
4 4 0 0 0 1&lt;BR /&gt;
&lt;BR /&gt;
so pretty the big matrix is always going to contain n+2 columns. the first 2 columns correspond  to ordered combinations of the 1 to n and the respective row repeat n times then n-1 times then n-2. i know this is very confusing. i might be explaining it wrong too. here is a better way to say it with the example.&lt;BR /&gt;
&lt;BR /&gt;
so the first two rows consist of all the different combinations (1 1) (1 2) (1 3) (1 4) (2 2) (2 3) (2 4) (3 3) etc. &lt;BR /&gt;
&lt;BR /&gt;
now there are 4 pairs that have 1 first, 3 pairs that have 2 first, 2 pairs with 3 first and 1 pair with 4 first. so the number of pairs determines how many rows to add. &lt;BR /&gt;
&lt;BR /&gt;
now the first number of the pair determines where to start the 1. for example (2 2) says start the 1 in the 2nd position 0 1 1 1. (2 3) says start the  1 in the 2nd position 0 1 1 1&lt;BR /&gt;
&lt;BR /&gt;
and so on&lt;BR /&gt;
&lt;BR /&gt;
im sorry if this is confusing but i cant figure out how to explain it. there is a clear structure to the matrix.&lt;BR /&gt;
&lt;BR /&gt;
i just want to have a macro that i can give the number of observations n, and it will spit out the n(n+1)/2 by n+2 matrix&lt;BR /&gt;
&lt;BR /&gt;
cheers</description>
      <pubDate>Tue, 26 Oct 2010 04:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22463#M92</guid>
      <dc:creator>trekvana</dc:creator>
      <dc:date>2010-10-26T04:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22464#M93</link>
      <description>Yes, this is doable. Basically, the algorithm is &lt;BR /&gt;
1) Figure out the dimensions of the matrix&lt;BR /&gt;
2) Allocate a matrix of ones&lt;BR /&gt;
3) Generate the first two columns&lt;BR /&gt;
4) Fill in the zeros&lt;BR /&gt;
&lt;BR /&gt;
I was going to post some code, but I have a question about how the first two columns are formed. You say "the first 2 columns correspond to ordered combinations," so I was going to suggest using the LEXCOMB function in Base SAS. However, there are only 6 distinct combinations (4 choose 2) of four elements taken two at a time. Combinations do not include the terms (1,1), (2,2), (3,3), or (4,4).  &lt;BR /&gt;
&lt;BR /&gt;
Then I thought, maybe you mean "permutations taken two at a time," but those don't include the (1,1), (2,2), (3,3), or (4,4) terms either, plus they DO include terms like (2,1), (3,1), (3,2), (4,1), etc.&lt;BR /&gt;
&lt;BR /&gt;
So my questions: &lt;BR /&gt;
1) What are the first two columns? Are they all combinations plus the constant terms?  &lt;BR /&gt;
&lt;BR /&gt;
2) What would the first few columns look like for 4 choose 3? Would you include (1,1,1), (1,1,2), ..., (1,2,1),...?  Or would you exclude (1,2,1) because you already have (1,1,2)?&lt;BR /&gt;
&lt;BR /&gt;
3) Maybe context would help. Are you trying to do something related to all two-way interactions in a regression model? If so, are the variables continuous or nominal?</description>
      <pubDate>Tue, 26 Oct 2010 12:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22464#M93</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2010-10-26T12:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22465#M94</link>
      <description>I think it will be easier for you to generate the first two columns in the DATA step, and then use IML for the rest of the matrix. Maybe this is close to what you want?&lt;BR /&gt;
&lt;BR /&gt;
%let n = 4;&lt;BR /&gt;
%let k = 2;&lt;BR /&gt;
&lt;BR /&gt;
data c&amp;amp;n._&amp;amp;k (keep=x1-x&amp;amp;k);&lt;BR /&gt;
   array x{&amp;amp;n};&lt;BR /&gt;
   ncomb = comb(&amp;amp;n, &amp;amp;k);&lt;BR /&gt;
   do i = 1 to &amp;amp;n; x[ i ]=i; end; &lt;BR /&gt;
   do i=1 to ncomb;&lt;BR /&gt;
      rc=lexcomb(i, &amp;amp;k, of x&lt;LI&gt;);&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;   &lt;BR /&gt;
   /** add in the repeated terms **/&lt;BR /&gt;
   do i = 1 to &amp;amp;n; &lt;BR /&gt;
      do j = 1 to &amp;amp;k; x[ j ]=i; end;&lt;BR /&gt;
      output;&lt;BR /&gt;
   end; &lt;BR /&gt;
run;&lt;BR /&gt;
proc sort; by x1-x&amp;amp;k; run;&lt;BR /&gt;
&lt;BR /&gt;
proc iml;&lt;BR /&gt;
varNames = "x1":"x&amp;amp;k";&lt;BR /&gt;
use c&amp;amp;n._&amp;amp;k;&lt;BR /&gt;
read all var varNames into c;&lt;BR /&gt;
close c&amp;amp;n._&amp;amp;k;&lt;BR /&gt;
&lt;BR /&gt;
n = &amp;amp;n; k = &amp;amp;k;&lt;BR /&gt;
nrows = nrow(c);&lt;BR /&gt;
x = j(nrows, n+2, 1);&lt;BR /&gt;
x[, 1:2] = c;&lt;BR /&gt;
do i = n+1 to nrows;&lt;BR /&gt;
   idx = 3:(c[ i,1 ]+1);&lt;BR /&gt;
   x[ i, idx ] = 0;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
print x;&lt;/LI&gt;</description>
      <pubDate>Tue, 26 Oct 2010 13:20:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22465#M94</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2010-10-26T13:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22466#M95</link>
      <description>Rick -&lt;BR /&gt;
&lt;BR /&gt;
so there will always only be 2 first columns corresponding to the covariance matrix parameter indices. so imagine a 4x4 covariance matrix (which is symmetric) with the indices (1,1) (1,2) (1,3) (1,4) (2,2) (2,3) , ect. the fact that it is symmetric allows us not to worry about (2,1) (3,1) (3,2) and so on. now that i think about it ordered permuations was not the correct way to describe that. &lt;BR /&gt;
&lt;BR /&gt;
once we have the indices then we add 4 more columns (or in general if the covariance matrix is nxn then we add n more columns) corresponding to ones starting at the first number of the index.&lt;BR /&gt;
&lt;BR /&gt;
so the row starting with (&lt;B&gt;2&lt;/B&gt;,3) will be 2 3 0 &lt;B&gt;1&lt;/B&gt; 1 1&lt;BR /&gt;
&lt;BR /&gt;
thus we will have zeros up the first index-1 (in this example 2-1=1 and then starting at 2 we have all ones

Message was edited by: trekvana</description>
      <pubDate>Tue, 26 Oct 2010 16:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22466#M95</guid>
      <dc:creator>trekvana</dc:creator>
      <dc:date>2010-10-26T16:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22467#M96</link>
      <description>Rick-&lt;BR /&gt;
&lt;BR /&gt;
thanks for the code. it works great. this is exactly what i wanted. just plug in n and get the matrix&lt;BR /&gt;
&lt;BR /&gt;
cheers

Message was edited by: trekvana</description>
      <pubDate>Tue, 26 Oct 2010 16:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22467#M96</guid>
      <dc:creator>trekvana</dc:creator>
      <dc:date>2010-10-26T16:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22468#M97</link>
      <description>one more question. how can output the x matrix into a sas data set, say A, with the columns labeled Parm Row Col1 Col2 ... Coln&lt;BR /&gt;
&lt;BR /&gt;
i know i can create the actual matrix like this but not sure how to rename the columns&lt;BR /&gt;
&lt;BR /&gt;
create A from x;&lt;BR /&gt;
append from x;&lt;BR /&gt;
close A;

Message was edited by: trekvana</description>
      <pubDate>Tue, 26 Oct 2010 16:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22468#M97</guid>
      <dc:creator>trekvana</dc:creator>
      <dc:date>2010-10-26T16:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22469#M98</link>
      <description>Great! I mistakenly thought that "k" could also vary, but I now understand that k=2 for your problem.  In that case, we don't really need the DATA step code; we could do it entirely in IML.</description>
      <pubDate>Tue, 26 Oct 2010 16:56:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22469#M98</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2010-10-26T16:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to create special matrix with proc iml</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22470#M99</link>
      <description>varNames = {"Row" "Col"} || ("Col1":"Col&amp;amp;n");&lt;BR /&gt;
create A from x[colname=varNames];&lt;BR /&gt;
append from x;&lt;BR /&gt;
close A;</description>
      <pubDate>Tue, 26 Oct 2010 17:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-create-special-matrix-with-proc-iml/m-p/22470#M99</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2010-10-26T17:17:59Z</dc:date>
    </item>
  </channel>
</rss>

