<?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: How do I extract a row from several observations under multiple conditions? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442737#M13659</link>
    <description>&lt;P&gt;Would the result be the same if the last obs was&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5 D12 A12 200111&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And either way, why?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Mar 2018 05:32:55 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-03-06T05:32:55Z</dc:date>
    <item>
      <title>How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442729#M13658</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am managing health insurance data so I got many&amp;nbsp;records by individuals.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like only one observation to remain per person with some conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the conditions below:&lt;/P&gt;&lt;P&gt;1) has a (two kinds of) specific classification number for the first time,&lt;/P&gt;&lt;P&gt;2) otherwise, has the latest record&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, here's the sample of the data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data play;&lt;/P&gt;&lt;P&gt;input id $class1 $class2 yyyymm&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 A12 C33 200105&lt;/P&gt;&lt;P&gt;1 A12 C32 200109&lt;/P&gt;&lt;P&gt;1 A12 C33 200203&lt;/P&gt;&lt;P&gt;1 A13 C34 200302&lt;/P&gt;&lt;P&gt;2&amp;nbsp;F12&amp;nbsp;D33 200105&lt;/P&gt;&lt;P&gt;2 F12 D32 200109&lt;/P&gt;&lt;P&gt;2 F12 D33 200203&lt;/P&gt;&lt;P&gt;3 S23 F32 200103&lt;/P&gt;&lt;P&gt;3 S23 F33 200105&lt;/P&gt;&lt;P&gt;3 A12 F32 200204&lt;/P&gt;&lt;P&gt;3 A12 C33 200205&lt;/P&gt;&lt;P&gt;3 A13 C34 200208&lt;/P&gt;&lt;P&gt;4 Q12 C33 200102&lt;/P&gt;&lt;P&gt;4 Q12 A12 200112&lt;/P&gt;&lt;P&gt;4 G12 A12 200207&lt;/P&gt;&lt;P&gt;4 G13 C32 200309&lt;/P&gt;&lt;P&gt;5 D12 G33 200105&lt;/P&gt;&lt;P&gt;5 D12 A32 200108&lt;/P&gt;&lt;P&gt;5 D12 A33 200111&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With this data, I want a row remained by an individual in which $class1 or $class2 has either "A12" or "A" for the first time. It otherwise has the latest record.&lt;/P&gt;&lt;P&gt;And here's the&amp;nbsp;expected result below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 A12 C33 200105 (&amp;lt;- "A12" contained in $class1)&lt;/P&gt;&lt;P&gt;2 F12 D33 200203 (&amp;lt;- nothing contained, so "the latest record")&lt;/P&gt;&lt;P&gt;3 A12 F32 200204 (&lt;SPAN&gt;&amp;lt;- "A12" contained in $class1)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 Q12 A12 200112 (&lt;SPAN&gt;&amp;lt;- "A12" contained in $class2)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;5 D12 A32 200108 (&lt;SPAN&gt;&amp;lt;- "A" contained in $class2)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I make the data like this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would much appreciate all your possible answers. Thank you!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 04:55:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442729#M13658</guid>
      <dc:creator>nexterd</dc:creator>
      <dc:date>2018-03-06T04:55:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442737#M13659</link>
      <description>&lt;P&gt;Would the result be the same if the last obs was&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5 D12 A12 200111&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And either way, why?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 05:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442737#M13659</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-06T05:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442738#M13660</link>
      <description>&lt;P&gt;this may work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
/* geting latest records other than A12 or A*/
select id,  class1,  class2,  yyyymm from play
where id not in 
(select id from play
 where class1 = "A12" 
 or   class2  = "A" 
 group by id 
having yyyymm = min(yyyymm))
 group by id 
having yyyymm = min(yyyymm)
union
/* geting latest records for A12 or A*/
select id,  class1,  class2,  yyyymm from play
 where class1 = "A12" 
 or   class2  = "A" 
 group by id 
