<?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 Code to move values from one column to 3 category columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738330#M230290</link>
    <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to move the values from the column "lot" to 3 columns A, B and C, according to respective category (A, B or C)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="The second table is my goal" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58925i6F6BA7A3C9077639/image-size/large?v=v2&amp;amp;px=999" role="button" title="Ask sas.png" alt="The second table is my goal" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;The second table is my goal&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can see the image below with 2 tables.&amp;nbsp;&amp;nbsp;The second table is what I am trying to reach out, because I would like to move variable from column lot to category A, B or C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;</description>
    <pubDate>Sat, 01 May 2021 13:46:40 GMT</pubDate>
    <dc:creator>Moraes86</dc:creator>
    <dc:date>2021-05-01T13:46:40Z</dc:date>
    <item>
      <title>Code to move values from one column to 3 category columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738330#M230290</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to move the values from the column "lot" to 3 columns A, B and C, according to respective category (A, B or C)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="The second table is my goal" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58925i6F6BA7A3C9077639/image-size/large?v=v2&amp;amp;px=999" role="button" title="Ask sas.png" alt="The second table is my goal" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;The second table is my goal&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can see the image below with 2 tables.&amp;nbsp;&amp;nbsp;The second table is what I am trying to reach out, because I would like to move variable from column lot to category A, B or C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Sat, 01 May 2021 13:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738330#M230290</guid>
      <dc:creator>Moraes86</dc:creator>
      <dc:date>2021-05-01T13:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Code to move values from one column to 3 category columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738331#M230291</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Very strange transpose, but OK ;-).&lt;/P&gt;
&lt;P&gt;Please always provide your 'HAVE' data as a datastep with cards (datalines).&lt;/P&gt;
&lt;P&gt;You get much more responses then!!&lt;/P&gt;
&lt;P&gt;Code below works but better, more performant and more elegant code, is certainly possible.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 Length Head $ 20 Lot $ 10;
 infile cards delimiter='|';
 input Head $ Lot $ A B C;
 total=SUM(A,B,C);
 cards;
S001, S012, S013 |R8091120 |. |. |2
S001, S012, S013 |Y0661220 |. |. |2
S003             |P8690321 |1 |. |.
S004             |P3030321 |2 |. |.
S004             |P3440221 |2 |. |.
;
run;

data want(drop=Aold Bold Cold);
 set have(rename=(A=Aold B=Bold C=Cold));
 if      Aold ne . then A=Lot;
 else if Bold ne . then B=Lot;
 else if Cold ne . then C=Lot;
run;

data want(drop=Aold Bold Cold Lot lagHead);
 LENGTH Head $ 20 A $ 50 B $ 50 C $ 50;
 set want(rename=(A=Aold B=Bold C=Cold));
 retain A B C;
 lagHead=lag(Head);
 by Head;
 if first.Head then do; A=Aold; B=Bold; C=Cold; end;
 if NOT first.Head AND Head=lagHead then do; 
  A=strip(A)||','||strip(Aold); if A=',' then A='';
  B=strip(B)||','||strip(Bold); if B=',' then B='';
  C=strip(C)||','||strip(Cold); if C=',' then C='';
 end;
 if last.Head then output;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 01 May 2021 14:36:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738331#M230291</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-01T14:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Code to move values from one column to 3 category columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738335#M230294</link>
      <description>&lt;P&gt;It is not clear what TOTAL means in your example output.&amp;nbsp; If I take the MAX over all A,B,C for all observations for HEAD I get the same number you reported for that sample data.&lt;/P&gt;
&lt;P&gt;You need to make NEW variables since the old A,B,C are numeric and the new A,B,C are character.&amp;nbsp; Make them long enough to hold all possible lists.&lt;/P&gt;
&lt;P&gt;So if the data is sorted by HEAD it is pretty straight forward.&amp;nbsp; For only three variables arrays might be overkill but here is an example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   do until (last.head);
     set have ;
     by head ;
     array _num a b c ;
     array _ch $100 _a _b _c ;
     do i=1 to dim(_num);
       if not missing(_num[i]) then do;
         _ch[i]=catx(', ',_ch[i],lot);
         total = max(total,_num[i]);
       end;
     end;
   end;
   drop i a b c lot ;
   rename _a=A _b=B _c=C ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    Head                A                     B            C             total

 1     S001, S012, S013                               R8091120, Y0661220      2
 2     S003                P8690321                                           1
 3     S004                P3030321, P3440221                                 2

&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 May 2021 16:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738335#M230294</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-01T16:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Code to move values from one column to 3 category columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738966#M230579</link>
      <description>&lt;P&gt;Thank you so much for this code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;It worked and it will help me a lot.&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738966#M230579</guid>
      <dc:creator>Moraes86</dc:creator>
      <dc:date>2021-05-04T15:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Code to move values from one column to 3 category columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738969#M230581</link>
      <description>&lt;P&gt;Thanks you soo much for this code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-to-move-values-from-one-column-to-3-category-columns/m-p/738969#M230581</guid>
      <dc:creator>Moraes86</dc:creator>
      <dc:date>2021-05-04T15:41:25Z</dc:date>
    </item>
  </channel>
</rss>

