<?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: Transform dataset using a pivot in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820344#M323787</link>
    <description>&lt;P&gt;If you really only have a few stocks, then this will do what you need, without transposing and sorting:.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (drop=apple msft  rename=(index=value) in=in_index)
      have (drop=index msft  rename=(apple=value) in=in_apple)
      have (drop=index apple rename=(msft=value)  in=in_msft) ;
  if in_index=1 then stock='Index'; else
  if in_apple=1 then stock='apple'; else
  if in_msft=1  then stock='msft'; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a lot more stocks, then something like will still avoid sorting, but take a lot more memory:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=date stock value);
  if 0 then set have;
  length stock $8;
  array stocks {*} index--msft;

  declare hash h(dataset:'have',ordered:'a');
    h.definekey('date');
    h.definedata(all:'Y');
    h.definedone();
  declare hiter hi ('h');

  do s=1 to dim(stocks);
    stock=vname(stocks{s});
    do rc=hi.first() by 0 until (hi.next()^=0);
      value=stocks{s};
      output;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 25 Jun 2022 04:08:06 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-06-25T04:08:06Z</dc:date>
    <item>
      <title>Transform dataset using a pivot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820342#M323786</link>
      <description>&lt;P&gt;I'm hoping you can help me with some code to change observations to variables, I have data with a structure like so:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;date Index apple msft&lt;BR /&gt;02-01-2022 575 656 595&lt;BR /&gt;03-01-2022 544 682 564&lt;BR /&gt;04-01-2022 598 641 579&lt;BR /&gt;05-01-2022 718 770 589&lt;BR /&gt;06-01-2022 553 528 659&lt;BR /&gt;07-01-2022 701 500 639&lt;BR /&gt;08-01-2022 560 727 603&lt;BR /&gt;09-01-2022 733 718 774&lt;BR /&gt;10-01-2022 626 663 651&lt;BR /&gt;11-01-2022 740 715 679&lt;BR /&gt;12-01-2022 614 512 533&lt;BR /&gt;13-01-2022 517 720 634&lt;BR /&gt;… … … …&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I would like to generate a dataset with the following structure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;date stock value&lt;BR /&gt;02-01-2022 Index 575&lt;BR /&gt;03-01-2022 Index 544&lt;BR /&gt;04-01-2022 Index 598&lt;BR /&gt;05-01-2022 Index 718&lt;BR /&gt;06-01-2022 Index 553&lt;BR /&gt;… … …&lt;BR /&gt;02-01-2022 apple 656&lt;BR /&gt;03-01-2022 apple 682&lt;BR /&gt;04-01-2022 apple 641&lt;BR /&gt;05-01-2022 apple 770&lt;BR /&gt;06-01-2022 apple 528&lt;BR /&gt;… … …&lt;BR /&gt;02-01-2022 msft 595&lt;BR /&gt;03-01-2022 msft 564&lt;BR /&gt;04-01-2022 msft 579&lt;BR /&gt;05-01-2022 msft 589&lt;BR /&gt;06-01-2022 msft 659&lt;BR /&gt;… … …&lt;/P&gt;
&lt;P&gt;help me?&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2022 03:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820342#M323786</guid>
      <dc:creator>rcolipi</dc:creator>
      <dc:date>2022-06-25T03:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: Transform dataset using a pivot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820344#M323787</link>
      <description>&lt;P&gt;If you really only have a few stocks, then this will do what you need, without transposing and sorting:.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (drop=apple msft  rename=(index=value) in=in_index)
      have (drop=index msft  rename=(apple=value) in=in_apple)
      have (drop=index apple rename=(msft=value)  in=in_msft) ;
  if in_index=1 then stock='Index'; else
  if in_apple=1 then stock='apple'; else
  if in_msft=1  then stock='msft'; 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a lot more stocks, then something like will still avoid sorting, but take a lot more memory:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=date stock value);
  if 0 then set have;
  length stock $8;
  array stocks {*} index--msft;

  declare hash h(dataset:'have',ordered:'a');
    h.definekey('date');
    h.definedata(all:'Y');
    h.definedone();
  declare hiter hi ('h');

  do s=1 to dim(stocks);
    stock=vname(stocks{s});
    do rc=hi.first() by 0 until (hi.next()^=0);
      value=stocks{s};
      output;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2022 04:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820344#M323787</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-25T04:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: Transform dataset using a pivot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820385#M323803</link>
      <description>&lt;P&gt;Indeed, there are hundreds of stocks. It works perfectly, thank you very much&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jun 2022 15:08:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transform-dataset-using-a-pivot/m-p/820385#M323803</guid>
      <dc:creator>rcolipi</dc:creator>
      <dc:date>2022-06-25T15:08:31Z</dc:date>
    </item>
  </channel>
</rss>

