<?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: Recode First Observations in a Group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405639#M279155</link>
    <description>&lt;P&gt;When you use a by statement in a datastep SAS is expecting that the data had been sorted in the order shown in the by statement.&lt;/P&gt;
&lt;P&gt;In your case if locacde is the variable that identifies group, and the data have already been sorted in the order shown in your previous by statement, all you would need is:&lt;/P&gt;
&lt;PRE&gt;data op23a_04;
  set op23a_03;
  by locacde;
  if (occupied = 'N' and first.locacde = 1) then occupied = 'Y';
run;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Thu, 19 Oct 2017 16:28:05 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-10-19T16:28:05Z</dc:date>
    <item>
      <title>Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405389#M279150</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having difficulty recoding the first observation in a group. Basically I need it to recode the occupied variable to a Y when both conditions are met. What I'm getting is all variables recoded to Y where the first condition is met (occupied = 'N').&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see the attachment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for any help out there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_04;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_03;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; locacde number count_date occupied;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (occupied = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'N'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; first.occupied = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; occupied = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Y'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 00:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405389#M279150</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2017-10-19T00:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405395#M279151</link>
      <description>&lt;P&gt;What identifies a group?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 01:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405395#M279151</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-10-19T01:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405404#M279152</link>
      <description>&lt;P&gt;Since OCCUPIED is a BY variable, it must be part of the incoming data set.&amp;nbsp; So how do you know that it changed at all?&amp;nbsp; Maybe it always was Y and just remained Y.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 03:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405404#M279152</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-19T03:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405413#M279153</link>
      <description>&lt;P&gt;Since coumt_date is changing in each row and is part of BY statement,&lt;/P&gt;
&lt;P&gt;then each row is defined as a group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you are looking for seems that you need to drop count_date from the BY saement.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 04:45:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405413#M279153</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-10-19T04:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405599#M279154</link>
      <description>&lt;P&gt;Ah hah! Now that makes all kind of sense. No wonder each value of occupied changes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without count date though how would I keep them in order to be sure only the first occurrence, in date order, of occupied is recoded? If I don't order them by group and place it in the BY statement wouldn't they just be randomly ordered and I'd be selecting some random observation and recoding it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, I'm more than a bit new at this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 15:19:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405599#M279154</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2017-10-19T15:19:20Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405639#M279155</link>
      <description>&lt;P&gt;When you use a by statement in a datastep SAS is expecting that the data had been sorted in the order shown in the by statement.&lt;/P&gt;
&lt;P&gt;In your case if locacde is the variable that identifies group, and the data have already been sorted in the order shown in your previous by statement, all you would need is:&lt;/P&gt;
&lt;PRE&gt;data op23a_04;
  set op23a_03;
  by locacde;
  if (occupied = 'N' and first.locacde = 1) then occupied = 'Y';
run;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 16:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405639#M279155</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-10-19T16:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405826#M279156</link>
      <description>&lt;P&gt;You can keep the data sorted as you want but define the group without the count_date&lt;/P&gt;
&lt;P&gt;like in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by locacde number count_date occupied;
run;

data op23a_04;
set op23a_03;
by locacde number;
if (occupied = 'N' and first.number = 1) then occupied = 'Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2017 00:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/405826#M279156</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-10-20T00:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406755#M279157</link>
      <description>&lt;P&gt;Sorry but I'm still having issues here. I'll attach a new file as an example. What I need is to flag the first occurrence of an unoccupied location if that unoccupied location is the first unoccupied record in that room. What I keep getting is either flagging nothing anywhere or flagging each unoccupied location (all OCCUPIED = N) across all the data regardless of if it's the first occurrence or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you look to row 97 and 98, it would be row 97 I would want flagged as removable but not row 98 since that is the second occurrence of the same room flagged as unoccupied. It was unoccupied for two days but we only want to remove the first day. Row 103 should also be flagged since it is the first occurrence of&amp;nbsp;E1-11B where it shows&amp;nbsp;not occupied. Row 275 should also be flagged but not row 276 since that is the second occurrence of E2-01B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the current version of my step (that doesn't work).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would sure appreciate any help. Sorry about being a bit less than clear before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_04;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_03;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; location unit room;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (occupied = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'N'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; first.occupied = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; deletion_flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Y'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 22:12:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406755#M279157</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2017-10-23T22:12:24Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406758#M279158</link>
      <description>&lt;P&gt;I think that the following will do what you want to achieve:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data op23a_04;
  set op23a_03;
  by location unit room record notsorted;
  if (occupied = 'N' and missing(record) and first.record) then do;
    deletion_flag = 'Y';
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 22:44:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406758#M279158</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-10-23T22:44:52Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406763#M279159</link>
      <description>&lt;P&gt;I so wish I could tell you that worked....but I don't get anything different. All I get is a new variable called "deletion_flag" (which I should) but no values across the entire variable. The entire column is blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a tough one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_04;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; op23a_03;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; location unit&amp;nbsp;room notsorted;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (occupied = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'N'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; missing(record) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; first.record) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; deletion_flag = &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'Y'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 23:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406763#M279159</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2017-10-23T23:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406769#M279160</link>
      <description>&lt;P&gt;That is NOT the code I suggested. If you're going to deviate from what is suggested then, of course, it might not work!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I imported your workbook and the code worked perfectly for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 23:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406769#M279160</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-10-23T23:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recode First Observations in a Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406775#M279161</link>
      <description>&lt;P&gt;My apologies for being new to this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help. It worked.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2017 00:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recode-First-Observations-in-a-Group/m-p/406775#M279161</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2017-10-24T00:02:32Z</dc:date>
    </item>
  </channel>
</rss>

