<?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: Columns Transposition in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352410#M82138</link>
    <description>Wow Ksharp,&lt;BR /&gt;&lt;BR /&gt;Thank you so much for your help! It works perfecty &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
    <pubDate>Sat, 22 Apr 2017 09:46:13 GMT</pubDate>
    <dc:creator>Question</dc:creator>
    <dc:date>2017-04-22T09:46:13Z</dc:date>
    <item>
      <title>Columns Transposition in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352096#M82038</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope you are well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a quick question on how to program this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to have one row per customer, and have in columns, for example what was the value &lt;SPAN&gt;of the variable Number&amp;nbsp;before the fisrt gap,&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;during the first gap , after the first gap, before the second gap, during the second gap, after the second gap etc..&amp;nbsp;Basically I am trying to find out how big or small is this number when they transact and when they don't and does&amp;nbsp;the value of the Number brings them back to transact etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope it makes sense, if not please do not hesitate to shout.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;&lt;FONT color="#000000"&gt;Have&lt;BR /&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="715"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;customer_id&lt;/TD&gt;
&lt;TD width="93"&gt;trans_date&lt;/TD&gt;
&lt;TD width="112"&gt;week_start_date&lt;/TD&gt;
&lt;TD width="108"&gt;week_end_date&lt;/TD&gt;
&lt;TD width="99"&gt;WEEK_NO&lt;/TD&gt;
&lt;TD width="79"&gt;no_trans&lt;/TD&gt;
&lt;TD width="140"&gt;Number&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50026&lt;/TD&gt;
&lt;TD&gt;03-Jan-17&lt;/TD&gt;
&lt;TD&gt;01-Jan-17&lt;/TD&gt;
&lt;TD&gt;07-Jan-17&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;30,000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50026&lt;/TD&gt;
&lt;TD&gt;03-Jan-17&lt;/TD&gt;
&lt;TD&gt;01-Jan-17&lt;/TD&gt;
&lt;TD&gt;07-Jan-17&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;21,000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50026&lt;/TD&gt;
&lt;TD&gt;04-Jan-17&lt;/TD&gt;
&lt;TD&gt;01-Jan-17&lt;/TD&gt;
&lt;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;TD&gt;14-Jan-17&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;53,000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50026&lt;/TD&gt;
&lt;TD&gt;14-Jan-17&lt;/TD&gt;
&lt;TD&gt;08-Jan-17&lt;/TD&gt;
&lt;TD&gt;14-Jan-17&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;10,400&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;&lt;FONT color="#000000"&gt;Want&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="884"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;customer_id&lt;/TD&gt;
&lt;TD width="114"&gt;Number_&lt;BR /&gt; during_First_Gap&lt;/TD&gt;
&lt;TD width="116"&gt;Number_&lt;BR /&gt; before_First_Gap&lt;/TD&gt;
&lt;TD width="104"&gt;Number_&lt;BR /&gt; after_First_Gap&lt;/TD&gt;
&lt;TD width="135"&gt;Number_&lt;BR /&gt; before_Second_Gap&lt;/TD&gt;
&lt;TD width="133"&gt;Number_&lt;BR /&gt; during_Second_Gap&lt;/TD&gt;
&lt;TD width="123"&gt;Number_&lt;BR /&gt; after_Second_Gap&lt;/TD&gt;
&lt;TD width="75"&gt;Gap 3 etc…&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;50026&lt;/TD&gt;
&lt;TD&gt;30,000&lt;/TD&gt;
&lt;TD&gt;21,000 &amp;amp; 2000&lt;/TD&gt;
&lt;TD&gt;31,000&lt;/TD&gt;
&lt;TD&gt;40,000&lt;/TD&gt;
&lt;TD&gt;7,300&lt;/TD&gt;
&lt;TD&gt;53,000&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 10:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352096#M82038</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-04-21T10:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Columns Transposition in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352115#M82046</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First off, post test data in the form of a datastep. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now sedonly I don't think that a transposed format is a good method, yes it may work on this small subset, but if there are thousands of records, The during list willl quickly run out of space, and there will be so many columns it wont be usable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The format you have should be fine to work with, what is the problem? &amp;nbsp;Just assign a code column, before, during, after, then you can proc freq/means or other calculation based on the flag. (can't post example as copy paste what you have provided is Excel or something - hence why post datastep).&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 11:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352115#M82046</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-21T11:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Columns Transposition in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352137#M82049</link>
      <description>&lt;P&gt;Your report can be done in SAS no doubt. Some areas that are unusual in programming are:&amp;nbsp; The number_before_first_gap is a variable length field depending on the number of times a gap occurs.&amp;nbsp; Then the whole record is variable depending on the number of gaps per customer.&lt;/P&gt;
