<?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 Question on adding values from colums and appending to a row based on a common value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Question-on-adding-values-from-colums-and-appending-to-a-row/m-p/886963#M350470</link>
    <description>&lt;PRE&gt;&lt;CODE&gt;data have;
infile cards dlm=',';
informat product_Id $8. type $8. Description $20.;
input Product_ID    Type      Description;
cards;
12, A, Made of wood
12, B, Made of wood
25, C, Made of steel
15, D, Made of iron
12, F, Made of wood&lt;BR /&gt;28, G, Made of paper&lt;BR /&gt;
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;data want;
  set have;
  by product_id description NOTSORTED;   /* &amp;lt;-------- */

  length types $20.;
  retain types;

  if first.description then call missing(types);
  types = catx(", ", types, type);

  if last.description then do;
    description = catt(description, " (", types, ")");
    output;
  end;
  keep product_id description;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN&gt;I'm expecting output to be like shown below, please help on what am i missing.Please note that the type is added to the description within paranthesis only when description is the same between the products I'm expecting output to be like shown below&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;My output should look like below&lt;BR /&gt;Product_ID description
12         Made of paper(G)
12         Made of Wood(A,B,F)&lt;BR /&gt;15         Made of iron ( D)
25         Made of steel (C)
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 29 Jul 2023 14:07:46 GMT</pubDate>
    <dc:creator>Newbie_23</dc:creator>
    <dc:date>2023-07-29T14:07:46Z</dc:date>
    <item>
      <title>Question on adding values from colums and appending to a row based on a common value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Question-on-adding-values-from-colums-and-appending-to-a-row/m-p/886963#M350470</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;data have;
infile cards dlm=',';
informat product_Id $8. type $8. Description $20.;
input Product_ID    Type      Description;
cards;
12, A, Made of wood
12, B, Made of wood
25, C, Made of steel
15, D, Made of iron
12, F, Made of wood&lt;BR /&gt;28, G, Made of paper&lt;BR /&gt;
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;data want;
  set have;
  by product_id description NOTSORTED;   /* &amp;lt;-------- */

  length types $20.;
  retain types;

  if first.description then call missing(types);
  types = catx(", ", types, type);

  if last.description then do;
    description = catt(description, " (", types, ")");
    output;
  end;
  keep product_id description;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;SPAN&gt;I'm expecting output to be like shown below, please help on what am i missing.Please note that the type is added to the description within paranthesis only when description is the same between the products I'm expecting output to be like shown below&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;My output should look like below&lt;BR /&gt;Product_ID description
12         Made of paper(G)
12         Made of Wood(A,B,F)&lt;BR /&gt;15         Made of iron ( D)
25         Made of steel (C)
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2023 14:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Question-on-adding-values-from-colums-and-appending-to-a-row/m-p/886963#M350470</guid>
      <dc:creator>Newbie_23</dc:creator>
      <dc:date>2023-07-29T14:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: Question on adding values from colums and appending to a row based on a common value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Question-on-adding-values-from-colums-and-appending-to-a-row/m-p/886973#M350476</link>
      <description>&lt;P&gt;First thank you for providing both a working DATA step with sample data, and your program code.&amp;nbsp; I appended a proc print to it, producing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;                                     product_
                              Obs       Id           Description

                               1        12       Made of wood (A, B)
                               2        25       Made of steel (C)
                               3        15       Made of iron (D)
                               4        12       Made of wood (F)
                               5        28       Made of paper (G)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You wanted all the "Made of wood" items together (for a given product_id) , but because they are not consecutive obs in dataset HAVE, your code doesn't group them.&amp;nbsp; This is because the basic behavior of the SET statement in a DATA step is &lt;EM&gt;&lt;STRONG&gt;sequential&lt;/STRONG&gt;&lt;/EM&gt; data processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So change the sequence of data, by means of a PROC SORT.&amp;nbsp; Something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=need;
  by product_id description;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use NEED in your sas code, instead of HAVE.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2023 17:06:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Question-on-adding-values-from-colums-and-appending-to-a-row/m-p/886973#M350476</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-29T17:06:24Z</dc:date>
    </item>
  </channel>
</rss>

