<?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 Stacking columns into new dataset in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497084#M72444</link>
    <description>&lt;P&gt;I want to be able to transpose data entered in multiple columns into a dataset that stacks those columns but still preserves the identifiers for those data and changes column names to represent the new structure. I've tried the macro examples given in other threads, but they don't give the me what I want.&amp;nbsp; I am using SAS 9.4 on a Windows 10 desktop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is an example of the initial dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data yield;&lt;BR /&gt;input trt rep f1 f2;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 20 25&lt;BR /&gt;1 2 17 15&lt;BR /&gt;1 3 30 35&lt;BR /&gt;1 4 25 25&lt;BR /&gt;2 1 35 33&lt;BR /&gt;2 2 30 35&lt;BR /&gt;2 3 37 38&lt;BR /&gt;2 4 32 37&lt;BR /&gt;3 1 42 44&lt;BR /&gt;3 2 43 45&lt;BR /&gt;3 3 40 45&lt;BR /&gt;3 4 43 44&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data yield;&lt;BR /&gt;input trt rep field yield;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 20&lt;BR /&gt;1 2 1 17&lt;BR /&gt;1 3 1 30&lt;BR /&gt;1 4 1 25&lt;BR /&gt;2 1 1 35&lt;BR /&gt;2 2 1 30&lt;BR /&gt;2 3 1 37&lt;BR /&gt;2 4 1 32&lt;BR /&gt;3 1 1 42&lt;BR /&gt;3 2 1 43&lt;BR /&gt;3 3 1 40&lt;BR /&gt;3 4 1 43&lt;BR /&gt;1 1 2 25&lt;BR /&gt;1 2 2 15&lt;BR /&gt;1 3 2 35&lt;BR /&gt;1 4 2 25&lt;BR /&gt;2 1 2 33&lt;BR /&gt;2 2 2 35&lt;BR /&gt;2 3 2 38&lt;BR /&gt;2 4 2 37&lt;BR /&gt;3 1 2 44&lt;BR /&gt;3 2 2 45&lt;BR /&gt;3 3 2 45&lt;BR /&gt;3 4 2 44&lt;BR /&gt;;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Sep 2018 18:46:52 GMT</pubDate>
    <dc:creator>mthorne</dc:creator>
    <dc:date>2018-09-19T18:46:52Z</dc:date>
    <item>
      <title>Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497084#M72444</link>
      <description>&lt;P&gt;I want to be able to transpose data entered in multiple columns into a dataset that stacks those columns but still preserves the identifiers for those data and changes column names to represent the new structure. I've tried the macro examples given in other threads, but they don't give the me what I want.&amp;nbsp; I am using SAS 9.4 on a Windows 10 desktop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is an example of the initial dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data yield;&lt;BR /&gt;input trt rep f1 f2;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 20 25&lt;BR /&gt;1 2 17 15&lt;BR /&gt;1 3 30 35&lt;BR /&gt;1 4 25 25&lt;BR /&gt;2 1 35 33&lt;BR /&gt;2 2 30 35&lt;BR /&gt;2 3 37 38&lt;BR /&gt;2 4 32 37&lt;BR /&gt;3 1 42 44&lt;BR /&gt;3 2 43 45&lt;BR /&gt;3 3 40 45&lt;BR /&gt;3 4 43 44&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data yield;&lt;BR /&gt;input trt rep field yield;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 20&lt;BR /&gt;1 2 1 17&lt;BR /&gt;1 3 1 30&lt;BR /&gt;1 4 1 25&lt;BR /&gt;2 1 1 35&lt;BR /&gt;2 2 1 30&lt;BR /&gt;2 3 1 37&lt;BR /&gt;2 4 1 32&lt;BR /&gt;3 1 1 42&lt;BR /&gt;3 2 1 43&lt;BR /&gt;3 3 1 40&lt;BR /&gt;3 4 1 43&lt;BR /&gt;1 1 2 25&lt;BR /&gt;1 2 2 15&lt;BR /&gt;1 3 2 35&lt;BR /&gt;1 4 2 25&lt;BR /&gt;2 1 2 33&lt;BR /&gt;2 2 2 35&lt;BR /&gt;2 3 2 38&lt;BR /&gt;2 4 2 37&lt;BR /&gt;3 1 2 44&lt;BR /&gt;3 2 2 45&lt;BR /&gt;3 3 2 45&lt;BR /&gt;3 4 2 44&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:46:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497084#M72444</guid>
      <dc:creator>mthorne</dc:creator>
      <dc:date>2018-09-19T18:46:52Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497091#M72445</link>
      <description>&lt;P&gt;How many columns f1...fn do you have? is it just the 2 or can vary?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497091#M72445</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T18:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497092#M72446</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data yield;
