<?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: Add flag=1 when condition true and ID in same group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450884#M283777</link>
    <description>&lt;P&gt;Is the count in the criteria (count&amp;lt;3) the same as the count in the data set, which looks to be a character variable (leading zeros?)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Apr 2018 20:53:56 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-04-03T20:53:56Z</dc:date>
    <item>
      <title>Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450813#M283776</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have dataset containing&amp;nbsp; IDs, with different rank, count, and dates.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you could offer a tip to meet the following: if condition true, then flag 1 for all same IDs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example HAVE&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; Rank Count Date&lt;/P&gt;&lt;P&gt;001&amp;nbsp; 01&amp;nbsp; &amp;nbsp; &amp;nbsp;01&amp;nbsp; &amp;nbsp; &amp;nbsp; 2017&lt;/P&gt;&lt;P&gt;001&amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp;01&amp;nbsp; &amp;nbsp; &amp;nbsp; 9999&lt;/P&gt;&lt;P&gt;002&amp;nbsp; 01&amp;nbsp; &amp;nbsp; &amp;nbsp;03&amp;nbsp; &amp;nbsp; &amp;nbsp;9999&lt;/P&gt;&lt;P&gt;003&amp;nbsp; 01&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&lt;/P&gt;&lt;P&gt;003&amp;nbsp; 02&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp;9999&lt;/P&gt;&lt;P&gt;004&amp;nbsp; 01&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9999&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example &lt;STRONG&gt;WANT&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;New Var Conditions:&amp;nbsp; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;midflag:&lt;/STRONG&gt;where year 9999 and count&amp;lt;3&amp;nbsp; /*optional variable*/&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;endflag:&lt;/STRONG&gt;where midlflag=1 and ID (appears once or is duplicate from different row)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; Rank Count Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MIDflag&amp;nbsp; Endflag&lt;/P&gt;&lt;P&gt;001&amp;nbsp; 01&amp;nbsp; &amp;nbsp; &amp;nbsp;01&amp;nbsp; &amp;nbsp; &amp;nbsp; 2017&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;001&amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp;01&amp;nbsp; &amp;nbsp; &amp;nbsp; 9999&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;002&amp;nbsp; 01&amp;nbsp; &amp;nbsp; &amp;nbsp;03&amp;nbsp; &amp;nbsp; &amp;nbsp;9999&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;003&amp;nbsp; 01&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;003&amp;nbsp; 02&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp;9999&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;004&amp;nbsp; 01&amp;nbsp; &amp;nbsp; 02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9999&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 19:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450813#M283776</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2018-04-03T19:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450884#M283777</link>
      <description>&lt;P&gt;Is the count in the criteria (count&amp;lt;3) the same as the count in the data set, which looks to be a character variable (leading zeros?)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 20:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450884#M283777</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-03T20:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450900#M283778</link>
      <description>&lt;P&gt;You can interleave the dataset with itself on an ID by ID basis, such that for each id:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The subset of cases with date=9999 and count&amp;lt;3 are read an extra time prior to reading all cases for the id.&amp;nbsp; If any such preliminary observations are found, then ENDFLAG must be 1&lt;/LI&gt;
&lt;OL&gt;
&lt;LI&gt;RETAIN the ENDFLAG&lt;/LI&gt;
&lt;LI&gt;but don't keep the above records (they are going to be re-read).&amp;nbsp; Hence the subsetting IF statement&amp;nbsp; "IF INKEEP;"&lt;/LI&gt;
&lt;/OL&gt;
&lt;LI&gt;Followed by reading all cases for the ID, with MIDFLAG set record by record.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Id  Rank Count Date;
datalines;
001  01    01     2017
001  02    01     9999
002  01    03     9999
003  01    02     2018
003  02    02     9999
004  01    02     9999
run;

data want;
  set have (where=(date=9999 and count&amp;lt;3) in=inmid)
      have (in=inkeep);
  by id;

  if first.id then ENDflag=0;
  if inmid then ENDflag=1;
  retain endflag;

  if inkeep;
  if date=9999 and count&amp;lt;3 then midflag=1;
  else midflag=0;
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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Apr 2018 21:45:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450900#M283778</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-04-03T21:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450902#M283779</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Id  Rank Count Date;
datalines;
001  01    01     2017
001  02    01     9999
002  01    03     9999
003  01    02     2018
003  02    02     9999
004  01    02     9999
run;

proc sql;
create table want1 as
select *,(date=9999 and count&amp;lt;3) as MIDflag,max(calculated MIDflag) as Endflag
from have
group by Id
order by id, rank;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Apr 2018 22:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/450902#M283779</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-03T22:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451033#M283780</link>
      <description>it is a number formatted 1.</description>
      <pubDate>Wed, 04 Apr 2018 11:41:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451033#M283780</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2018-04-04T11:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451075#M283781</link>
      <description>hi,&lt;BR /&gt;thank you for the tip, + explanation... as an aside, if there are any key books or articles you have to recommend, please let me know!</description>
      <pubDate>Wed, 04 Apr 2018 12:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451075#M283781</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2018-04-04T12:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Add flag=1 when condition true and ID in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451076#M283782</link>
      <description>thank you for the tip! very helpful</description>
      <pubDate>Wed, 04 Apr 2018 12:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-flag-1-when-condition-true-and-ID-in-same-group/m-p/451076#M283782</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2018-04-04T12:49:33Z</dc:date>
    </item>
  </channel>
</rss>

