<?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: Transposing a table with two group levels in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909650#M358772</link>
    <description>&lt;P&gt;Since you posted photographs instead of data let's make a dataset that looks like your data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (A B C D E ) (:$8.);
cards;
Duration Life Life Health Health
Type     Main Sub  Main   Sub
1        0.9  0.8  0.7    0.8
2        1    0.9  0.8    0.9
3        1.1  1    0.9    1
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(PROC IMPORT should generate the same thing if you use GETNAMES=NO).&lt;/P&gt;
&lt;P&gt;Now we can transpose the two parts separately and recombine.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(obs=2) out=names ;
  by A ;
  var B--E ;
run;
proc transpose data=have(firstobs=3) out=values;
  by A ;
  var B--E ;
run;

proc sql ;
create table want as select 
  input(a.a,32.) as duration
, b.col1 as product
, c.col1 as type
, input(a.col1,32.) as value
from values a
   , names(where=(A='Duration')) b
   , names(where=(A='Type')) c
where a._name_=b._name_ and a._name_=c._name_ and b._name_=c._name_
order by 1,2,3
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;OBS    duration    product    type    value

  1        1       Health     Main     0.7
  2        1       Health     Sub      0.8
  3        1       Life       Main     0.9
  4        1       Life       Sub      0.8
  5        2       Health     Main     0.8
  6        2       Health     Sub      0.9
  7        2       Life       Main     1.0
  8        2       Life       Sub      0.9
  9        3       Health     Main     0.9
 10        3       Health     Sub      1.0
 11        3       Life       Main     1.1
 12        3       Life       Sub      1.0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Dec 2023 16:01:25 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-12-25T16:01:25Z</dc:date>
    <item>
      <title>Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909637#M358763</link>
      <description>&lt;P&gt;I have an excel table with values that depend on Product and Type.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that the first row is Product, second row is Type, and the next rows are values, depending on the first two rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I transpose it in SAS so that I can easily query on that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the example attached.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="example.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92066i4912D2477027E863/image-size/medium?v=v2&amp;amp;px=400" role="button" title="example.png" alt="example.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 14:18:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909637#M358763</guid>
      <dc:creator>yaniv_daaata</dc:creator>
      <dc:date>2023-12-25T14:18:16Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909638#M358764</link>
      <description>&lt;P&gt;Most of us will not download Excel files as they can be a security threat. Please provide (a portion of) your data as working SAS data step code (and not in any other format). Examples and instructions: &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 14:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909638#M358764</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-25T14:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909640#M358765</link>
      <description>&lt;P&gt;I changed it to image. Hope it is ok now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 14:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909640#M358765</guid>
      <dc:creator>yaniv_daaata</dc:creator>
      <dc:date>2023-12-25T14:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909641#M358766</link>
      <description>&lt;P&gt;It would help if you provided the data as working SAS data step code, as I requested.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 14:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909641#M358766</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-25T14:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909643#M358767</link>
      <description>&lt;P&gt;What part of that photograph is the actual data?&lt;/P&gt;
&lt;P&gt;Do you have the data in a TEXT file, perhaps a CSV file?&amp;nbsp; That would be much easier to deal with since you can write your own program to read it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have the data in a SAS dataset? What are the variable names?&lt;/P&gt;
&lt;P&gt;Do you have the data in an Excel spreadsheet?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need to do this once? Or will you get more of these files?&amp;nbsp; If many files then explain what varies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 15:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909643#M358767</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-25T15:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909647#M358769</link>
      <description>&lt;P&gt;This is the actual data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yaniv_daaata_0-1703517040255.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92067iD0E3031FEC581545/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yaniv_daaata_0-1703517040255.png" alt="yaniv_daaata_0-1703517040255.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;It's in an excel file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the SAS table that I need:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yaniv_daaata_1-1703517127637.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92068i13FBCAAD3718F2F1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yaniv_daaata_1-1703517127637.png" alt="yaniv_daaata_1-1703517127637.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It will need to be run periodically. Not just once.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 15:12:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909647#M358769</guid>
      <dc:creator>yaniv_daaata</dc:creator>
      <dc:date>2023-12-25T15:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909648#M358770</link>
      <description>&lt;P&gt;Since the left side of your picture is not a dataset we have try and IMAGINE what dataset you actually have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you ignore the first line of that block now it looks like a dataset and something PROC TRANSPOSE will handle easily.&lt;/P&gt;
&lt;P&gt;So assuming you read from cell A2 to E8 then the variables are probably going to be named TYPE, MAIN, SUB, MAIN1, SUB1&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=want;
  by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you problem is just to create your other variables from the _NAME_ variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set want(rename=(type=Duration));
  length product&amp;nbsp;type&amp;nbsp;$8&amp;nbsp;;
&amp;nbsp;&amp;nbsp;if&amp;nbsp;upcase(_name_)=:'MAIN'&amp;nbsp;then&amp;nbsp;type='Main';&amp;nbsp;else&amp;nbsp;type='Sub';
&amp;nbsp;&amp;nbsp;if&amp;nbsp;indexc(_name_,'1') then&amp;nbsp;product='Health';&amp;nbsp;else&amp;nbsp;product='Life';
&amp;nbsp;&amp;nbsp;drop&amp;nbsp;_name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Dec 2023 15:39:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909648#M358770</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-25T15:39:41Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909649#M358771</link>
      <description>&lt;P&gt;My example was just an example. In reality, I have 10s of products.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can we not ignore the product, and import it as well?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 15:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909649#M358771</guid>
      <dc:creator>yaniv_daaata</dc:creator>
      <dc:date>2023-12-25T15:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909650#M358772</link>
      <description>&lt;P&gt;Since you posted photographs instead of data let's make a dataset that looks like your data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (A B C D E ) (:$8.);
cards;
Duration Life Life Health Health
Type     Main Sub  Main   Sub
1        0.9  0.8  0.7    0.8
2        1    0.9  0.8    0.9
3        1.1  1    0.9    1
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(PROC IMPORT should generate the same thing if you use GETNAMES=NO).&lt;/P&gt;
&lt;P&gt;Now we can transpose the two parts separately and recombine.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(obs=2) out=names ;
  by A ;
  var B--E ;
run;
proc transpose data=have(firstobs=3) out=values;
  by A ;
  var B--E ;
run;

proc sql ;
create table want as select 
  input(a.a,32.) as duration
, b.col1 as product
, c.col1 as type
, input(a.col1,32.) as value
from values a
   , names(where=(A='Duration')) b
   , names(where=(A='Type')) c
where a._name_=b._name_ and a._name_=c._name_ and b._name_=c._name_
order by 1,2,3
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;OBS    duration    product    type    value

  1        1       Health     Main     0.7
  2        1       Health     Sub      0.8
  3        1       Life       Main     0.9
  4        1       Life       Sub      0.8
  5        2       Health     Main     0.8
  6        2       Health     Sub      0.9
  7        2       Life       Main     1.0
  8        2       Life       Sub      0.9
  9        3       Health     Main     0.9
 10        3       Health     Sub      1.0
 11        3       Life       Main     1.1
 12        3       Life       Sub      1.0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 16:01:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909650#M358772</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-25T16:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing a table with two group levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909659#M358780</link>
      <description>&lt;P&gt;Thank you so much !&lt;/P&gt;&lt;P&gt;Worked like a charm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 20:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-a-table-with-two-group-levels/m-p/909659#M358780</guid>
      <dc:creator>yaniv_daaata</dc:creator>
      <dc:date>2023-12-25T20:05:24Z</dc:date>
    </item>
  </channel>
</rss>

