<?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: New field that concatenate values from different rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431604#M106786</link>
    <description>&lt;P&gt;Because of the requirement to put the items in alphabetical order, it takes a little more programming.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=aaa out=wide (keep=CustomerID col: ) ;&lt;/P&gt;
&lt;P&gt;var group;&lt;/P&gt;
&lt;P&gt;by CustomerID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set wide;&lt;/P&gt;
&lt;P&gt;array groups {*} col: ;&lt;/P&gt;
&lt;P&gt;call sortc(of col: ) ;&lt;/P&gt;
&lt;P&gt;length CustomerGroups $ 30;&lt;/P&gt;
&lt;P&gt;do k=1 to dim(groups);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if groups{k} &amp;gt; ' ' then CustomerGroups = catx('_', CustomerGroups, groups{k});&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep CustomerID CustomerGroups;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, so might need a small tweak.&amp;nbsp; But it should contain all the proper steps.&lt;/P&gt;</description>
    <pubDate>Sun, 28 Jan 2018 13:13:32 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-01-28T13:13:32Z</dc:date>
    <item>
      <title>New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431584#M106780</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have to following issue.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have data that includes few rows for each customer.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; aaa;&lt;/P&gt;&lt;P&gt;infile cards;&lt;/P&gt;&lt;P&gt;input CustomerID &amp;nbsp;group &amp;nbsp;&lt;SPAN&gt;$&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 a&lt;/P&gt;&lt;P&gt;1 c&lt;/P&gt;&lt;P&gt;2 b&lt;/P&gt;&lt;P&gt;2 a&lt;/P&gt;&lt;P&gt;2 c&lt;/P&gt;&lt;P&gt;3 a&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new data set that contain 1 row for each customer with a calculated string field called: CustomerGroups&amp;nbsp; that concatenate all groups that a customer belong to&lt;/P&gt;&lt;P&gt;CustomerID&amp;nbsp;&amp;nbsp; CustomerGroups&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a_c&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a_b_c&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Please see that the concatenate of the groups should be from low to high.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For example customer 2 belongs to groups a&amp;nbsp; b c so the new field should be a_b_c&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 09:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431584#M106780</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-01-28T09:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431592#M106781</link>
      <description>&lt;P&gt;like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aaa;
infile cards;
input CustomerID  group  $;
cards;
1 a
1 c
2 b
2 a
2 c
3 a
;
run;

data want(keep=CustomerID CustomerGroups);
   set aaa;
   by CustomerID;
   if first.CustomerID then CustomerGroups=group;
   else CustomerGroups=catx('_', CustomerGroups, group);
   if last.CustomerID then output;
   retain CustomerGroups;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2018 10:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431592#M106781</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-01-28T10:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431593#M106782</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Look at proc transpose.&amp;nbsp; You could also loop over the data in a data step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aaa;
infile cards;
input CustomerID  group  $;
cards;
1 a
1 c
2 b
2 a
2 c
3 a
;
run;

proc sort data=aaa;
	by CustomerID;
run;

proc transpose data=aaa out=aaatp;
	by CustomerID;
	var group;
run;

data aaa_concat;
	set aaatp;
	array col col1-col3;
	attrib CustomrGroups length=$6;
	CustomerGroups = catx(',', of col[*]);
	drop _: col:;	
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;Cameron.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 10:13:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431593#M106782</guid>
      <dc:creator>foobarbaz</dc:creator>
      <dc:date>2018-01-28T10:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431604#M106786</link>
      <description>&lt;P&gt;Because of the requirement to put the items in alphabetical order, it takes a little more programming.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=aaa out=wide (keep=CustomerID col: ) ;&lt;/P&gt;
&lt;P&gt;var group;&lt;/P&gt;
&lt;P&gt;by CustomerID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set wide;&lt;/P&gt;
&lt;P&gt;array groups {*} col: ;&lt;/P&gt;
&lt;P&gt;call sortc(of col: ) ;&lt;/P&gt;
&lt;P&gt;length CustomerGroups $ 30;&lt;/P&gt;
&lt;P&gt;do k=1 to dim(groups);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if groups{k} &amp;gt; ' ' then CustomerGroups = catx('_', CustomerGroups, groups{k});&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep CustomerID CustomerGroups;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, so might need a small tweak.&amp;nbsp; But it should contain all the proper steps.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 13:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431604#M106786</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-28T13:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431612#M106791</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array temp(100)  $20   ;
call missing(of temp(*));
do _n_=1 by 1 until( last.CustomerID);
set aaa;
by 	CustomerID ;
temp(_n_)= Group;
if last.CustomerID then do;call sortc(of temp(*));
customergroups=trim(catx('_', of temp:)) ;
end;
end;
drop temp:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2018 15:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431612#M106791</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-28T15:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: New field that concatenate values from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431668#M106821</link>
      <description>&lt;P&gt;Until SAS implements an aggregate function to do this in SAS/SQL (hint, hint &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;) I always do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=aaa; by customerId group; run;

data want;
length customerGroups $24;
do until(last.customerid);
    set aaa; by customerid;
    customerGroups = catx("_", customerGroups,  group);
    end;
drop group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2018 04:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-field-that-concatenate-values-from-different-rows/m-p/431668#M106821</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-29T04:49:46Z</dc:date>
    </item>
  </channel>
</rss>

