<?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: top row by group with flag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773881#M245908</link>
    <description>&lt;P&gt;Its a little more tricky than just using FIRST., but this does it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input idt_ixn tsp_deb datetime18. top_dec $ ;
informat tsp_deb datetime18.;
format tsp_deb datetime18. ;
datalines ;
123 10NOV2020:10:10:10 0
123 10NOV2020:11:11:11 1
123 10NOV2020:12:12:12 1
456 11NOV2020:10:10:10 0
456 12NOV2020:11:11:11 0
456 13NOV2020:12:12:12 0
;

data want;
merge
  have
  have (
    in=h1
    keep=idt_ixn top_dec
    rename=(top_dec=_top)
    where=(_top = "1")
  )
;
by idt_ixn;
__top = lag(top_dec);
flag = 0;
if first.idt_ixn and not h1 then flag = 1;
if h1 and top_dec = "1" and (first.idt_ixn or __top ne "1") then flag = 1;
drop _top __top;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Oct 2021 10:33:35 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-10-13T10:33:35Z</dc:date>
    <item>
      <title>top row by group with flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773876#M245904</link>
      <description>&lt;PRE&gt;data have;
input idt_ixn tsp_deb datetime18. top_dec $ ;
informat tsp_deb datetime18.;
format tsp_deb datetime18. ;
datalines ;
123 10NOV2020:10:10:10 0
123 10NOV2020:11:11:11 1
123 10NOV2020:12:12:12 1
456 11NOV2020:10:10:10 0
456 12NOV2020:11:11:11 0
456 13NOV2020:12:12:12 0
;&lt;/PRE&gt;
&lt;P&gt;hello,&lt;/P&gt;
&lt;P&gt;I would like tocreate a flag with value "1" only on the first top_dec =1 of the idt_ixn &amp;nbsp;or on the first idt_ixn when there is no top_dec=1&lt;/P&gt;
&lt;P&gt;in this case,&lt;/P&gt;
&lt;P&gt;for the group&amp;nbsp;idt_ixn=123, the flag should be 1 only where tsp_deb is 10NOV2020:11:11:11&lt;/P&gt;
&lt;P&gt;for the group&amp;nbsp;idt_ixn=456, the flag should be 1 only where tsp_deb is 11NOV2020:10:10:10&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;thnaks in advance&lt;/P&gt;
&lt;P&gt;kind regards&lt;/P&gt;
&lt;P&gt;Nass&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Oct 2021 10:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773876#M245904</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2021-10-13T10:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: top row by group with flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773878#M245906</link>
      <description>&lt;P&gt;Check out &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p181g1p4bw3phkn1vt5p67xvynd5.htm" target="_self"&gt;FIRST. and LAST. Data Step Variables&lt;/A&gt;&lt;BR /&gt;That's how you can do this&lt;BR /&gt;EDIT - Should have read the post more carefully, not as simple as it first appears&lt;/P&gt;</description>
      <pubDate>Wed, 13 Oct 2021 10:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773878#M245906</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-10-13T10:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: top row by group with flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773881#M245908</link>
      <description>&lt;P&gt;Its a little more tricky than just using FIRST., but this does it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input idt_ixn tsp_deb datetime18. top_dec $ ;
informat tsp_deb datetime18.;
format tsp_deb datetime18. ;
datalines ;
123 10NOV2020:10:10:10 0
123 10NOV2020:11:11:11 1
123 10NOV2020:12:12:12 1
456 11NOV2020:10:10:10 0
456 12NOV2020:11:11:11 0
456 13NOV2020:12:12:12 0
;

data want;
merge
  have
  have (
    in=h1
    keep=idt_ixn top_dec
    rename=(top_dec=_top)
    where=(_top = "1")
  )
;
by idt_ixn;
__top = lag(top_dec);
flag = 0;
if first.idt_ixn and not h1 then flag = 1;
if h1 and top_dec = "1" and (first.idt_ixn or __top ne "1") then flag = 1;
drop _top __top;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Oct 2021 10:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/top-row-by-group-with-flag/m-p/773881#M245908</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-13T10:33:35Z</dc:date>
    </item>
  </channel>
</rss>

