<?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: Want to subset a champion/Challenger dataset from larger population in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676937#M204141</link>
    <description>&lt;P&gt;Thank you Shmuel,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the output was blank... no errors. the second do block was not closed so i closed it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;data want1;&lt;BR /&gt;&amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; by ID;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain lag_type&amp;nbsp; lag_date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.ID then do; lag_type=" "; lag_date=.; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else do; lag_type = type; lag_date=date; end;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not first.ID and type='Symend' and lag_type='Regular' then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; R_type = lag_type; R_date=lag_date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; S_type = type;&amp;nbsp; S_date = date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keep ID R_: S_: ;end;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;The solution by&amp;nbsp;mkeintz&amp;nbsp; worked. I would love to run both and see if there is a variance. Would appreciate you looking into it.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;Thank you again&lt;/FONT&gt;&lt;/DIV&gt;</description>
    <pubDate>Sat, 15 Aug 2020 06:07:02 GMT</pubDate>
    <dc:creator>TheNovice</dc:creator>
    <dc:date>2020-08-15T06:07:02Z</dc:date>
    <item>
      <title>Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676925#M204134</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The dataset is like below with accounts undergoing treatment R. At some point they will switch to treatment S. Once they switch to S they will not switch back to R. I would like to identify 2 consecutive records where an ID had R and then received S&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: Some id's will never move to S&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;99997887&lt;/TD&gt;&lt;TD&gt;05-May-20&lt;/TD&gt;&lt;TD&gt;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t;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100009933&lt;/TD&gt;&lt;TD&gt;10-May-20&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100009933&lt;/TD&gt;&lt;TD&gt;10-Jul-20&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100009933&lt;/TD&gt;&lt;TD&gt;09-Aug-20&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100010577&lt;/TD&gt;&lt;TD&gt;22-May-20&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100010577&lt;/TD&gt;&lt;TD&gt;22-Jul-20&lt;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sat, 15 Aug 2020 04:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676925#M204134</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-08-15T04:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676926#M204135</link>
      <description>&lt;P&gt;Do you want a report?&amp;nbsp; Or a dataset?&amp;nbsp; What do you want it to look like?&lt;/P&gt;</description>
      <pubDate>Sat, 15 Aug 2020 04:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676926#M204135</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-15T04:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676927#M204136</link>
      <description>&lt;P&gt;A dataset so i can further analysis.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so i would want all id's where there was an event R and then an event S after.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I only care about extracting 2 rows per ID this way. R would be my champion and S would be the challenger&lt;/P&gt;</description>
      <pubDate>Sat, 15 Aug 2020 05:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676927#M204136</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-08-15T05:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676928#M204137</link>
      <description>&lt;P&gt;Assuming that (1) the data are sorted by ID/DATE, and (2) you want both the R record and the S record for all occasions when a given ID has an R immediately followed by an S:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID	date :date9. type :$1. ;
  format date date9. ;
datalines;
99997887	05-May-20	R
99997887	04-Jun-20	R
99998960	02-Jun-20	S
99998960	03-Jul-20	S
99998960	03-Aug-20	S
100003216	14-Jun-20	R
100003216	15-Jul-20	R
100005355	05-May-20	R
100005355	04-Jun-20	R
100005355	05-Jul-20	R
100005981	03-Jun-20	R
100005981	03-Aug-20	S
100009933	10-May-20	R
100009933	10-Jul-20	R
100009933	09-Aug-20	R
100010577	22-May-20	R
100010577	22-Jul-20	S
run;

data want (drop=nxt_:);
  set have (keep=id);
  by id;
  merge have have (firstobs=2 keep=type rename=(type=nxt_type));

  if (last.id=0 and type='R' and nxt_type='S')
  or (first.id=0 and type='S' and lag(type)='R');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The SET statement only retrieves the ID variable, because (in combination with the subsequent BY statement) its only purpose is to provide a way to determine whether the record-in-hand is not the start of an ID (first.id=0) or not the end of an ID (last.id=0).&lt;/LI&gt;
