<?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: Concatenate values in N columns but create a new column with unique values in previous fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637357#M189458</link>
    <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #999999;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/38479" target="_blank"&gt;AviS&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;Here is a possible solution to your problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
   length ID $2 Y2017-Y2019 $10;
   input ID 1-2 Y2017 4-10 Y2018 13-18 Y2019 20-25;
   datalines;
a	a2, a3	a2,a3	 a4
b	a2	 	          a4
c	a1, a2	a2,a3	 a4,a3
;

data B (keep=ID VAL);
   set A;
   length ALL $200 VAL $10;
   ALL = catx(',',Y2017,Y2018,Y2019);
   do i=1 to countw(ALL,',');
      VAL = strip(scan(ALL,i,','));
      output;
   end;
run;

proc sort data=B out=C nodupkey;
   by ID VAL;
run;

data D (drop=VAL);
   merge A C;
   by ID;
   length FINAL $200;
   retain FINAL;
   if first.ID then FINAL = '';
   FINAL = catx(', ', FINAL,VAL);
   if last.ID then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this code to make sure it works for you.&lt;/P&gt;
&lt;P&gt;Is that what you want?&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
    <pubDate>Fri, 03 Apr 2020 20:52:03 GMT</pubDate>
    <dc:creator>LeonidBatkhan</dc:creator>
    <dc:date>2020-04-03T20:52:03Z</dc:date>
    <item>
      <title>Concatenate values in N columns but create a new column with unique values in previous fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637343#M189452</link>
      <description>&lt;P&gt;I have a SAS dataset with 3 columns that could have one or more values separated by comma. I would have to add a new column with distinct values in 3 columns separated by comma in a new field "Final.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Y2017&lt;/TD&gt;&lt;TD&gt;Y2018&lt;/TD&gt;&lt;TD&gt;Y2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;a2, a3&lt;/TD&gt;&lt;TD&gt;a2,a3&lt;/TD&gt;&lt;TD&gt;a4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;a4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;a1, a2&lt;/TD&gt;&lt;TD&gt;a2,a3&lt;/TD&gt;&lt;TD&gt;a4,a3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Y2017&lt;/TD&gt;&lt;TD&gt;Y2018&lt;/TD&gt;&lt;TD&gt;Y2019&lt;/TD&gt;&lt;TD&gt;Final&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;a2, a3&lt;/TD&gt;&lt;TD&gt;a2,a3&lt;/TD&gt;&lt;TD&gt;a4&lt;/TD&gt;&lt;TD&gt;a2, a3, a4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;a4&lt;/TD&gt;&lt;TD&gt;a2, a4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;a1, a2&lt;/TD&gt;&lt;TD&gt;a2,a3&lt;/TD&gt;&lt;TD&gt;a4,a3&lt;/TD&gt;&lt;TD&gt;a1, a2, a3, a4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637343#M189452</guid>
      <dc:creator>AviS</dc:creator>
      <dc:date>2020-04-03T20:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate values in N columns but create a new column with unique values in previous fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637350#M189455</link>
      <description>&lt;P&gt;Did you do something to force the multiple values per cell?&lt;/P&gt;
&lt;P&gt;If so you may find it easier to back before that step was done and start over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, is the final order at all important? Will you be doing something where you need to treat a1,a2,a3 the same as a3,a1,a2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637350#M189455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-03T20:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate values in N columns but create a new column with unique values in previous fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637356#M189457</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input (ID	Y2017	Y2018	Y2019) (:$10.);
cards;
a	a2,a3	a2,a3	a4
b	a2	 .	a4 .
c	a1,a2	a2,a3	a4,a3
;

data want;
 set have;
 length t1 t2 final $1000;
 array y Y2017	Y2018	Y2019;
 t1=catx(',',of y(*));
 do _n_=1 to countw(t1,', ');
  t2=scan(t1,_n_,',');
  if not index(final,strip(t2)) then final=catx(',',final,t2);
 end;
 drop t:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637356#M189457</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-03T20:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate values in N columns but create a new column with unique values in previous fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637357#M189458</link>
      <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #999999;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/38479" target="_blank"&gt;AviS&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;Here is a possible solution to your problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
   length ID $2 Y2017-Y2019 $10;
   input ID 1-2 Y2017 4-10 Y2018 13-18 Y2019 20-25;
   datalines;
a	a2, a3	a2,a3	 a4
b	a2	 	          a4
c	a1, a2	a2,a3	 a4,a3
;

data B (keep=ID VAL);
   set A;
   length ALL $200 VAL $10;
   ALL = catx(',',Y2017,Y2018,Y2019);
   do i=1 to countw(ALL,',');
      VAL = strip(scan(ALL,i,','));
      output;
   end;
run;

proc sort data=B out=C nodupkey;
   by ID VAL;
run;

data D (drop=VAL);
   merge A C;
   by ID;
   length FINAL $200;
   retain FINAL;
   if first.ID then FINAL = '';
   FINAL = catx(', ', FINAL,VAL);
   if last.ID then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this code to make sure it works for you.&lt;/P&gt;
&lt;P&gt;Is that what you want?&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-values-in-N-columns-but-create-a-new-column-with/m-p/637357#M189458</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-04-03T20:52:03Z</dc:date>
    </item>
  </channel>
</rss>

