<?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 Field based on other variables until next group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Field-based-on-other-variables-until-next-group/m-p/899699#M355578</link>
    <description>&lt;P&gt;I want to create a new field based on existing field if certain conditions are met for a specific group; and keep on assigning it until the next group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data (3 columns - ID, Date, RandomInteger) :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID | Date | RandomInteger&lt;/P&gt;&lt;P&gt;01 | Jul 1, 2000 | 1&lt;/P&gt;&lt;P&gt;01 | Aug 15, 2001 | 0&lt;/P&gt;&lt;P&gt;02 | Aug 30, 2000 | 0&lt;/P&gt;&lt;P&gt;02| Aug 31, 2001 | 2&lt;/P&gt;&lt;P&gt;02 | Sep 30, 2003 | 5&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2005 | 1&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2007 | 0&lt;/P&gt;&lt;P&gt;03| Mar 31, 1999 | 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An ID can have numerous Dates. RandomInteger can not be negative.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;NewField = RandomInteger&lt;/P&gt;&lt;P&gt;however, if RandomInteger for the group (ID and Date) have exceeded a threshold ( take 3 for example) then NewField is assigned to 3 for subsequent rows that belong to the same ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID | Date | RandomInteger |NewField&lt;/P&gt;&lt;P&gt;01 | Jul 1, 2000 | 1 | 1&lt;/P&gt;&lt;P&gt;01 | Aug 15, 2001 | 0 | 0&lt;/P&gt;&lt;P&gt;02 | Aug 30, 2000 | 0 | 0&lt;/P&gt;&lt;P&gt;02| Aug 31, 2001 | 2 | 2&lt;/P&gt;&lt;P&gt;02 | Sep 30, 2003 | 5 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2005 | 1 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2007 | 0 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;03| Mar 31, 1999 | 0 | 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 24 Oct 2023 00:48:31 GMT</pubDate>
    <dc:creator>sasuser1234567</dc:creator>
    <dc:date>2023-10-24T00:48:31Z</dc:date>
    <item>
      <title>Field based on other variables until next group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Field-based-on-other-variables-until-next-group/m-p/899699#M355578</link>
      <description>&lt;P&gt;I want to create a new field based on existing field if certain conditions are met for a specific group; and keep on assigning it until the next group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data (3 columns - ID, Date, RandomInteger) :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID | Date | RandomInteger&lt;/P&gt;&lt;P&gt;01 | Jul 1, 2000 | 1&lt;/P&gt;&lt;P&gt;01 | Aug 15, 2001 | 0&lt;/P&gt;&lt;P&gt;02 | Aug 30, 2000 | 0&lt;/P&gt;&lt;P&gt;02| Aug 31, 2001 | 2&lt;/P&gt;&lt;P&gt;02 | Sep 30, 2003 | 5&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2005 | 1&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2007 | 0&lt;/P&gt;&lt;P&gt;03| Mar 31, 1999 | 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An ID can have numerous Dates. RandomInteger can not be negative.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;NewField = RandomInteger&lt;/P&gt;&lt;P&gt;however, if RandomInteger for the group (ID and Date) have exceeded a threshold ( take 3 for example) then NewField is assigned to 3 for subsequent rows that belong to the same ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID | Date | RandomInteger |NewField&lt;/P&gt;&lt;P&gt;01 | Jul 1, 2000 | 1 | 1&lt;/P&gt;&lt;P&gt;01 | Aug 15, 2001 | 0 | 0&lt;/P&gt;&lt;P&gt;02 | Aug 30, 2000 | 0 | 0&lt;/P&gt;&lt;P&gt;02| Aug 31, 2001 | 2 | 2&lt;/P&gt;&lt;P&gt;02 | Sep 30, 2003 | 5 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2005 | 1 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;02 | Sep 15, 2007 | 0 | &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;03| Mar 31, 1999 | 0 | 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 00:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Field-based-on-other-variables-until-next-group/m-p/899699#M355578</guid>
      <dc:creator>sasuser1234567</dc:creator>
      <dc:date>2023-10-24T00:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Field based on other variables until next group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Field-based-on-other-variables-until-next-group/m-p/899706#M355582</link>
      <description>&lt;P&gt;Using an additional flag variable to help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dlm='|';
  input ID :$2. Date :$12. RandomInteger;
  cards;
01 | Jul 1, 2000  | 1
01 | Aug 15, 2001 | 0
02 | Aug 30, 2000 | 0
02 | Aug 31, 2001 | 2
02 | Sep 30, 2003 | 5
02 | Sep 15, 2005 | 1
02 | Sep 15, 2007 | 0
03 | Mar 31, 1999 | 0
;

data want;
  set have;
  by id;

  retain NewField _3_happend;
  if first.id then call missing(newfield,_3_happend);
  if RandomInteger&amp;gt;3 then _3_happend=1;

  if RandomInteger&amp;lt;=3 and _3_happend=. then NewField=RandomInteger;
  else NewField=3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89044i141315BBA98AD739/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 02:03:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Field-based-on-other-variables-until-next-group/m-p/899706#M355582</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-10-24T02:03:42Z</dc:date>
    </item>
  </channel>
</rss>

