<?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: Creating variable by calling values in the rows above and below current row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849545#M335852</link>
    <description>&lt;P&gt;It is easier to look back than ahead in SAS code.&lt;/P&gt;
&lt;P&gt;One way to approach is to roll the values out into an array and then scan that to apply your rules.&lt;/P&gt;
&lt;P&gt;If I assume that for RULE 2 you actually meant to scan the 2 weeks before and 2 weeks after the current week for run lengths of 2 or more weeks then this code replicates rule 1 and rule2.&amp;nbsp; The other rules are just simple modifications to the rule 2 rule.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array counts[102] ;
  do week=1 by 1 until(last.group);
     set have ;
     by group week;
     counts[week]=count;
  end;
  maxweek=week;
  do week=1 to maxweek ;
     count=counts[week];
     rule=.;
     if 3 &amp;lt;= count then rule=1; 
     if missing(rule) then do;
       runlength=0;
       do check=max(1,week-2) to min(maxweek,week+2) until(runlength&amp;gt;1);
          if 2 &amp;lt;= counts[check] then runlength+1;
          else runlength=0;
       end;
       if runlength &amp;gt; 1 then rule=2;
     end;
     if missing(rule) then do;
       * similar logic for rule 3 ;
     end;
     output;
  end;
  keep group week rule ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Dec 2022 22:28:19 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-12-13T22:28:19Z</dc:date>
    <item>
      <title>Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849480#M335837</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am tying to create a new variable that applies a set of rules to an ordered variable in my dataset. We apply the rules to each row of a variable successively. If rule 1 fails, we move on to rule 2, if rule 2 fails we move on to rule 3 if rule 3 fails we move on to rule 4 and if rule 4 fails we stop applying rules at that row and move on to the next group.&lt;/P&gt;&lt;P&gt;My data consists of weekly counts by group. The idea is to apply the rules successively to the main row plus the rows above and below for each group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The rules go like this:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Rule 1 applies when count =&amp;gt; 3 (easy)&lt;/LI&gt;&lt;LI&gt;Rule 2 applies when 2/3 successive rows containing the main row are =&amp;gt; 2 (i.e. main row + 2 rows above OR main row + 1 row above and 1 row below OR main row + 2 rows below)&lt;/LI&gt;&lt;LI&gt;Rule 3 applies when 4/5 successive rows containing the main row are =&amp;gt; 1 (i.e. main row + 4 rows above OR main row + 3 rows above and 1 row below OR main row + 2 rows above and 2 rows below.. etc. etc.)&lt;/LI&gt;&lt;LI&gt;Rule 4 applies when 8/8 successive rows contain the main row are =&amp;gt; 0 (i.e. main row + 7 rows above OR main row + 6 rows above and 1 row below OR main row + 5 rows above and 2 rows below.. etc. etc.)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;My main issue here is that I'm not sure how to indicate to sas that I want to call the rows above/below my main row..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example of my data. I want to apply the rule to the count column and then create a new column called "rule" that applies the rules.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input group count rule;&lt;BR /&gt;datalines;
1 14.81 1 
1 69.33 1
1 17.70 1 
1 7.5 1 
1 3.1 1 
1 5.41 1 
1 1.75 2
1 1.97 2
1 3.12 1 
1 2.66 2
1 0.83 2
1 1.06 3
1 0.60 4
1 -0.30 0 
1 1.75 0 
1 -0.8 0 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;My SAS skills are proficient enough to get the job done but still fairly rudimentary, so any help or direction would appreciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2022 18:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849480#M335837</guid>
      <dc:creator>ldavis020</dc:creator>
      <dc:date>2022-12-13T18:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849493#M335842</link>
      <description>&lt;P&gt;You want this to work by group right? Even though you only have one group in your data?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2022 19:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849493#M335842</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-13T19:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849503#M335845</link>
      <description>&lt;P&gt;Yes sorry, I want to apply the rules by group. So there would be more lines in the data with additional groups.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2022 20:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849503#M335845</guid>
      <dc:creator>ldavis020</dc:creator>
      <dc:date>2022-12-13T20:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849545#M335852</link>
      <description>&lt;P&gt;It is easier to look back than ahead in SAS code.&lt;/P&gt;
&lt;P&gt;One way to approach is to roll the values out into an array and then scan that to apply your rules.&lt;/P&gt;
&lt;P&gt;If I assume that for RULE 2 you actually meant to scan the 2 weeks before and 2 weeks after the current week for run lengths of 2 or more weeks then this code replicates rule 1 and rule2.&amp;nbsp; The other rules are just simple modifications to the rule 2 rule.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array counts[102] ;
  do week=1 by 1 until(last.group);
     set have ;
     by group week;
     counts[week]=count;
  end;
  maxweek=week;
  do week=1 to maxweek ;
     count=counts[week];
     rule=.;
     if 3 &amp;lt;= count then rule=1; 
     if missing(rule) then do;
       runlength=0;
       do check=max(1,week-2) to min(maxweek,week+2) until(runlength&amp;gt;1);
          if 2 &amp;lt;= counts[check] then runlength+1;
          else runlength=0;
       end;
       if runlength &amp;gt; 1 then rule=2;
     end;
     if missing(rule) then do;
       * similar logic for rule 3 ;
     end;
     output;
  end;
  keep group week rule ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Dec 2022 22:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849545#M335852</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-13T22:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849687#M335878</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;-- this seems to work with the example data! I haven't applied it to my own dataset yet.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to clarify what's happening here. You're essentially creating a new variable that represents the value for each week in each group (e.g. variable count23 = value for week 23 in each group). Then we can go scan across each of the variables and apply the rules instead of scanning through one variable vertically.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2022 16:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849687#M335878</guid>
      <dc:creator>ldavis020</dc:creator>
      <dc:date>2022-12-14T16:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating variable by calling values in the rows above and below current row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849692#M335881</link>
      <description>&lt;P&gt;So essentially it is making a temporary array of all of the values for this group.&lt;/P&gt;
&lt;P&gt;The first DO loop loads the array.&amp;nbsp; Then the second pulls the individual values back out and so the rule checking can be down with the values in the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2022 16:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-variable-by-calling-values-in-the-rows-above-and-below/m-p/849692#M335881</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-14T16:55:36Z</dc:date>
    </item>
  </channel>
</rss>

