<?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 dataset with or without Proc Transpose in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515925#M139293</link>
    <description>&lt;P&gt;You need 2 transposes, and a little manipulation in-between:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input country :$10. a_sept17 a_oct17 b_sept17 b_oct17;
cards;
Denmark 100 99 3 5
Japan 700 350 7 9
;
run;

proc transpose data=have out=trans;
by country;
var a_: b_:;
run;

data trans1;
set trans;
name = scan(_name_,1,'_');
period = scan(_name_,2,'_');
drop _name_;
run;

proc sort data=trans1;
by country period;
run;

proc transpose
  data=trans1
  out=want (drop=_name_)
  prefix=col
;
by country period;
var col1;
id name;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;country    period    cola    colb

Denmark    oct17       99      5 
Denmark    sept17     100      3 
Japan      oct17      350      9 
Japan      sept17     700      7 
&lt;/PRE&gt;
&lt;P&gt;If you want to have really nice SAS date values, expand the manipulation step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans1;
set trans;
name = scan(_name_,1,'_');
_period = scan(_name_,2,'_');
period = input('01' !! substr(_period,1,3) !! substr(_period,length(_period) - 1),date7.);
drop _name_ _period;
format period yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 26 Nov 2018 09:17:17 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-11-26T09:17:17Z</dc:date>
    <item>
      <title>Transpose dataset with or without Proc Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515918#M139291</link>
      <description>&lt;P&gt;Hi everyone. This is a lil bit tricky for me and I am seeking for advice online. I am currently reading from a xlsx file and then want to transpose the data from wide to long. Here is the trick, have a look at the table structure:&lt;/P&gt;&lt;PRE&gt;Country    A_Sept17  A_Oct17  B_Sept17  B_Oct17
Denmark  100           99             3                5
Japan       700           350           7                9&lt;/PRE&gt;&lt;P&gt;And i want it to look like this:&lt;/P&gt;&lt;PRE&gt;Country   Period   ColA   ColB
Denmark Sept17  100     3
Denmark Oct17    99       5
Japan      Sept17  700     7
Japan      Oct17    350     9&lt;/PRE&gt;&lt;P&gt;As you all can see, It looks like kinda straightforward transpose. However, when i use PROC Transpose, i do not get the desire result. Here is my code:&lt;/P&gt;&lt;PRE&gt;proc transpose data=test out=transposed;
by country;

var 
a_sept17
a_oct17
b_factor_sept17
b_factor_oct17;
run;&lt;/PRE&gt;&lt;P&gt;The result that i got from my code:&lt;/P&gt;&lt;PRE&gt;Country   Period      Col1
Denmark a_sept17      100
Denmark a_oct17       99
Denmark b_sept17      3 &lt;BR /&gt;Denmark b_sept17      5&lt;BR /&gt;Japan   a_sept17      700&lt;BR /&gt;Japan   a_oct17       350&lt;BR /&gt;Japan   b_sept17      7&lt;BR /&gt;Japan   b_oct17       9&lt;/PRE&gt;&lt;P&gt;What could be wrong here?&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 08:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515918#M139291</guid>
      <dc:creator>WorkingMan</dc:creator>
      <dc:date>2018-11-26T08:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose dataset with or without Proc Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515925#M139293</link>
      <description>&lt;P&gt;You need 2 transposes, and a little manipulation in-between:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input country :$10. a_sept17 a_oct17 b_sept17 b_oct17;
cards;
Denmark 100 99 3 5
Japan 700 350 7 9
;
run;

proc transpose data=have out=trans;
by country;
var a_: b_:;
run;

data trans1;
set trans;
name = scan(_name_,1,'_');
period = scan(_name_,2,'_');
drop _name_;
run;

proc sort data=trans1;
by country period;
run;

proc transpose
  data=trans1
  out=want (drop=_name_)
  prefix=col
;
by country period;
var col1;
id name;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;country    period    cola    colb

Denmark    oct17       99      5 
Denmark    sept17     100      3 
Japan      oct17      350      9 
Japan      sept17     700      7 
&lt;/PRE&gt;
&lt;P&gt;If you want to have really nice SAS date values, expand the manipulation step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans1;
set trans;
name = scan(_name_,1,'_');
_period = scan(_name_,2,'_');
period = input('01' !! substr(_period,1,3) !! substr(_period,length(_period) - 1),date7.);
drop _name_ _period;
format period yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Nov 2018 09:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515925#M139293</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-11-26T09:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose dataset with or without Proc Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515926#M139294</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here you can go ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input Country $ A_Sept17  A_Oct17  B_Sept17  B_Oct17;
datalines;
Denmark 100 99 3 5
Japan 700 350 7 9
;
run;

proc transpose data=test out=want name=name1 prefix=cola;
by country notsorted;
var a_:;
run;

proc transpose data=test out=want1 name=name1 prefix=colb;
by country notsorted;
var b_:;
run;

proc sort data=want;
by country ;
run;
proc sort data=want1;
by country ;
run;

data final (drop=name1);
merge want(in=a) want1(in=b);
by country ;
if a and b;
Period=substr(name1,3);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 26 Nov 2018 09:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515926#M139294</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2018-11-26T09:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose dataset with or without Proc Transpose</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515929#M139296</link>
      <description>&lt;P&gt;Assuming those variables appear in month/year order and there is always 2 of them, its easier with arrays:&lt;/P&gt;
&lt;PRE&gt;data want (keep=country month a b);
  set have;
  array x{2} a_:;
  array y{2} b_:;
  do i=1 to 2;
    month=scan(vname(x{i}),2,"_");
    a=x{i};
    b=x{i};
    output;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Nov 2018 09:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-dataset-with-or-without-Proc-Transpose/m-p/515929#M139296</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-26T09:47:48Z</dc:date>
    </item>
  </channel>
</rss>

