<?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: Getting the average two by two in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783657#M249939</link>
    <description>&lt;P&gt;I am loosing row number one here, is there a way that i don't loose it?&lt;/P&gt;</description>
    <pubDate>Thu, 02 Dec 2021 15:24:01 GMT</pubDate>
    <dc:creator>msf2021</dc:creator>
    <dc:date>2021-12-02T15:24:01Z</dc:date>
    <item>
      <title>Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783578#M249884</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I have a dataset with 2 columns :&lt;/P&gt;&lt;PRE&gt;percentile	lift
1	        13,14
2	        4,59
3	        3,91
4	        2,70
5	        2,70
6	        2,47
7	        2,61
8	        2,20
9	        2,38
10	        1,84&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create the average two by two, like this:&lt;/P&gt;&lt;P&gt;1st : average of percentile 1 and 2&lt;/P&gt;&lt;P&gt;2nd: average of percentile 3 and 4&lt;/P&gt;&lt;P&gt;3rd: average of percentile 5 and 6&lt;/P&gt;&lt;P&gt;....:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;percentile lift   average&lt;BR /&gt;1         13,14   8,86&lt;BR /&gt;2          4,59 &lt;BR /&gt;3          3,91   3,31&lt;BR /&gt;4          2,70 &lt;BR /&gt;5          2,70   2,59&lt;BR /&gt;6          2,47 &lt;BR /&gt;7          2,61   2,41&lt;BR /&gt;8          2,20 &lt;BR /&gt;9          2,38   2,11&lt;BR /&gt;10         1,84 &lt;/PRE&gt;&lt;P&gt;Can anyone help me please?&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 10:51:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783578#M249884</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T10:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783582#M249885</link>
      <description>&lt;P&gt;Use the MOD function to determine every second observation, and use the LAG() function within IFN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input percentile lift :commax10.;
datalines;
1	        13,14
2	        4,59
3	        3,91
4	        2,70
5	        2,70
6	        2,47
7	        2,61
8	        2,20
9	        2,38
10	        1,84
;

data want;
set have;
average = ifn(mod(_N_,2)=0,(lift + lag(lift)) / 2,.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;LAG within IFN is necessary to make sure that the LAG function is executed in every data step iteration.&lt;/P&gt;
&lt;P&gt;Alternatively, you can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
average = (lift + lag(lift)) / 2;
if mod(_n_,2) = 1 then average = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 11:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783582#M249885</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T11:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783585#M249886</link>
      <description>&lt;P&gt;Thanks! My i ask how can i do a flag=1 until average is below 1? but flag=1 must be in all rows, even the ones with ".", until the average is below 1&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 11:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783585#M249886</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T11:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783588#M249889</link>
      <description>&lt;P&gt;Add this to the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain flag 1;
if average ne . and average &amp;lt; 1 then flag = 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The RETAIN initializes the variable to 1; this is kept until the condition is satisfied.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 11:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783588#M249889</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T11:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783592#M249892</link>
      <description>&lt;P&gt;Thanks again for the explanaition!! Just one thing that i didn't explain:&lt;/P&gt;&lt;P&gt;I would like that the flag is set to 1 only for rows that make the average &amp;gt;=1 so for example in last two rows, flag should be zero because the average of last two lift's is &amp;lt;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;LIFT	flag	average
1,83	1	
1,53	1	1,675643327
1,05	1	
1,38	1	1,211849192
1,23	1	
1,17	1	1,196888091
1,44	1	
1,08	1	1,256732496
0,99	1	
1,26	1	1,122082585
1,29	1	
1,41	1	1,346499102
&lt;FONT color="#993300"&gt;0,78	1	
0,96	0	0,867743866&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 12:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783592#M249892</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T12:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783636#M249928</link>
      <description>&lt;P&gt;So we need to do a look-ahead also:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input lift :commax10.;
datalines;
1,83
1,53
1,05
1,38
1,23
1,17
1,44
1,08
0,99
1,26
1,29
1,41
0,78
0,96
;

data want;
merge
  have
  have (firstobs=2 rename=(lift=_lift))
;
average = ifn(mod(_N_,2)=0,(lift + lag(lift)) / 2,.);
retain flag 1;
if average ne . and average &amp;lt; 1 then flag = 0;
if average = . and (lift + _lift) / 2 &amp;lt; 1 then flag = 0;
drop _lift;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 14:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783636#M249928</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T14:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783648#M249935</link>
      <description>&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 15:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783648#M249935</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T15:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783657#M249939</link>
      <description>&lt;P&gt;I am loosing row number one here, is there a way that i don't loose it?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 15:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783657#M249939</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T15:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783666#M249945</link>
      <description>&lt;P&gt;The data step reads all observations and writes them, log:&lt;/P&gt;
&lt;PRE&gt; NOTE: There were 14 observations read from the data set WORK.HAVE.
 NOTE: There were 13 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.WANT has 14 observations and 3 variables.
&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 15:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783666#M249945</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T15:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783678#M249952</link>
      <description>&lt;P&gt;If we have another column it starts in the second one the output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input lift :commax10.;
datalines;
1,83
1,53
1,05
1,38
1,23
1,17
1,44
1,08
0,99
1,26
1,29
1,41
0,78
0,96
;

data have2;
set have;
i=_n_;
run;

data want;
merge
  have2
  have2 (firstobs=2 rename=(lift=_lift))
;
average = ifn(mod(_N_,2)=0,(lift + lag(lift)) / 2,.);
retain flag 1;
if average ne . and average &amp;lt; 1 then flag = 0;
if average = . and (lift + _lift) / 2 &amp;lt; 1 then flag = 0;
drop _lift;
run;&lt;/PRE&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;PRE&gt;lift	i	average	flag
1.83	2	.	1
1.53	3	1.68	1
1.05	4	.	1
1.38	5	1.215	1
1.23	6	.	1
1.17	7	1.2	1
1.44	8	.	1
1.08	9	1.26	1
0.99	10	.	1
1.26	11	1.125	1
1.29	12	.	1
1.41	13	1.35	1
0.78	14	.	0
0.96	14	0.87	0&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 16:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783678#M249952</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2021-12-02T16:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Getting the average two by two</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783730#M249989</link>
      <description>&lt;P&gt;Add a KEEP= option for the look ahead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;have2 (firstobs=2 keep=lift rename=(lift=_lift))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 18:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-the-average-two-by-two/m-p/783730#M249989</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T18:46:12Z</dc:date>
    </item>
  </channel>
</rss>

