<?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: Need to create a flag variable based on Ref &amp;amp;amp; ID when UNIT is different. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909885#M358842</link>
    <description>&lt;P&gt;"&lt;SPAN&gt;dropped the &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;card deck&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;How long it has been!&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 29 Dec 2023 04:49:33 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-12-29T04:49:33Z</dc:date>
    <item>
      <title>Need to create a flag variable based on Ref and ID when UNIT is different.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909862#M358836</link>
      <description>&lt;P&gt;When Ref = 10 for particular ID = 1 then UNIT is expected to be same as the first record.&lt;/P&gt;&lt;P&gt;In below scenario, when Ref = 20 we see different value as C in 2nd row.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expectation is to create a Flag variable when we have different values compared to 1st record within a same Ref and ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ref&amp;nbsp; &amp;nbsp; ID&amp;nbsp; &amp;nbsp;UNIT&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Kg&lt;/P&gt;&lt;P&gt;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Kg&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;</description>
      <pubDate>Thu, 28 Dec 2023 19:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909862#M358836</guid>
      <dc:creator>AwesomeSAS</dc:creator>
      <dc:date>2023-12-28T19:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a flag variable based on Ref &amp;amp; ID when UNIT is different.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909865#M358839</link>
      <description>&lt;P&gt;Not sure how&amp;nbsp; you can tell which is "first" since there does not seem to a date or other ordering variable.&amp;nbsp; And your listing is NOT currently sorted by REF and ID.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But assuming that you haven't dropped the card deck somewhere along the way and you want to process the groups as they appear in the data you can use the NOTSORTED keyword on the BY statement to tell SAS to create the by groups based on the current ordering.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your listing into an actual SAS dataset so we have something to program against.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Ref ID UNIT $;
cards;
10 1 C
10 1 C
10 1 C
20 1 Kg
20 1 C
10 2 Kg
10 2 C
10 2 C
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just use BY group processing and a new RETAINed variable to remember the first value.&amp;nbsp; Then we can compare the two variables to make a new "flag" variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by ref id notsorted;
  if first.id then first_unit=unit;
  retain first_unit;
  flag = (unit = first_unit);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                            first_
OBS    Ref    ID    UNIT     unit     flag

 1      10     1     C        C         1
 2      10     1     C        C         1
 3      10     1     C        C         1
 4      20     1     Kg       Kg        1
 5      20     1     C        Kg        0
 6      10     2     Kg       Kg        1
 7      10     2     C        Kg        0
 8      10     2     C        Kg        0

&lt;/PRE&gt;
&lt;P&gt;If that is not the output you wanted then provide a more complete description of the algorithm and the output you expect for the sample input you provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Dec 2023 19:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909865#M358839</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-28T19:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a flag variable based on Ref &amp;amp; ID when UNIT is different.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909885#M358842</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;dropped the &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;card deck&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;How long it has been!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Dec 2023 04:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-flag-variable-based-on-Ref-and-ID-when-UNIT-is/m-p/909885#M358842</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-29T04:49:33Z</dc:date>
    </item>
  </channel>
</rss>

