<?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: Select rows before and after a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945963#M370515</link>
    <description>Hi Tom, I just edited my question a little bit, hopefully it is more clear now. The data is sorted by time and each group will have a 1 and only one "1" either at middle of somewhere or at the end. And I want to flag the rows before 1.</description>
    <pubDate>Wed, 02 Oct 2024 14:40:56 GMT</pubDate>
    <dc:creator>Yuna11</dc:creator>
    <dc:date>2024-10-02T14:40:56Z</dc:date>
    <item>
      <title>Select rows before a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945942#M370513</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;&lt;STRONG&gt;/*In my study, I want to flag all the rows before 1 by each group. &lt;BR /&gt;They are already sorted by time, and each group will have 9 records and will have and only have one "1" marked, and I want to select _n_ &amp;lt; rownum(when x=1) */&lt;BR /&gt;&lt;/STRONG&gt;
Data have;
  INPUT Group $ ID X;
  DATALINES;
A 01 0
A 02 0
A 03 0
A 04 0
A 05 1
A 06 0
A 07 0
A 08 0
A 09 0
B 01 0
B 02 0
B 03 0
B 04 0
B 05 0
B 06 0
B 07 0
B 08 1
B 09 0
;

Data want;
INPUT Group $ ID X Y;
DATALINES;
A 01 0 Y
A 02 0 Y
A 03 0 Y
A 04 0 Y
A 05 1 Y
A 06 0 
A 07 0 
A 08 0 
A 09 0 
B 01 0 Y
B 02 0 Y
B 03 0 Y
B 04 0 Y
B 05 0 Y
B 06 0 Y
B 07 0 Y
B 08 1 Y
B 09 0 
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 14:47:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945942#M370513</guid>
      <dc:creator>Yuna11</dc:creator>
      <dc:date>2024-10-02T14:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows before and after a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945961#M370514</link>
      <description>&lt;P&gt;What is "row index number"?&lt;/P&gt;
&lt;P&gt;Why not just start the value as Y and then change it to N after you see the first one?&amp;nbsp; That works for your two example groups.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by group x notsorted;
  if first.group then y='Y';
  else if first.x and x=0 then y='N';
  retain y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But what do you want to do with groups that never have a one?&lt;/P&gt;
&lt;P&gt;What about groups that have multiple observations with a one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 14:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945961#M370514</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-02T14:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows before and after a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945963#M370515</link>
      <description>Hi Tom, I just edited my question a little bit, hopefully it is more clear now. The data is sorted by time and each group will have a 1 and only one "1" either at middle of somewhere or at the end. And I want to flag the rows before 1.</description>
      <pubDate>Wed, 02 Oct 2024 14:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945963#M370515</guid>
      <dc:creator>Yuna11</dc:creator>
      <dc:date>2024-10-02T14:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows before and after a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945965#M370516</link>
      <description>&lt;P&gt;Please explain why if you want "also all the rows after 1 by each group. " that all the rows actually after the 1 are marked N in your "want" data set?&lt;/P&gt;
&lt;P&gt;I don't see anything in your "want" that indicates a selection of "after". So you need to very clearly describe what that would look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a general use of SAS aside: recommend use 1 for Yes/True/of a property or similar and 0 for No/False/not of a property if there are only two values. For one thing SAS treats 1/0 as true/false internally.&lt;/P&gt;
&lt;P&gt;So&lt;/P&gt;
&lt;P&gt;x = (a &amp;gt; b);&lt;/P&gt;
&lt;P&gt;assigns values of 1 to x when a is greater than b. To use Y/N letters you have to use If/Then/Else or some other more complex code. It can also be much easier to report on the 1's without including the 0s by using Sum and mean(percent of 1's).&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 14:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945965#M370516</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-02T14:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows before and after a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945969#M370517</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376186"&gt;@Yuna11&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Tom, I just edited my question a little bit, hopefully it is more clear now. The data is sorted by time and each group will have a 1 and only one "1" either at middle of somewhere or at the end. And I want to flag the rows before 1.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is even easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by group ;
  if first.group then y='Y';
  if lag(x)=1 and not first.group then y='N';
  retain y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The reason there are two IF statements instead of just an ELSE is because you need to execute the LAG() function for every observation for it to work properly.&amp;nbsp; The LAG() function remembers the values that are passed into it, so if you skip executing it for some observations then those values of X never make into the list of values that can be returned by the LAG() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also skip the LAG() function and instead insert an OUTPUT statement so you can change the value of Y after it has already been written to the output dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by group ;
  if first.group then y='Y';
  retain y;
  output;
  if x=1 then y='N';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Oct 2024 14:54:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-before-a-condition/m-p/945969#M370517</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-02T14:54:08Z</dc:date>
    </item>
  </channel>
</rss>

