<?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 Combining and extracting rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637164#M189405</link>
    <description>&lt;P&gt;Apologies for poor wording of my question but I would like to get some help, please.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large data but simplified like this below. Days represents the days from the first visit to the doctor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means ID Number 100 received drug A and B on the 5th day, and drug A on the 7th day.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But currently, ID Number 100 has two rows for the 5th day.&amp;nbsp; What I would exactly likely to get is that if the ID gets a multiple medications on the same day, I want them to be combined into a single column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;ORIGINAL&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; Drug Days&lt;/P&gt;
&lt;P&gt;100 A 5&lt;/P&gt;
&lt;P&gt;100 B 5&lt;/P&gt;
&lt;P&gt;100 A 7&lt;/P&gt;
&lt;P&gt;101 C 3&lt;/P&gt;
&lt;P&gt;101 A 4&lt;/P&gt;
&lt;P&gt;102 D 2&lt;/P&gt;
&lt;P&gt;102 A 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;NEW&amp;gt;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; Drug Days&lt;/P&gt;
&lt;P&gt;100 A,B 5&lt;/P&gt;
&lt;P&gt;100 A 7&lt;/P&gt;
&lt;P&gt;101 C 3&lt;/P&gt;
&lt;P&gt;101 A 4&lt;/P&gt;
&lt;P&gt;102 D,A 2&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 06:13:28 GMT</pubDate>
    <dc:creator>SAS_AMUH</dc:creator>
    <dc:date>2020-04-03T06:13:28Z</dc:date>
    <item>
      <title>How to combine rows with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/636775#M189226</link>
      <description>&lt;P&gt;I need help with combining data into one row.&lt;/P&gt;
&lt;P&gt;I would like the &amp;lt;ORIGINAL&amp;gt; data to be displayed as &amp;lt;NEW&amp;gt;.&lt;/P&gt;
&lt;P&gt;I believe that if I TRANSPOSE , it will result into multiple column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like &amp;lt;NEW&amp;gt; I would like multiple products to be displayed in a single column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help in advance.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;ORIGINAL&amp;gt;&lt;/P&gt;
&lt;P&gt;ID Product Days&lt;/P&gt;
&lt;P&gt;100 A 1&lt;BR /&gt;101 B 2&lt;BR /&gt;102 A 1&lt;BR /&gt;102 B 1&lt;BR /&gt;102 C 1&lt;BR /&gt;103 A 5&lt;BR /&gt;103 A 5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;NEW&amp;gt;&lt;/P&gt;
&lt;P&gt;ID Product Days&lt;/P&gt;
&lt;P&gt;100 A 1&lt;BR /&gt;101 B 2&lt;BR /&gt;102 ABC 1&lt;BR /&gt;103 AA 5&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Apr 2020 06:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/636775#M189226</guid>
      <dc:creator>SAS_AMUH</dc:creator>
      <dc:date>2020-04-02T06:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine rows with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/636779#M189229</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Product $ Days;
datalines;
100 A 1
101 B 2
102 A 1
102 B 1
102 C 1
103 A 5
103 A 5
;

data want(drop=_:);
   set have (rename=Product=_Product);
   by ID;
   length Product $ 200;
   if first.ID then call missing(Product);
   Product = cats (Product, _Product);
   if last.ID;
   retain Product;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Apr 2020 07:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/636779#M189229</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-02T07:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine rows with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637125#M189394</link>
      <description>&lt;P&gt;@&lt;SPAN class="lia-user-login"&gt;draycut&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Thank you so much for your reply.&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Your answer was really helpful for me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, I'm sorry I didn't write my question and example clearly.&lt;/P&gt;
&lt;P&gt;What I exactly needed is like this below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The current coding resulted in one ID and one observation, but each ID will have multiple days.&lt;/P&gt;
&lt;P&gt;I would be grateful if you could show me, how to code this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;ORIGINAL_2&amp;gt;&lt;/P&gt;
&lt;P&gt;ID Product Days&lt;/P&gt;
&lt;P&gt;100 A 1&lt;/P&gt;
&lt;P&gt;100 A 2&lt;/P&gt;
&lt;P&gt;100 B 2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;100 C 2&lt;/P&gt;
&lt;P&gt;101 A 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;101 A 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;NEW_2&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Product Days&lt;/P&gt;
&lt;P&gt;100 A 1&lt;/P&gt;
&lt;P&gt;100 ABC 2&lt;/P&gt;
&lt;P&gt;101 A 1&lt;/P&gt;
&lt;P&gt;101 A 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 01:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637125#M189394</guid>
      <dc:creator>SAS_AMUH</dc:creator>
      <dc:date>2020-04-03T01:50:33Z</dc:date>
    </item>
    <item>
      <title>Combining and extracting rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637164#M189405</link>
      <description>&lt;P&gt;Apologies for poor wording of my question but I would like to get some help, please.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large data but simplified like this below. Days represents the days from the first visit to the doctor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means ID Number 100 received drug A and B on the 5th day, and drug A on the 7th day.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But currently, ID Number 100 has two rows for the 5th day.&amp;nbsp; What I would exactly likely to get is that if the ID gets a multiple medications on the same day, I want them to be combined into a single column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;ORIGINAL&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; Drug Days&lt;/P&gt;
&lt;P&gt;100 A 5&lt;/P&gt;
&lt;P&gt;100 B 5&lt;/P&gt;
&lt;P&gt;100 A 7&lt;/P&gt;
&lt;P&gt;101 C 3&lt;/P&gt;
&lt;P&gt;101 A 4&lt;/P&gt;
&lt;P&gt;102 D 2&lt;/P&gt;
&lt;P&gt;102 A 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;NEW&amp;gt;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; Drug Days&lt;/P&gt;
&lt;P&gt;100 A,B 5&lt;/P&gt;
&lt;P&gt;100 A 7&lt;/P&gt;
&lt;P&gt;101 C 3&lt;/P&gt;
&lt;P&gt;101 A 4&lt;/P&gt;
&lt;P&gt;102 D,A 2&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 06:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637164#M189405</guid>
      <dc:creator>SAS_AMUH</dc:creator>
      <dc:date>2020-04-03T06:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and extracting rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637167#M189406</link>
      <description>&lt;P&gt;If the result you want is not the final report, then i don't think, that the data-format you want is useful for most analysis i can think of.&lt;/P&gt;
&lt;P&gt;Try (untested code):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(rename=(Drug = _Drug));
  by Id Day;
  
  length Drug $ 20; /* the length depends on the number of drugs/day */
  retain Drug;
  
  if first.Day then do;
	Drug = ' ';
  end;
  
  Drug = catx(',', Drug, _Drug);
  
  if last.Day then do;
    output;
  end;

  drop _Drug;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 06:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637167#M189406</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-04-03T06:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and extracting rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637176#M189411</link>
      <description>&lt;P&gt;I appreciate your answer. I will learn about the CATS and&amp;nbsp; CATX functions.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 08:15:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637176#M189411</guid>
      <dc:creator>SAS_AMUH</dc:creator>
      <dc:date>2020-04-03T08:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine rows with a condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637179#M189413</link>
      <description>&lt;P&gt;Please try the below code as well,it is slightly different from the existing posts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Product $ Days;
datalines;
100 A 1
101 B 2
102 A 1
102 B 1
102 C 1
103 A 5
103 A 5
;

data want;
set have;
by id;
retain new;
if first.id then new=product;
else new=catx('',new,product);
if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Apr 2020 08:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-rows-with-a-condition/m-p/637179#M189413</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-03T08:57:58Z</dc:date>
    </item>
  </channel>
</rss>

