<?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: Transforming data: 1 row to many in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396623#M95787</link>
    <description>&lt;P&gt;This doesn't add a hell of a lot of value that I can see.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wouldn't a long format make more sense?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could get do a double transpose to get it done dynamically, I think you'd have to add some sort of indicator variable though.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 17 Sep 2017 02:39:32 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-09-17T02:39:32Z</dc:date>
    <item>
      <title>Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396608#M95778</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;I have reimbusement data in which each person file how much they spend on Hotel, Meal, and Airfare for each travel (1, 2, 3,4...).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to each payment is record in a seperate row.&lt;/P&gt;
&lt;P&gt;Say for a travel (number 2 in the sample), the original is 500, 100, 300.&lt;/P&gt;
&lt;P&gt;I want to tranform to 3 row&lt;/P&gt;
&lt;P&gt;500, 0,0&lt;/P&gt;
&lt;P&gt;0,100,0&lt;/P&gt;
&lt;P&gt;0,0,300&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure how to get it done.&lt;/P&gt;
&lt;P&gt;Any help is very much appreciated.&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input travel hotel meal airfare ;
datalines;
1 0 20 0
2 500 100 300
3 200 100 0
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;data want:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="4" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl76" style="height: 15.0pt; width: 48pt;"&gt;day&lt;/TD&gt;
&lt;TD width="64" class="xl77" style="width: 48pt;"&gt;Hotel&lt;/TD&gt;
&lt;TD width="64" class="xl77" style="width: 48pt;"&gt;Meal&lt;/TD&gt;
&lt;TD width="64" class="xl78" style="width: 48pt;"&gt;Airfare&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl76" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD align="right" class="xl77" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl77" style="border-top: none;"&gt;20&lt;/TD&gt;
&lt;TD align="right" class="xl78" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD align="right" class="xl64" style="border-top: none;"&gt;&lt;STRONG&gt;500&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl66" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl67" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl68"&gt;&lt;STRONG&gt;100&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD align="right" class="xl69"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl70" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right" class="xl71"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl71"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl72"&gt;&lt;STRONG&gt;300&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD align="right" class="xl73" style="border-top: none;"&gt;&lt;FONT color="#FF0000"&gt;200&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl66" style="border-top: none;"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl70" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right" class="xl71"&gt;0&lt;/TD&gt;
&lt;TD align="right" class="xl74"&gt;&lt;FONT color="#FF0000"&gt;100&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" class="xl75"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 17 Sep 2017 00:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396608#M95778</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-09-17T00:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396611#M95780</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below doesn't give you exactly what you are asking for but it transposes the data into a narrow structure the way that's normally done.&lt;/P&gt;
&lt;P&gt;Such a structure is then also what you need for many of the SAS Proc's.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input travel hotel meal airfare ;
  datalines;
1 0 20 0
2 500 100 300
3 200 100 0
;
run;

data want(keep=travel type amount);
  set have;
  array atype {*} hotel meal airfare;
  do i=1 to dim(atype);
    if atype[i]&amp;gt;0 then
      do;
        type=vname(atype[i]);
        amount=atype[i];
        output;
      end;
  end;
run;

options missing='0';
proc tabulate data=want;
  class travel type;
  var amount;
  table travel, type=' '*amount=' '*sum=' ';
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Sep 2017 01:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396611#M95780</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-17T01:37:36Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396616#M95783</link>
      <description>&lt;P&gt;Mine best is below.&lt;/P&gt;
&lt;P&gt;Is there anyway to combine them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input travel hotel meal airfare ;
datalines;
1 . 20 .
2 500 100 300
3 200 100 .
;run;


data want;
set have nobs=totalobs;
i+1;
set have point=i;
array _col{*} hotel meal airfare;
	do k=1 to dim(_col);
	if _col{k}^=. then do; 
		output;
	end;
end;
run;

data want; set want;
drop k kk;
array _col{*} hotel meal airfare;
	do kk=1 to dim(_col);
	if kk^=k then do;
		_col(kk)=0;
		end;
	end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Sep 2017 01:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396616#M95783</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-09-17T01:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396618#M95784</link>
      <description>&lt;P&gt;If you want to produce the desired result from raw data, you can read lines one variable at a time, using the trailing @&amp;nbsp; sign:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=i);
  retain travel hotel meal airfare 0;

  input travel @;

  array x {*} hotel -- airfare;

  do i=1 to dim(x);
    input x{I} @;
    if x{I}^=0 then do; output; x{I}=0; end;
  end;
datalines;
1 0 20 0
2 500 100 300
3 200 100 0
;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you want to produce WANT from HAVE, you can emulate the above by having multiple SET statements (instead of multiple INPUT statements).&amp;nbsp; Each SET produces an independent stream of data.&amp;nbsp; And because each keeps only one variable at a time you can test for non-zero values, then output (and reset to 0) when appropriate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

  set have (keep=travel hotel);
  retain meal airfare 0;
  if hotel^=0 then do; output; hotel=0;end;

  set have (keep=meal);
  if meal ^=0 then do; output; meal=0;end;

  set have (keep=airfare);
  if airfare ^=0 then do; output; airfare=0;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The earlier version, from raw data to WANT,&amp;nbsp;is easier to scale to a large number of variables.&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2017 02:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396618#M95784</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-17T02:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396623#M95787</link>
      <description>&lt;P&gt;This doesn't add a hell of a lot of value that I can see.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wouldn't a long format make more sense?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could get do a double transpose to get it done dynamically, I think you'd have to add some sort of indicator variable though.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2017 02:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396623#M95787</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-17T02:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396625#M95788</link>
      <description>&lt;P&gt;This is untested but looks promising:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;do hotel2 = 0, hotel;&lt;/P&gt;
&lt;P&gt;do meal2 = 0, meal;&lt;/P&gt;
&lt;P&gt;do airfare2 = 0, airfare;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;n_zeros=(hotel2=0 + meal2=0 + airfare2=0);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if n_zeros=2 then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop hotel meal airfare;&lt;/P&gt;
&lt;P&gt;rename hotel2=hotel meal2=meal airfare2=airfare;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2017 02:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396625#M95788</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-17T02:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396633#M95793</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Not sure where a data structure like you ask for would be useful. It feels like you're after such a structure to solve another problem. Experience tells me: If you need such an uncommon data structure as input to a downstream process then may-be this downstream process needs some re-thinking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway: Below code should give you what you have been asking for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input travel hotel meal airfare ;
datalines;
1 . 20 .
2 500 100 300
3 200 100 .
;
run;

data want(drop=_:);
  merge have have(rename=(hotel=_hotel meal=_meal airfare=_airfare));
  array amt_in  {*} _hotel _meal _airfare;
  array amt_out {*} hotel meal airfare;

  do _i=1 to dim(amt_out);
    amt_out[_i]=0;
  end;

  do _i=1 to dim(amt_in);
    if amt_in[_i]&amp;gt;0 then 
      do;
        amt_out[_i]=amt_in[_i];
        output;
        amt_out[_i]=0;
      end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Sep 2017 04:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396633#M95793</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-17T04:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming data: 1 row to many</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396659#M95801</link>
      <description>&lt;P&gt;Thanks a lot, members.&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2017 16:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transforming-data-1-row-to-many/m-p/396659#M95801</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-09-17T16:06:57Z</dc:date>
    </item>
  </channel>
</rss>

