<?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: Restructure dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237543#M43540</link>
    <description>&lt;P&gt;Why?&lt;/P&gt;</description>
    <pubDate>Thu, 03 Dec 2015 08:36:24 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2015-12-03T08:36:24Z</dc:date>
    <item>
      <title>Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237506#M43536</link>
      <description>&lt;P&gt;How can I restructure a dataset like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A &amp;nbsp;B &amp;nbsp;C&lt;/P&gt;&lt;P&gt;1 &amp;nbsp;4 &amp;nbsp;3&lt;/P&gt;&lt;P&gt;1 &amp;nbsp;6 &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2 &amp;nbsp;7 &amp;nbsp;6&lt;/P&gt;&lt;P&gt;2 &amp;nbsp;3 &amp;nbsp;7&lt;/P&gt;&lt;P&gt;3 &amp;nbsp;2 &amp;nbsp;8&lt;/P&gt;&lt;P&gt;3 &amp;nbsp;1 &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A B1 B2 C1 C2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp;4 &amp;nbsp; &amp;nbsp;6 &amp;nbsp; 3 &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2 &amp;nbsp;7 &amp;nbsp; &amp;nbsp;3 &amp;nbsp; 6 &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;3 &amp;nbsp;2 &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 8 &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the code if i want to do it by ARRAY or without ARRAY?&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 00:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237506#M43536</guid>
      <dc:creator>forsignupar</dc:creator>
      <dc:date>2015-12-03T00:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237510#M43537</link>
      <description>&lt;P&gt;Here are two versions that should give you what you need. Enjoy.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
  LENGTH A B C 8;
  INPUT A B C;
  DATALINES;
1  4  3
1  6  4
2  7  6
2  3  7
3  2  8
3  1  1
;
RUN;


/* don't run PROC SORT because it will change the order of variables B and C */
/* PROC SORT DATA=have; 
     BY A B C; 
   RUN; 
*/


/**************************************/
/* version 1 - pure data step version */
/**************************************/
DATA want1(DROP=B C);
  SET have;
  BY A;
  LENGTH B1 B2 C1 C2 8;
  IF FIRST.A THEN DO;
    B1 = B;
    C1 = C;
    RETAIN B1 C1;
    DELETE;
  END;
  IF LAST.A THEN DO;
    B2 = B;
    C2 = C;
  END;
RUN;


/********************************************/
/* version 2 - data step + PROC SQL version */
/********************************************/
DATA have;
  SET have;
  BY A;
  IF FIRST.A THEN counter_a = 1;
  ELSE            counter_a + 1;
RUN;


PROC SQL;
  CREATE TABLE want2 AS 
  SELECT A
       , MAX(CASE WHEN counter_a = 1 THEN B END) AS B1
       , MAX(CASE WHEN counter_a = 2 THEN B END) AS B2
       , MAX(CASE WHEN counter_a = 1 THEN C END) AS C1
       , MAX(CASE WHEN counter_a = 2 THEN C END) AS C2
  FROM have
  GROUP BY A;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 01:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237510#M43537</guid>
      <dc:creator>hbi</dc:creator>
      <dc:date>2015-12-03T01:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237514#M43538</link>
      <description>Here's two references:&lt;BR /&gt;Via an Array&lt;BR /&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/longtowide_data.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/longtowide_data.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Via proc transpose&lt;BR /&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/ltow_transpose.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/ltow_transpose.htm&lt;/A&gt;</description>
      <pubDate>Thu, 03 Dec 2015 02:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237514#M43538</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-03T02:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237515#M43539</link>
      <description>&lt;P&gt;Without arrays&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc transpose data=have out=ab(drop=_name_) prefix=B; by A; var B; run;
proc transpose data=have out=ac(drop=_name_) prefix=C; by A; var C; run;

data want;
merge ab ac; by a;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Dec 2015 02:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237515#M43539</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-03T02:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237543#M43540</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 08:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237543#M43540</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-12-03T08:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237580#M43545</link>
      <description>&lt;P&gt;Here's a solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input A&amp;nbsp; B&amp;nbsp; C;&lt;BR /&gt;cards;&lt;BR /&gt;1&amp;nbsp; 4&amp;nbsp; 3&lt;BR /&gt;1&amp;nbsp; 6&amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; 7&amp;nbsp; 6&lt;BR /&gt;2&amp;nbsp; 3&amp;nbsp; 7&lt;BR /&gt;3&amp;nbsp; 2&amp;nbsp; 8&lt;BR /&gt;3&amp;nbsp; 1&amp;nbsp; 1&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want(rename=(b = b2 c = c2));&lt;BR /&gt;set have;&lt;BR /&gt;by a;&lt;BR /&gt;retain b1 c1;&lt;BR /&gt;if first.a then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;b1 = b;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;c1 = c;&lt;BR /&gt;end;&lt;BR /&gt;if last.a then output;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 13:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237580#M43545</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-12-03T13:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237613#M43556</link>
      <description>&lt;P&gt;thankyou so much&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 16:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-dataset/m-p/237613#M43556</guid>
      <dc:creator>forsignupar</dc:creator>
      <dc:date>2015-12-03T16:26:15Z</dc:date>
    </item>
  </channel>
</rss>