having yyyymm = min(yyyymm);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 05:34:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442738#M13660</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-03-06T05:34:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442739#M13661</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Sort the data by id and&amp;nbsp;yyyymm&lt;/P&gt;&lt;P&gt;proc sort data =&amp;nbsp;&lt;SPAN&gt;play;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; by id yyyymm;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp;Extract the rows containing 'A' in either class1 or class2&lt;/P&gt;&lt;P&gt;data playClassA;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set&amp;nbsp;&lt;SPAN&gt;play;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if substr(class1,1,1)='A' |&amp;nbsp;substr(class2,1,1)='A';&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. Get the first record&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;data&lt;SPAN&gt;&amp;nbsp;dsOut1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set&amp;nbsp;&lt;SPAN&gt;playClassA;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; by id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if first.id then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4. Extract the&amp;nbsp;ids containing no 'A' in either class1 or class2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data&amp;nbsp;playClassABar;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; merge&amp;nbsp;play &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dsOut1 (in=a keep=id);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; by id;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;5.&amp;nbsp;&lt;SPAN&gt;Get the&amp;nbsp;last record&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;data&lt;SPAN&gt;&amp;nbsp;dsOut2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set&amp;nbsp;&lt;SPAN&gt;playClassA;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; by id;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if last.id then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;6. Combine the results&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data dsOut;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; set dsOut1 dsOut2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hope this will help.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 05:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442739#M13661</guid>
      <dc:creator>liangchh0</dc:creator>
      <dc:date>2018-03-06T05:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442750#M13662</link>
      <description>&lt;P&gt;Maybe this will do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view temp as
select 
    *,
    case 
        when class1 = "A12" or class2 = "A12" then 1
        when first(class1) = "A" or first(class2) = "A" then 2
        else 3
        end as order
from play
order by id, order, yyyymm;
quit;

data want;
set temp; by id;
if first.id;
drop order;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit -- Sorry, this code doesn't always give the correct answer. See below.&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 17:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442750#M13662</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-06T17:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442758#M13663</link>
      <description>&lt;P&gt;Oh I just wanted to differentiate A12 from all values which contain "A" because I set "A12" as a case and other "A"s as censored.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 06:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442758#M13663</guid>
      <dc:creator>nexterd</dc:creator>
      <dc:date>2018-03-06T06:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442760#M13664</link>
      <description>&lt;P&gt;This solution is almost similar to what I was trying to. It's kind of a classical approach so I did it, but it was complicated to use. I will also try your solution. Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 06:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442760#M13664</guid>
      <dc:creator>nexterd</dc:creator>
      <dc:date>2018-03-06T06:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442777#M13665</link>
      <description>&lt;P&gt;Thank your for your reply! I tried it as you wrote and it looks like doing well but I cannot confirm that because of a resource problem. Besides, how can I save the new data set which was made throughout the process?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 07:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442777#M13665</guid>
      <dc:creator>nexterd</dc:creator>
      <dc:date>2018-03-06T07:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442786#M13666</link>
      <description>&lt;P&gt;I perfectly resolved the problem thanks to your solution! Thank you very much! I really&amp;nbsp;love such a laconic style of your coding.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Mar 2018 08:18:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442786#M13666</guid>
      <dc:creator>nexterd</dc:creator>
      <dc:date>2018-03-06T08:18:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442792#M13667</link>
      <description>&lt;P&gt;Something along the line of below should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want(drop=_:);
  set have;
  by id;
  retain _done;
  if first.id then _done=0;
  if _done=0 then 
    do;
      if findc(class1,'A','i') or findc(class2,'A','i') then
        do;
          output;
          _done=1;
        end;
      else if last.id then output;
    end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 08:41:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442792#M13667</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-03-06T08:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract a row from several observations under multiple conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442978#M13675</link>
      <description>&lt;P&gt;Sorry. My suggestion above does NOT&amp;nbsp;always give the expected answer in the case where A12 or A is not found..This, however will:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
lookfor = "---";
do until(last.id);
    set play; by id;
    if class1 = "A12" or class2 = "A12" then lookfor = "A12";
    if lookfor ne "A12" and (first(class1) = "A" or first(class2) = "A") then lookfor = "A";
    end;
do until(last.id);
    set play; by id;
    if class1 =: trim(lookfor) or class2 =: trim(lookfor) then do;
        output;
        lookfor = "+++";
        end;
    end;
if lookfor = "---" then output;
drop lookfor;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Mar 2018 19:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-extract-a-row-from-several-observations-under-multiple/m-p/442978#M13675</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-06T19:06:08Z</dc:date>
    </item>
  </channel>
</rss>

