<?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 to get nth value by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310357#M66926</link>
    <description>No its not working. getting zero observations&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
    <pubDate>Wed, 09 Nov 2016 13:12:50 GMT</pubDate>
    <dc:creator>SrikanthY</dc:creator>
    <dc:date>2016-11-09T13:12:50Z</dc:date>
    <item>
      <title>How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310352#M66924</link>
      <description>&lt;P&gt;I want immediate non missing observation value after last flag is Y. that is: visit 3rd of id A1001 and visit 4th of&amp;nbsp;&lt;SPAN&gt;A1002&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data x;&lt;BR /&gt;infile cards missover;&lt;BR /&gt;input id $ visit val flag $;&lt;BR /&gt;cards;&lt;BR /&gt;A1001 1 20 Y&lt;BR /&gt;A1001 2 20 Y&lt;BR /&gt;A1001 3 20&lt;BR /&gt;A1001 4 .&lt;BR /&gt;A1001 5 20&lt;BR /&gt;A1002 1 20&lt;BR /&gt;A1002 2 20 Y&lt;BR /&gt;A1002 3 .&lt;BR /&gt;A1002 4 20&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 12:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310352#M66924</guid>
      <dc:creator>SrikanthY</dc:creator>
      <dc:date>2016-11-09T12:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310355#M66925</link>
      <description>&lt;P&gt;Not sure what the output should look like but something like:&lt;/P&gt;
&lt;PRE&gt;data want;
  set x;
  by id;
  retain tmp_flg;
  if first.id then tmp_flg=0;
  if flag="" and tmp_flag then tmp_flag=1;
  else if tmp_flg=1 and val ne . then output;
run;&lt;/PRE&gt;
&lt;P&gt;Not tested, but that should work.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 13:08:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310355#M66925</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-09T13:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310357#M66926</link>
      <description>No its not working. getting zero observations&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 09 Nov 2016 13:12:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310357#M66926</guid>
      <dc:creator>SrikanthY</dc:creator>
      <dc:date>2016-11-09T13:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310360#M66927</link>
      <description>&lt;P&gt;There was a typo:&lt;/P&gt;
&lt;PRE&gt;data x;
infile cards missover;
input id $ visit val flag $;
cards;
A1001 1 20 Y
A1001 2 20 Y
A1001 3 20
A1001 4 .
A1001 5 20
A1002 1 20
A1002 2 20 Y
A1002 3 .
A1002 4 20
;
run;
data want;
  set x;
  by id;
  retain tmp_flg;
  if first.id then tmp_flg=0;
  if flag="Y" then tmp_flg=1;
  if flag="" and tmp_flg=1 and val ne . then do;
    tmp_flg=2;
    final="Y";
  end;
run;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 13:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310360#M66927</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-09T13:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310362#M66928</link>
      <description>fine thank you. But I want to keep only those records in final dataset.&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 09 Nov 2016 13:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310362#M66928</guid>
      <dc:creator>SrikanthY</dc:creator>
      <dc:date>2016-11-09T13:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310364#M66929</link>
      <description>&lt;P&gt;Yes, but you didn't specify what the output should look like in your post, hence I was guessing. &amp;nbsp;Replace this line:&lt;/P&gt;
&lt;PRE&gt;    final="Y";
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With:&lt;/P&gt;
&lt;PRE&gt;    output;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 13:29:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310364#M66929</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-09T13:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to get nth value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310561#M67006</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data x;
infile cards missover;
input id $ visit val flag $;
cards;
A1001 1 20 Y
A1001 2 20 Y
A1001 3 20
A1001 4 .
A1001 5 20
A1002 1 20
A1002 2 20 Y
A1002 3 .
A1002 4 20
;
run;
data want;
 do i=1 by 1 until(last.id);
  set x;
  by id;
  if flag='Y' then _i=i;
 end;
 n=0;
 do j=1 by 1 until(last.id);
  set x;
  by id;
  if j gt _i and not missing(val) then n+1;
  if n=1 and not missing(val) then output;
 end;
drop i j n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 03:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-nth-value-by-group/m-p/310561#M67006</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-10T03:33:39Z</dc:date>
    </item>
  </channel>
</rss>

