<?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: transpose row data into multi columns in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66893#M19117</link>
    <description>Well this is one way of doing it[pre]&lt;BR /&gt;
data input;&lt;BR /&gt;
  length a b c d $1;&lt;BR /&gt;
  input a b c d;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
x x x x&lt;BR /&gt;
y y y y&lt;BR /&gt;
z z z z&lt;BR /&gt;
x x x x&lt;BR /&gt;
;&lt;BR /&gt;
run;[/pre][pre]&lt;BR /&gt;
* find variable names, number of varibales and number of obs *;&lt;BR /&gt;
proc sql NOPRINT;&lt;BR /&gt;
  select name into :varnames SEPARATED by ','&lt;BR /&gt;
  from sashelp.vcolumn&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
&lt;BR /&gt;
  select nobs into :nobs&lt;BR /&gt;
  from sashelp.vtable&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
&lt;BR /&gt;
  select nvar into :nvar&lt;BR /&gt;
  from sashelp.vtable&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
quit;;[/pre][pre]&lt;BR /&gt;
* create list with new variables *;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  length _x_ $2000;&lt;BR /&gt;
  do vars = 1 to &amp;amp;nvar.;&lt;BR /&gt;
   do obs = 1 to &amp;amp;nobs.;&lt;BR /&gt;
    _x_ = catx(' ',_x_,cats(scan("&amp;amp;varnames.",obs,','),vars));&lt;BR /&gt;
   end;&lt;BR /&gt;
  end;&lt;BR /&gt;
  call symput('varlist',strip(_x_));&lt;BR /&gt;
run;;[/pre][pre]&lt;BR /&gt;
* transpose input data into one record *;&lt;BR /&gt;
data transposed(keep=&amp;amp;varlist.);&lt;BR /&gt;
  retain &amp;amp;varlist.;&lt;BR /&gt;
  array vars{&amp;amp;nvar.,&amp;amp;nobs.} $1. &amp;amp;varlist.;&lt;BR /&gt;
  set input end=last;&lt;BR /&gt;
  do i = 1 to &amp;amp;nvar.;&lt;BR /&gt;
   x=scan("&amp;amp;varnames",i,','); &lt;BR /&gt;
   y=vvaluex(x);&lt;BR /&gt;
   vars{_n_,i}=vvaluex(x);  &lt;BR /&gt;
  end;&lt;BR /&gt;
  if last then do; output; end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Mon, 22 Dec 2008 21:46:51 GMT</pubDate>
    <dc:creator>GertNissen</dc:creator>
    <dc:date>2008-12-22T21:46:51Z</dc:date>
    <item>
      <title>transpose row data into multi columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66892#M19116</link>
      <description>Below are the contents of my dataset with 4 observations and 4 columns(a,b,c,d) with values x, y, z, x in each observation.&lt;BR /&gt;
   a b c d&lt;BR /&gt;
1 x x x x&lt;BR /&gt;
2 y y y y&lt;BR /&gt;
3 z z z z&lt;BR /&gt;
4 x x x x&lt;BR /&gt;
&lt;BR /&gt;
Now, I need to transform this into below structure:&lt;BR /&gt;
&lt;BR /&gt;
a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4&lt;BR /&gt;
== == == == == == == == == == == == == == == == &lt;BR /&gt;
 x   x   x   x    y   y   y    y   z   z   z   z   x   x    x   x&lt;BR /&gt;
&lt;BR /&gt;
In the other sense, all the row data needs to be transformed into columns (appending each column name with the observation a1, b1, a2, b2 etc...)&lt;BR /&gt;
&lt;BR /&gt;
How I can achieve this, any easy way to accomplish this?</description>
      <pubDate>Mon, 22 Dec 2008 19:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66892#M19116</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-12-22T19:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: transpose row data into multi columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66893#M19117</link>
      <description>Well this is one way of doing it[pre]&lt;BR /&gt;
data input;&lt;BR /&gt;
  length a b c d $1;&lt;BR /&gt;
  input a b c d;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
