<?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: Concat the values in columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828499#M327265</link>
    <description>Thank you, Peter !&lt;BR /&gt;It works !</description>
    <pubDate>Fri, 12 Aug 2022 18:49:09 GMT</pubDate>
    <dc:creator>SASdevAnneMarie</dc:creator>
    <dc:date>2022-08-12T18:49:09Z</dc:date>
    <item>
      <title>Concat the values in columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828242#M327166</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to rearrange the columns in this way : I have the columns produit, nbr_code, lib_produit and I would like to create&amp;nbsp; the column code_2 :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="366"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="60"&gt;produit&lt;/TD&gt;
&lt;TD width="65"&gt;nbr_code&lt;/TD&gt;
&lt;TD width="84"&gt;lib_produit&lt;/TD&gt;
&lt;TD width="157"&gt;code_2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A270&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1819&lt;/TD&gt;
&lt;TD&gt;A270|A270S&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A270S&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1819&lt;/TD&gt;
&lt;TD&gt;A270|A270S&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;X256&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;PARTENAIRE&lt;/TD&gt;
&lt;TD&gt;X256&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;K103&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;AEDE&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;K103|M103S|L279|O479&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;M103S&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;AEDE&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;K103|M103S|L279|O479&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;L279&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;AEDE&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;K103|M103S|L279|O479&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;O479&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;AEDE&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;K103|M103S|L279|O479&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code is :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
	set test;
	by lib_produit;
	length code_prod $200;
	retain code_prod;


			if produit='' then
				do;
					code_prod=' ';
					output;
				end;
			else
				do;
					if last.lib_produit then
		            do;
					code_2=catx('',code_2,produit);
					output;
				end;
				end;
	
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I didn't obtain the desired result.&lt;/P&gt;
&lt;P&gt;Thank you for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 10:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828242#M327166</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-08-11T10:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Concat the values in columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828243#M327167</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input produit $ nbr_code lib_produit :$20.;
datalines;
A270  2 1819       
A270S 2 1819       
X256  1 PARTENAIRE 
K103  4 AEDE       
M103S 4 AEDE       
L279  4 AEDE       
O479  4 AEDE       
;

data want;
   do until (last.lib_produit);
      set have;
      by lib_produit notsorted;
      length code_2 $200;
      code_2 = catx('|', code_2, produit);
   end;

   do until (last.lib_produit);
      set have;
      by lib_produit notsorted;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;produit  nbr_code  lib_produit  code_2
A270     2         1819         A270|A270S 
A270S    2         1819         A270|A270S 
X256     1         PARTENAIRE   X256 
K103     4         AEDE         K103|M103S|L279|O479 
M103S    4         AEDE         K103|M103S|L279|O479 
L279     4         AEDE         K103|M103S|L279|O479 
O479     4         AEDE         K103|M103S|L279|O479 &lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Aug 2022 10:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828243#M327167</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-11T10:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Concat the values in columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828289#M327177</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For one thing your code reuses a variable Code_2 that does not have a defined length, would get the length set from the first value assigned and then you are attempting to stuff 3 (or more) values into it. So very likely the length of the first use of Code_2 is not long enough to hold the additional text.&lt;/P&gt;
&lt;P&gt;I think you meant&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;code_2=catx('',code_2,produit);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;to be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;code_prod=catx('|',code_prod,produit);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;or set the length for code_2 instead of code_prod.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also need to reset Code_prod (or Code_2) at the First.lib_produit to the first produit. Otherwise you are retaining and accumulating from the previous lib_produit group of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second the data step processes things sequentially (in general) Since you are combining multiple observations of data once you build the total combined version you would have to merge that result back to the source data. Which is going to be fun as you don't show anything that is going to work real well for matching to the original data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Didn't obtain the desired result" is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;/&amp;gt;" to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;/&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 15:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828289#M327177</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-11T15:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Concat the values in columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828499#M327265</link>
      <description>Thank you, Peter !&lt;BR /&gt;It works !</description>
      <pubDate>Fri, 12 Aug 2022 18:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/828499#M327265</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-08-12T18:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Concat the values in columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/832086#M328876</link>
      <description>Hi Peter,&lt;BR /&gt;Could you explain me please this part of the code :&lt;BR /&gt;  do until (last.lib_produit);&lt;BR /&gt;      set have;&lt;BR /&gt;      by lib_produit notsorted;&lt;BR /&gt;      output;&lt;BR /&gt;   end;&lt;BR /&gt;Thank you !</description>
      <pubDate>Wed, 07 Sep 2022 09:02:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concat-the-values-in-columns/m-p/832086#M328876</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-09-07T09:02:50Z</dc:date>
    </item>
  </channel>
</rss>

