<?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: Transforming single column into matrix in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834583#M329938</link>
    <description>&lt;P&gt;A data step solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Add a counter variable to indicate which values go in which columns, change the value in the MOD() function to generate matrices of different sizes.&lt;/LI&gt;
&lt;LI&gt;Use PROC TRANSPOSE to flip the data, specifying the PREFIX to have the columns named P1-P3&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
set have;
retain counter 0;
if mod(_n_, 3) = 1 then counter+1;
run;

proc transpose data=have2 out=want prefix=P;
by counter;
var p1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Sep 2022 21:49:18 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-09-21T21:49:18Z</dc:date>
    <item>
      <title>Transforming single column into matrix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834444#M329903</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a single column dataset, which I would like to transform into 3x3 matrix.&lt;/P&gt;&lt;P&gt;In my dataset p1 is the column which includes the variables and p2, p3 are just empty colums&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p1&amp;nbsp; &amp;nbsp; &amp;nbsp;p2&amp;nbsp; &amp;nbsp;p3&lt;/P&gt;&lt;P&gt;71&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;122&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;167&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;122&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;194&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;266&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;167&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;266&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;365&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I would like to transform this dataset into 3x3 matrix like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p1&amp;nbsp; &amp;nbsp; &amp;nbsp;p2&amp;nbsp; &amp;nbsp; &amp;nbsp;p3&lt;/P&gt;&lt;P&gt;77&amp;nbsp; &amp;nbsp; 122&amp;nbsp; 167&lt;/P&gt;&lt;P&gt;122&amp;nbsp; &amp;nbsp;294&amp;nbsp; 266&lt;/P&gt;&lt;P&gt;167&amp;nbsp; &amp;nbsp;266&amp;nbsp; 365&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words, I would like to select three set of rows 1:3, 3:6 and 6:9 and form a matrix.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 10:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834444#M329903</guid>
      <dc:creator>Jhoony</dc:creator>
      <dc:date>2022-09-21T10:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming single column into matrix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834564#M329936</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input  p1     p2   p3;
datalines;
71    .      .
122   .      .
167   .      .
122   .      .
194   .      .
266   .      .
167   .      .
266   .      .
365   .      .
;

proc sql noprint;
      select name
      into :list separated by ' '
      from dictionary.columns
      where libname eq "WORK"
         and memname eq "HAVE"
      ;
   quit;
%put &amp;amp;=list;
proc iml;
  use have (keep=p1);
    read all into p;
    w=shape(p,3,3);
    print p,w;
  create want from w[colname={&amp;amp;list}];	
	append from w;
  close;
quit;   
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ghosh_0-1663796029055.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75450i989ED6C85BA35D10/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ghosh_0-1663796029055.png" alt="ghosh_0-1663796029055.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 21:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834564#M329936</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2022-09-21T21:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming single column into matrix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834573#M329937</link>
      <description>&lt;P&gt;I would bet you really don't want to transform your data like that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you could tell us a little more about the problem you are trying to solve.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;If you don't understand&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/78622"&gt;@ghosh&lt;/a&gt;&amp;nbsp;'s solution or&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;'s solution you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input p1; 
datalines;
71
122
167
122
194
166
167
266
365
;
run;


data one;
	set have (firstobs=1 obs=3);
	rownum=_n_;
run;

data two;
	set have (firstobs=4 obs=6);
	rownum=_n_;
run;

data three;
	set have (firstobs=7 obs=9);
	rownum=_n_;
run;

proc sql;
	create table want as 
	select one.p1 as p1, two.p1 as p2, three.p1 as p3
	from one inner join two on one.rownum = two.rownum
	inner join three on one.rownum = three.rownum;
quit; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;p1	p2	p3
71	122	167
122	194	266
167	166	365&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2022 21:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834573#M329937</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2022-09-21T21:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming single column into matrix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834583#M329938</link>
      <description>&lt;P&gt;A data step solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Add a counter variable to indicate which values go in which columns, change the value in the MOD() function to generate matrices of different sizes.&lt;/LI&gt;
&lt;LI&gt;Use PROC TRANSPOSE to flip the data, specifying the PREFIX to have the columns named P1-P3&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
set have;
retain counter 0;
if mod(_n_, 3) = 1 then counter+1;
run;

proc transpose data=have2 out=want prefix=P;
by counter;
var p1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Sep 2022 21:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834583#M329938</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-09-21T21:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming single column into matrix</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834613#M329944</link>
      <description>&lt;P&gt;Simple enough with an array. But the input variable name should not be one of the target variable names, that just complicates things.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input X; 
datalines;
71
122
167
122
194
166
167
266
365
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So read 3 observations at a time and insert them into the proper variable using an index into an array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array p p1-p3;
  do column=1 to 3 until(eof);
    set have end=eof;
    p[column]=x;
  end;
  drop column x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are actually reading the data from a text file then just read it originally directly into the array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input p1-p3;
datalines;
71
122
167
122
194
166
167
266
365
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Sep 2022 04:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-single-column-into-matrix/m-p/834613#M329944</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-22T04:53:50Z</dc:date>
    </item>
  </channel>
</rss>