x x x x&lt;BR /&gt;
y y y y&lt;BR /&gt;
z z z z&lt;BR /&gt;
x x x x&lt;BR /&gt;
;&lt;BR /&gt;
run;[/pre][pre]&lt;BR /&gt;
* find variable names, number of varibales and number of obs *;&lt;BR /&gt;
proc sql NOPRINT;&lt;BR /&gt;
  select name into :varnames SEPARATED by ','&lt;BR /&gt;
  from sashelp.vcolumn&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
&lt;BR /&gt;
  select nobs into :nobs&lt;BR /&gt;
  from sashelp.vtable&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
&lt;BR /&gt;
  select nvar into :nvar&lt;BR /&gt;
  from sashelp.vtable&lt;BR /&gt;
  where libname = 'WORK' and memname = 'INPUT';&lt;BR /&gt;
quit;;[/pre][pre]&lt;BR /&gt;
* create list with new variables *;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  length _x_ $2000;&lt;BR /&gt;
  do vars = 1 to &amp;amp;nvar.;&lt;BR /&gt;
   do obs = 1 to &amp;amp;nobs.;&lt;BR /&gt;
    _x_ = catx(' ',_x_,cats(scan("&amp;amp;varnames.",obs,','),vars));&lt;BR /&gt;
   end;&lt;BR /&gt;
  end;&lt;BR /&gt;
  call symput('varlist',strip(_x_));&lt;BR /&gt;
run;;[/pre][pre]&lt;BR /&gt;
* transpose input data into one record *;&lt;BR /&gt;
data transposed(keep=&amp;amp;varlist.);&lt;BR /&gt;
  retain &amp;amp;varlist.;&lt;BR /&gt;
  array vars{&amp;amp;nvar.,&amp;amp;nobs.} $1. &amp;amp;varlist.;&lt;BR /&gt;
  set input end=last;&lt;BR /&gt;
  do i = 1 to &amp;amp;nvar.;&lt;BR /&gt;
   x=scan("&amp;amp;varnames",i,','); &lt;BR /&gt;
   y=vvaluex(x);&lt;BR /&gt;
   vars{_n_,i}=vvaluex(x);  &lt;BR /&gt;
  end;&lt;BR /&gt;
  if last then do; output; end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 22 Dec 2008 21:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66893#M19117</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2008-12-22T21:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: transpose row data into multi columns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66894#M19118</link>
      <description>use the following to cover both variable types.&lt;BR /&gt;
/*&lt;BR /&gt;
data input;&lt;BR /&gt;
  length a b $1 c d $2;&lt;BR /&gt;
  input a b c d gm;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
x x x x 1&lt;BR /&gt;
y y y y 0 &lt;BR /&gt;
z z z z 3&lt;BR /&gt;
x x x x 4&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
	call execute('data transposed;');&lt;BR /&gt;
	length name $ 200 type $ 1 length 8;&lt;BR /&gt;
	do _n_=1 by 1 until (last);&lt;BR /&gt;
		set input end=last;&lt;BR /&gt;
		do until(name eq '');&lt;BR /&gt;
			call vnext(name, type, length);&lt;BR /&gt;
			if name not in ('', 'name', 'type', 'length', '_N_', '_ERROR_', 'last') then&lt;BR /&gt;
			if type eq 'N' then&lt;BR /&gt;
				call execute(cats(name, put(_n_, best.)) || '=' || vvaluex(name) || ';');&lt;BR /&gt;
			else&lt;BR /&gt;
				call execute(cats(name, put(_n_, best.)) || '="' || vvaluex(name) || '";');&lt;BR /&gt;
		end;&lt;BR /&gt;
	end;&lt;BR /&gt;
	call execute('run;');&lt;BR /&gt;
	stop;&lt;BR /&gt;
run;&lt;BR /&gt;
*/</description>
      <pubDate>Tue, 23 Dec 2008 03:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/transpose-row-data-into-multi-columns/m-p/66894#M19118</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-12-23T03:46:53Z</dc:date>
    </item>
  </channel>
</rss>