&lt;LI&gt;The MERGE retrieves each record and simultaneously one variable (type renamed to nxt_type) from the following record.&lt;/LI&gt;
&lt;LI&gt;Once you know whether the record-in-hand is an R (not at the end of an ID) followed by an S,&amp;nbsp;&amp;nbsp; or is an S (not at the beginning of an ID) preceded by an R, then keep it.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Aug 2020 05:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676928#M204137</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-15T05:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676934#M204140</link>
      <description>&lt;P&gt;You can subset two records as required and may be prefer next output format&lt;/P&gt;
&lt;P&gt;having both records data in one observation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
   by ID;
      retain lag_type  lag_date;
      if first.ID then do; lag_type=" "; lag_date=.; end;
      else do; lag_type = type; lag_date=date; end;

     if not first.ID and type='S' and lag_type='R' then do;
        R_type = lag_type; R_date=lag_date;
        S_type = type;  S_date = date;
        output;
       keep ID R_: S_: ;&lt;BR /&gt;     end;    /* line added */
run;
         &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Aug 2020 06:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676934#M204140</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-15T06:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676937#M204141</link>
      <description>&lt;P&gt;Thank you Shmuel,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the output was blank... no errors. the second do block was not closed so i closed it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;FONT&gt;&lt;BR /&gt;data want1;&lt;BR /&gt;&amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; by ID;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain lag_type&amp;nbsp; lag_date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.ID then do; lag_type=" "; lag_date=.; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else do; lag_type = type; lag_date=date; end;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not first.ID and type='Symend' and lag_type='Regular' then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; R_type = lag_type; R_date=lag_date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; S_type = type;&amp;nbsp; S_date = date;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keep ID R_: S_: ;end;&lt;BR /&gt;run;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;The solution by&amp;nbsp;mkeintz&amp;nbsp; worked. I would love to run both and see if there is a variance. Would appreciate you looking into it.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT&gt;Thank you again&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 15 Aug 2020 06:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676937#M204141</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-08-15T06:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676938#M204142</link>
      <description>&lt;P&gt;It worked. Thank you so much. I was going to go the proc sql way by subsetting and then joining again but your method is fast and elegant. Thanks for the explanation. I would just like to compare this to the second solution posted and see if there is a variance in output. Your extract is what i was looking for. Your assumption is bang on.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Aug 2020 06:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676938#M204142</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-08-15T06:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676939#M204143</link>
      <description>&lt;P&gt;Next is a fixed tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x;
  input ID	date :date9. type :$1. ;
  format date date9. ;
datalines;
99997887	05-May-20	R
99997887	04-Jun-20	R
99998960	02-Jun-20	S
99998960	03-Jul-20	S
99998960	03-Aug-20	S
100003216	14-Jun-20	R
100003216	15-Jul-20	R
100005355	05-May-20	R
100005355	04-Jun-20	R
100005355	05-Jul-20	R
100005981	03-Jun-20	R
100005981	03-Aug-20	S
100009933	10-May-20	R
100009933	10-Jul-20	R
100009933	09-Aug-20	R
100010577	22-May-20	R
100010577	22-Jul-20	S
run;
data want;
 set have;
   by ID;
      retain lag_type  lag_date;
      lag_type = lag(type); lag_date=lag(date); 
      if first.ID then do; lag_type=" "; lag_date=.; end;

if ID in (100005981	, 100010577	) then 
    putlog  ID= date= type= lag_type= lag_date;

     if not first.ID and type='S' and lag_type='R' then do;
        R_type = lag_type; R_date=lag_date;
        S_type = type;  S_date = date;
        output;
       keep ID R_: S_: ;     end;    /* line added */
run;
         &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Aug 2020 06:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676939#M204143</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-15T06:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676947#M204145</link>
      <description>&lt;P&gt;you may also try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length val $10.;
do until(last.id);
set have;
by id date;
retain val;
if first.id then val=type;
else val=cats(val,type);
end;
do until(last.id);
set have;
by id date;
if index(val,'RS') then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Aug 2020 10:16:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/676947#M204145</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-08-15T10:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: Want to subset a champion/Challenger dataset from larger population</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/681672#M206209</link>
      <description>&lt;P&gt;Sorry for the delay all. I was on vacation and taking a break from all work. I tested the next 2 suggestions and they both work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 15:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-subset-a-champion-Challenger-dataset-from-larger/m-p/681672#M206209</guid>
      <dc:creator>TheNovice</dc:creator>
      <dc:date>2020-09-04T15:52:09Z</dc:date>
    </item>
  </channel>
</rss>