input trt rep f1 f2;
cards;
1 1 20 25
1 2 17 15
1 3 30 35
1 4 25 25
2 1 35 33
2 2 30 35
2 3 37 38
2 4 32 37
3 1 42 44
3 2 43 45
3 3 40 45
3 4 43 44
;

data one(keep=trt rep yield);
   set yield;
   rename f1=yield;
run;

data two(keep=trt rep yield);
   set yield;
   rename f2=yield;
run;

data want;
   set one two indsname=source;
   field=scan(source,2,'.');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497092#M72446</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-09-19T19:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497093#M72447</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this example I just used two, but it would depend on the dataset. This is an issue when I do measurements over time on the same experimental unit (plot of ground), so some datasets may have five or six columns of measurements made over time on the same plot.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497093#M72447</guid>
      <dc:creator>mthorne</dc:creator>
      <dc:date>2018-09-19T19:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497094#M72448</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data yield;
input trt rep f1 f2;
cards;
1 1 20 25
1 2 17 15
1 3 30 35
1 4 25 25
2 1 35 33
2 2 30 35
2 3 37 38
2 4 32 37
3 1 42 44
3 2 43 45
3 3 40 45
3 4 43 44
;

data temp;
set yield;
array t(*) f1 f2;
do field=1 to dim(t);
yield=t(field);
output;
end;
keep trt rep field yield;
run;

proc sort data=temp out=want;
by field;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/234662"&gt;@mthorne&lt;/a&gt;, you definitely need an array and i am glad i asked that question. See if the above works&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497094#M72448</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T19:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497095#M72449</link>
      <description>&lt;P&gt;and some hash fun:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data yield;
input trt rep f1 f2;
cards;
1 1 20 25
1 2 17 15
1 3 30 35
1 4 25 25
2 1 35 33
2 2 30 35
2 3 37 38
2 4 32 37
3 1 42 44
3 2 43 45
3 3 40 45
3 4 43 44
;

data _null_;
if _n_=1 then do;
dcl hash h(multidata:'y',ordered:'y');
h.definekey('field');
h.definedata('trt', 'rep', 'field', 'yield');
h.definedone();
end;
set yield end=lr;
array t(*) f:;
do field=1 to dim(t);
yield=t(field);
rc=h.add();
end;
if lr then h.output(dataset:'want');
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497095#M72449</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T19:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking columns into new dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497097#M72450</link>
      <description>&lt;P&gt;Or as simple as this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data yield;
input trt rep f1 f2;
cards;
1 1 20 25
1 2 17 15
1 3 30 35
1 4 25 25
2 1 35 33
2 2 30 35
2 3 37 38
2 4 32 37
3 1 42 44
3 2 43 45
3 3 40 45
3 4 43 44
;

proc transpose data=yield out=t(rename=(_name_=field)) prefix=yield;
by trt rep;
var f:;
run;

proc sort data=t out=want;
by field;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Stacking-columns-into-new-dataset/m-p/497097#M72450</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-19T19:24:39Z</dc:date>
    </item>
  </channel>
</rss>

