<?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: selecting records, ranked variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711305#M219100</link>
    <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361111"&gt;@Batta&lt;/a&gt;&amp;nbsp; Do you mean this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 do _n_=1 by 1 until(last.subject);
  set have;
  by subject;
  length k $3;
  if not last.subject then  k=treatment;
 end;
 _iorc_=k='no' and treatment='yes';
 k1=_n_;
 do _n_=1 to _n_;
  set have;
  if _iorc_ and _n_ &amp;gt;=k1-1  then output;
 end;
 drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Jan 2021 21:49:03 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2021-01-13T21:49:03Z</dc:date>
    <item>
      <title>selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711272#M219082</link>
      <description>&lt;P&gt;I apologize that I could not find better way to define my subject line.&lt;/P&gt;&lt;P&gt;This is the example of the data set: I have subjects , with different visit number (visit_num) 1-3 with treatment, "yes" for treatment received and "no" for treatment not received. I need to select/list subjects that look like subject s2, that has the last visit (3) with treatment="yes" but the previous visit (2) with treatment = "no" (others have last visit treatment "no" while the previous one was "yes". Your help is highly appreciated. Thanks a lot.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;subject&lt;/TD&gt;&lt;TD&gt;visit_num&lt;/TD&gt;&lt;TD&gt;treatment&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;yes&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;s3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;no&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711272#M219082</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-01-13T20:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711278#M219087</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361111"&gt;@Batta&lt;/a&gt;&amp;nbsp; Do all subjects have the same 1-3 vistnum?&lt;/P&gt;
&lt;P&gt;If Yes--&amp;gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input subject $	visit_num	treatment $;
cards;
s1	1	yes
s1	2	yes
s1	3	no
s2	1	yes
s2	2	no
s2	3	yes
s3	1	yes
s3	2	yes
s3	3	no
;

proc sql;
 create table want as
 select *
 from have
 group by subject
 having max(visit_num=2 and treatment='no') and max(visit_num=3 and treatment='yes')
 order by subject,visit_num;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711278#M219087</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-13T20:53:32Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711282#M219090</link>
      <description>&lt;P&gt;Is the only condition that last visit has to be "yes" and the previous visit is "no"? And will there always be only three visits per subject?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, as your example indicated, here's one way to do it--by transposing your original dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input subject $ visit_num treatment $;
    datalines;
s1 1 yes
s1 2 yes
s1 3 no
s2 1 yes
s2 2 no
s2 3 yes
s3 1 yes
s3 2 yes
s3 3 no
    ;
run;

proc transpose data=have out=have_transposed prefix=visit_num;
    by subject;
    id visit_num;
    var treatment;
run;

proc sql;
    create table want as
    select distinct subject
    from have_transposed
    where visit_num3 = "yes" 
        and visit_num2 = "no";
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711282#M219090</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2021-01-13T20:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711285#M219091</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input subject $	visit_num	treatment $;
cards;
s1	1	yes
s1	2	yes
s1	3	no
s2	1	yes
s2	2	no
s2	3	yes
s3	1	yes
s3	2	yes
s3	3	no
;

data want;
 do _n_=1 by 1 until(last.subject);
  set have;
  by subject;
  length k $3;
  if not last.subject then  k=treatment;
 end;
 _iorc_=k='no' and treatment='yes';
 do _n_=1 to _n_;
  set have;
  if _iorc_ then output;
 end;
 drop k;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:11:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711285#M219091</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-13T21:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711289#M219092</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am sorry, I made a mistake, so the data came not quite completed:&lt;/P&gt;&lt;P&gt;Number of visits can vary, some subject may have 5, some other subject can have 10, the point is that I always select the LAST visit and the visit before the last one and those subject that always have the last visit treatment "yes" and the previous one was "no"&lt;/P&gt;&lt;P&gt;My apology for not properly explaining the data.&lt;/P&gt;&lt;P&gt;THank you very much.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711289#M219092</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-01-13T21:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711301#M219099</link>
      <description>&lt;P&gt;Thank you very much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works great!&amp;nbsp;&lt;/P&gt;&lt;P&gt;If it's not big problem to do: is it possible to drop all other visits from the output but the last one and the one before the last one, since all other before are not relevant to have them in the output.&lt;/P&gt;&lt;P&gt;I really appreciate your dedicated time and your wish to help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:42:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711301#M219099</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-01-13T21:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711305#M219100</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361111"&gt;@Batta&lt;/a&gt;&amp;nbsp; Do you mean this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 do _n_=1 by 1 until(last.subject);
  set have;
  by subject;
  length k $3;
  if not last.subject then  k=treatment;
 end;
 _iorc_=k='no' and treatment='yes';
 k1=_n_;
 do _n_=1 to _n_;
  set have;
  if _iorc_ and _n_ &amp;gt;=k1-1  then output;
 end;
 drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711305#M219100</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-13T21:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711332#M219112</link>
      <description>&lt;P&gt;That's it!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much Sir!.&lt;/P&gt;&lt;P&gt;I appreciate your effort and your time dedicated to this!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards!&lt;/P&gt;&lt;P&gt;Batta&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 01:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711332#M219112</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-01-14T01:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711333#M219113</link>
      <description>&lt;P&gt;Ah, mistakenly I click on my own reply to accept solution. I hope that admins can correct this...&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 01:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711333#M219113</guid>
      <dc:creator>Batta</dc:creator>
      <dc:date>2021-01-14T01:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: selecting records, ranked variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711367#M219133</link>
      <description>&lt;P&gt;You can easily correct this yourself, no Admin needed &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 08:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-records-ranked-variables/m-p/711367#M219133</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-14T08:27:15Z</dc:date>
    </item>
  </channel>
</rss>

