<?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: Flagging first variable above a certain value within  a by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666520#M199444</link>
    <description>&lt;P&gt;Thank you,&amp;nbsp; works! would you mind explaining&amp;nbsp; what the found variable is? especially the ^found part?&lt;/P&gt;</description>
    <pubDate>Thu, 02 Jul 2020 01:50:53 GMT</pubDate>
    <dc:creator>KPCklebspn</dc:creator>
    <dc:date>2020-07-02T01:50:53Z</dc:date>
    <item>
      <title>Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666501#M199431</link>
      <description>&lt;P&gt;I want to&amp;nbsp; flag&amp;nbsp; the&amp;nbsp; first value for var &amp;gt;7 within&amp;nbsp; a by&amp;nbsp; group (ID). What&amp;nbsp; is the&amp;nbsp; simplest way to do this?&amp;nbsp; Thank&amp;nbsp; you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;P&gt;b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;/P&gt;&lt;P&gt;b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Want&lt;/P&gt;&lt;P&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jul 2020 23:04:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666501#M199431</guid>
      <dc:creator>KPCklebspn</dc:creator>
      <dc:date>2020-07-01T23:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666504#M199433</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  set HAVE;
  by ID;
  if first.ID then FOUND=0;
  if ^FOUND &amp;amp; VAR &amp;gt; 7 then do;
    FOUND+1;
    FLAG=1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jul 2020 23:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666504#M199433</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-01T23:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666506#M199435</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input id $    var ;
cards;
a       1

a       7

a       10

b        8

b        9
;

data  want;
 do until(last.id);
  set have;
  by id;
  call missing(flag);
  if _n_ and var&amp;gt;7 then do;
   flag=1;
   _n_=0;
  end;
  output;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Jul 2020 23:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666506#M199435</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-07-01T23:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666514#M199441</link>
      <description>&lt;P&gt;The "trick" here is make sure that you don't inadvertently flag a VAR&amp;gt;7 if it is the second (or third, etc.) such record for a given id.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if I would exactly recommend this technique, but it is an excellent task to demonstrate the queue-management techniques of the LAG function, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $1.    var;
datalines;
a       1
a       7
a       10
b        8
b        9
run;

data want;
  set have;
  if var&amp;gt;7 then do;
    if id^=lag(id) then flag=1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The LAG function only updates its underlying queue (in this case a queue of length 1) when the record in hand has VAR&amp;gt;7.&amp;nbsp; &amp;nbsp;So the only time ID comparisons are made are for those records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note you don't need a BY statement or resetting of any variables in this code, although the data must be physically sorted/grouped by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you were to want, say, the first THREE records with var&amp;gt;7 for each ID&amp;nbsp; (where each ID data can have any number of such ID's), then you can replace LAG(ID) with LAG3(ID), as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $1.    var;
datalines;
a       1
a       7
a       10
a        12
a        13
a       16
b        8
b        9
b        10
b        12
b        13
run;

data want;
  set have;
  if var&amp;gt;7 then do;
    if id^=lag3(id) then flag=1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The LAG3 function maintains a queue (a FIFO queue) with length 3, i.e. the three most recent id's for records with VAR&amp;gt;7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 00:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666514#M199441</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-02T00:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666517#M199442</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;Only one flag per ID is wanted as I understand.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 01:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666517#M199442</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-02T01:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666520#M199444</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp; works! would you mind explaining&amp;nbsp; what the found variable is? especially the ^found part?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 01:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666520#M199444</guid>
      <dc:creator>KPCklebspn</dc:creator>
      <dc:date>2020-07-02T01:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666603#M199476</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you may have looked only at my second example.&amp;nbsp; The first example does produce only one flag per id.&amp;nbsp; The reason for the second example was just to show how easy it would be to generate flags for the first X qualifying records for each ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And just to gild the lily.&amp;nbsp; What if the OP wanted to flag, say the 2nd through 3rd&amp;nbsp; qualifying records.&amp;nbsp; It would be still be relatively straightforward using the conditional lag functions. It would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; the first 3, i.e. where &lt;EM&gt;&lt;STRONG&gt;lag3(id)^=id&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; starting with the 2nd, where&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;lag(id)=id&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;resulting in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if var&amp;gt;7 then do;
    if id^=lag3(id) and lag(id)=id then flag=1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&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>Thu, 02 Jul 2020 14:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666603#M199476</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-02T14:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666985#M199668</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $    var ;
cards;
a       1
a       7
a       10
b        8
b        9
;

proc sort data=have out=haves;
by id descending var;
run;

data want;
set haves;
by id descending var;
if (first.id and first.var and var &amp;gt; 7) then flag=1;
else flag=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2020 03:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/666985#M199668</guid>
      <dc:creator>tocilj</dc:creator>
      <dc:date>2020-07-05T03:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging first variable above a certain value within  a by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/667231#M199753</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/322220"&gt;@tocilj&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code will work, but you don't need the "and first.var" in the subsetting IF condition.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 16:24:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-first-variable-above-a-certain-value-within-a-by-group/m-p/667231#M199753</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-06T16:24:57Z</dc:date>
    </item>
  </channel>
</rss>

