<?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 Data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702210#M215080</link>
    <description>&lt;P&gt;One trick for changing the order of variables in a dataset is to use some statement like RETAIN that sets the order of the variables without actually forcing the data step compiler to determine the other attributes of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  retain id y1999 y2000 ;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in your case you can use an SQL query to get the list of names in the order you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  select distinct translate(monyr,'_','-') into :varlist separated by ' '
  from have
  order by dateyr,datemon
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can transpose as normal&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want(drop=_name_);
  by fruit status ;
  id monyr ;
  var casecount;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And run another step to change the order of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  retain fruit status &amp;amp;varlist;
  set want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    fruit     status    Nov_19    Mar_20    May_20    Jun_20    Aug_20    Jul_20    Sep_20    Oct_20

 1     APPLE     CLOSED       .         1         .         .         .         .         .         .
 2     APPLE     OPEN         .         .         .         1         .         .         .         .
 3     GRAPES    CLOSED       .         .         .         .         .         .         .         1
 4     GRAPES    OPEN         .         .         2         .         .         .         .         .
 5     ORANGE    CLOSED       .         .         .         .         1         .         .         .
 6     ORANGE    OPEN         .         .         .         .         .         2         .         .
 7     PEAR      CLOSED       .         .         .         .         .         .         1         .
 8     PEAR      OPEN         1         .         .         .         .         .         .         .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 28 Nov 2020 17:58:26 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-11-28T17:58:26Z</dc:date>
    <item>
      <title>Transpose Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702204#M215079</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;I have a dataset in the long format that I would like to transpose to a wide dataset. I would also like&lt;BR /&gt;the months to remain in sequential order. The dataset is attached. The long data set is in this format: &lt;BR /&gt;FRUIT STATUS DATEMON DATEYR MONYR CASECOUNT&lt;BR /&gt;APPLE CLOSED 3 2020 Mar-20 1&lt;BR /&gt;APPLE OPEN 6 2020 Jun-20 1&lt;BR /&gt;GRAPES CLOSED 10 2020 Oct-20 1&lt;BR /&gt;GRAPES OPEN 5 2020 May-20 2&lt;BR /&gt;ORANGE CLOSED 8 2020 Aug-20 1&lt;BR /&gt;ORANGE OPEN 8 2020 Jul-20 2&lt;BR /&gt;PEAR CLOSED 9 2020 Sep-20 1&lt;BR /&gt;PEAR OPEN 11 2019 Nov-19 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like the data set to look like below with the casecounts corresponding to the month they were opened or closed. For example, in March 2020, apple had one casecount therefore march 2020 would have 1 for apple with a closed status and so on.&amp;nbsp;&lt;BR /&gt;FRUIT STATUS Nov-19 Mar-20 May-20 Jun-20 Jul-20 Aug-20 Sep-20 Oct-20&lt;BR /&gt;APPLE CLOSED&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;APPLE OPEN&amp;nbsp;&lt;BR /&gt;GRAPES CLOSED&amp;nbsp;&lt;BR /&gt;GRAPES OPEN&amp;nbsp;&lt;BR /&gt;ORANGE CLOSED&amp;nbsp;&lt;BR /&gt;ORANGE OPEN&amp;nbsp;&lt;BR /&gt;PEAR CLOSED&amp;nbsp;&lt;BR /&gt;PEAR OPEN&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance would be great. Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2020 17:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702204#M215079</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-11-28T17:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702210#M215080</link>
      <description>&lt;P&gt;One trick for changing the order of variables in a dataset is to use some statement like RETAIN that sets the order of the variables without actually forcing the data step compiler to determine the other attributes of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  retain id y1999 y2000 ;
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in your case you can use an SQL query to get the list of names in the order you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  select distinct translate(monyr,'_','-') into :varlist separated by ' '
  from have
  order by dateyr,datemon
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can transpose as normal&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want(drop=_name_);
  by fruit status ;
  id monyr ;
  var casecount;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And run another step to change the order of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  retain fruit status &amp;amp;varlist;
  set want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    fruit     status    Nov_19    Mar_20    May_20    Jun_20    Aug_20    Jul_20    Sep_20    Oct_20

 1     APPLE     CLOSED       .         1         .         .         .         .         .         .
 2     APPLE     OPEN         .         .         .         1         .         .         .         .
 3     GRAPES    CLOSED       .         .         .         .         .         .         .         1
 4     GRAPES    OPEN         .         .         2         .         .         .         .         .
 5     ORANGE    CLOSED       .         .         .         .         1         .         .         .
 6     ORANGE    OPEN         .         .         .         .         .         2         .         .
 7     PEAR      CLOSED       .         .         .         .         .         .         1         .
 8     PEAR      OPEN         1         .         .         .         .         .         .         .&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2020 17:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702210#M215080</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-28T17:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702215#M215081</link>
      <description>&lt;P&gt;The code works great! Also, I appreciate you providing a thorough explanation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 28 Nov 2020 18:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data/m-p/702215#M215081</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-11-28T18:41:02Z</dc:date>
    </item>
  </channel>
</rss>