&lt;P&gt;Some code that does not address these areas would be somthing like this;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort; by customer week;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data;&amp;nbsp;&amp;nbsp; set;&amp;nbsp;&amp;nbsp;&amp;nbsp; by customer&amp;nbsp; week;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain hold1 hold2 hold3&amp;nbsp; seq;&lt;/P&gt;
&lt;P&gt;gap=no_trans=.;&lt;/P&gt;
&lt;P&gt;If first.week then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; hold1=number;&amp;nbsp; hold2=0; hold3=0;&amp;nbsp; seq=1;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if gap and seq=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; during=hold1;&amp;nbsp;&amp;nbsp; seq+1;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if gap and seq gt 1&amp;nbsp; then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if seq=2 then hold2=number;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if seq=3 then hold3=number;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if not gap and seq gt 1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; before= (&lt;EM&gt;string of hold2 and hold3&lt;/EM&gt;);&amp;nbsp; after=number;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 12:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352137#M82049</guid>
      <dc:creator>Jim_G</dc:creator>
      <dc:date>2017-04-21T12:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: Columns Transposition in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352405#M82134</link>
      <description>&lt;P&gt;Really not easy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover expandtabs;
input (customer_id	trans_date	week_start_date	week_end_date) (:$20.)	WEEK_NO	no_trans	Number : comma32.;
cards;
50026	03-Jan-17	01-Jan-17	07-Jan-17	1	1	30,000
50026	03-Jan-17	01-Jan-17	07-Jan-17	1	.	21,000
50026	04-Jan-17	01-Jan-17	07-Jan-17	1	.	2,000
50026	06-Jan-17	01-Jan-17	07-Jan-17	1	1	31,000
50026	07-Jan-17	01-Jan-17	07-Jan-17	1	2	5,500
50026	10-Jan-17	08-Jan-17	14-Jan-17	2	2	40,000
50026	11-Jan-17	08-Jan-17	14-Jan-17	2	.	7,300
50026	13-Jan-17	08-Jan-17	14-Jan-17	2	1	53,000
50026	14-Jan-17	08-Jan-17	14-Jan-17	2	.	10,400
50027	03-Jan-17	01-Jan-17	07-Jan-17	1	.	21,000
50027	04-Jan-17	01-Jan-17	07-Jan-17	1	.	2,000
50027	06-Jan-17	01-Jan-17	07-Jan-17	1	1	31,000
;
run;
data temp1;
 set have;
 by customer_id no_trans notsorted;
 group+first.no_trans;
 if missing(no_trans);
run;
data temp2;
 set temp1;
 by customer_id group;
 if first.customer_id then n=0;
 n+first.group;
run;
data temp3;
length during $ 200;
 do until(last.n);
  set temp2;
  by customer_id n;
  during=catx('&amp;amp;',during,number);
 end;
keep during customer_id n;
run;


data temp;
 merge have have(firstobs=2 keep=customer_id number no_trans
 rename=(customer_id=_id number=_number no_trans=_trans));
 length name $ 20;
 if customer_id=_id and not missing(no_trans) and missing(_trans) then do;
  before=number;name='before';
 end;
 if customer_id=_id and missing(no_trans) and not missing(_trans) then do;
  after=_number;name='after';
 end;
 value=coalesce(before,after);
 if not missing(name);
 keep customer_id value name;
run;

data temp;
 set temp;
 by customer_id name notsorted;
 if first.customer_id then n=0;
 if name='before' then n+1;
run;
proc transpose data=temp out=temp33(keep=customer_id before after);
by customer_id n;
var value;
id name;
run;
data temp33;
 set temp33;
 by customer_id;
 if first.customer_id then n=0;
 n+1;
run;
data want;
 merge temp3 temp33;
 by customer_id n;
run;
proc sql noprint;
select max(n) into : n from want;
quit;
proc summary data=want ;
by customer_id;
output out=final_want idgroup(out[&amp;amp;n] (before during after)=);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Apr 2017 07:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352405#M82134</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-22T07:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: Columns Transposition in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352410#M82138</link>
      <description>Wow Ksharp,&lt;BR /&gt;&lt;BR /&gt;Thank you so much for your help! It works perfecty &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Sat, 22 Apr 2017 09:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-Transposition-in-SAS/m-p/352410#M82138</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-04-22T09:46:13Z</dc:date>
    </item>
  </channel>
</rss>

