<?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: average of consecutive numbers in a column in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69631#M20013</link>
    <description>Hi.Peter.&lt;BR /&gt;
I know the usage of 'merge' statement without 'by' statement,But I forget the dataset option 'firstobs' which can do it. It enlight my brain, and give me one more approach to process these data.Thanks.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Best Regards.&lt;BR /&gt;
Ksharp</description>
    <pubDate>Fri, 18 Feb 2011 06:27:05 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-02-18T06:27:05Z</dc:date>
    <item>
      <title>average of consecutive numbers in a column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69627#M20009</link>
      <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
I have a set of data given to me by a student who entered the data in the following format:&lt;BR /&gt;
&lt;BR /&gt;
sub$	d	v&lt;BR /&gt;
a	5	2&lt;BR /&gt;
a	19	3&lt;BR /&gt;
a	20	2&lt;BR /&gt;
a	21	5&lt;BR /&gt;
a	23	2&lt;BR /&gt;
a	18	3&lt;BR /&gt;
a	9	.&lt;BR /&gt;
b	6	2&lt;BR /&gt;
b	18	1&lt;BR /&gt;
b	19	3&lt;BR /&gt;
b	18	4&lt;BR /&gt;
b	11	2&lt;BR /&gt;
b	3	.&lt;BR /&gt;
&lt;BR /&gt;
I need to take the average of two consecutive numbers in column 'd' and put them in a new column so that I can multiply them by column 'v' for each of the subjects (column 'sub$').&lt;BR /&gt;
The result of taking the average (ave) should look like this:&lt;BR /&gt;
&lt;BR /&gt;
sub$	d	v	ave&lt;BR /&gt;
a	5	2	12&lt;BR /&gt;
a	19	3	19.5&lt;BR /&gt;
a	20	2	20.5&lt;BR /&gt;
a	21	5	22&lt;BR /&gt;
a	23	2	20.5&lt;BR /&gt;
a	18	3	13.5&lt;BR /&gt;
a	9	.	0&lt;BR /&gt;
b	6	2	12&lt;BR /&gt;
b	18	1	18.5&lt;BR /&gt;
b	19	3	18.5&lt;BR /&gt;
b	18	4	14.5&lt;BR /&gt;
b	11	2	7&lt;BR /&gt;
b	3	.	0&lt;BR /&gt;
&lt;BR /&gt;
My first thought was to transpose the data (I have other variables that can be used to transpose each row into a column for all subjects), and apply the operations on the newly created variables.  But if there is an easier way, I would be interested to learn; I have spent a day looking through online documentation.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance.</description>
      <pubDate>Sun, 06 Feb 2011 16:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69627#M20009</guid>
      <dc:creator>peatjohnston</dc:creator>
      <dc:date>2011-02-06T16:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: average of consecutive numbers in a column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69628#M20010</link>
      <description>here is a simple merge-without-by&lt;BR /&gt;
data t1 ;&lt;BR /&gt;
input sub $ d v ;&lt;BR /&gt;
list; datalines;&lt;BR /&gt;
a 5 2&lt;BR /&gt;
a 19 3&lt;BR /&gt;
a 20 2&lt;BR /&gt;
a 21 5&lt;BR /&gt;
a 23 2&lt;BR /&gt;
a 18 3&lt;BR /&gt;
a 9 .&lt;BR /&gt;
b 6 2&lt;BR /&gt;
b 18 1&lt;BR /&gt;
b 19 3&lt;BR /&gt;
b 18 4&lt;BR /&gt;
b 11 2&lt;BR /&gt;
b 3 .&lt;BR /&gt;
;&lt;BR /&gt;
data aves ;&lt;BR /&gt;
 merge t1 t1( firstobs=2 keep= sub d rename=( sub=sub2 d=d2 ))  ;&lt;BR /&gt;
 * notice, no By statement ;&lt;BR /&gt;
 if sub ne sub2 then do ; &lt;BR /&gt;
   * handle last for SUB   ;&lt;BR /&gt;
   ave = 0 ;&lt;BR /&gt;
 end ;&lt;BR /&gt;
 else &lt;BR /&gt;
   ave = (d + d2 ) /2 ;&lt;BR /&gt;
drop sub2 d2 ;&lt;BR /&gt;
put ( sub d v ave )( $3. 3*best6. );&lt;BR /&gt;
run ; &lt;BR /&gt;
which puts this report into the log[pre]a       5     2    12&lt;BR /&gt;
a      19     3  19.5&lt;BR /&gt;
a      20     2  20.5&lt;BR /&gt;
a      21     5    22&lt;BR /&gt;
a      23     2  20.5&lt;BR /&gt;
a      18     3  13.5&lt;BR /&gt;
a       9     .     0&lt;BR /&gt;
b       6     2    12&lt;BR /&gt;
b      18     1  18.5&lt;BR /&gt;
b      19     3  18.5&lt;BR /&gt;
b      18     4  14.5&lt;BR /&gt;
b      11     2     7&lt;BR /&gt;
b       3     .     0&lt;BR /&gt;
NOTE: There were 12 observations read from the data set WORK.T1.&lt;BR /&gt;
NOTE: There were 13 observations read from the data set WORK.T1.&lt;BR /&gt;
NOTE: The data set WORK.AVES has 13 observations and 4 variables.&lt;BR /&gt;
NOTE: DATA statement used[/pre]Without the BY statement the data step merges the "next" row columns SUB and D - renamed to ensure the step has next D in a different name. With no BY statement there is no LAST.SUB available so SUB2 (the next SUB) clarifies the end of a SUB when these differ.&lt;BR /&gt;
 &lt;BR /&gt;
The next stage AVE * V could be done in the same step, but you want to work on that yourself...... &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
      <pubDate>Sun, 06 Feb 2011 16:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69628#M20010</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-02-06T16:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: average of consecutive numbers in a column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69629#M20011</link>
      <description>Hi.&lt;BR /&gt;
Peter.C 's code is great.&lt;BR /&gt;
Peter ,I have not think 'merge' statement can be used in that way. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
input sub$ d v;&lt;BR /&gt;
datalines;&lt;BR /&gt;
a 5 2&lt;BR /&gt;
a 19 3&lt;BR /&gt;
a 20 2&lt;BR /&gt;
a 21 5&lt;BR /&gt;
a 23 2&lt;BR /&gt;
a 18 3&lt;BR /&gt;
a 9 .&lt;BR /&gt;
b 6 2&lt;BR /&gt;
b 18 1&lt;BR /&gt;
b 19 3&lt;BR /&gt;
b 18 4&lt;BR /&gt;
b 11 2&lt;BR /&gt;
b 3 .&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 by sub;&lt;BR /&gt;
 ave=mean(lag(d),d);&lt;BR /&gt;
 if not first.sub  then do; &lt;BR /&gt;
                  obs=_n_-1;&lt;BR /&gt;
                  set temp point=obs;&lt;BR /&gt;
                  output;&lt;BR /&gt;
                  end;&lt;BR /&gt;
 if last.sub then do;&lt;BR /&gt;
              ave=0;&lt;BR /&gt;
              set temp point=_n_;&lt;BR /&gt;
              output;&lt;BR /&gt;
              end;  &lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Thu, 17 Feb 2011 05:14:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69629#M20011</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-17T05:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: average of consecutive numbers in a column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69630#M20012</link>
      <description>thank you kSharp&lt;BR /&gt;
it is the classic "read-ahead"&lt;BR /&gt;
should be around the archives in more than one posting&lt;BR /&gt;
frequently brought out when a poster wants LEAD instead of LAG&lt;BR /&gt;
 &lt;BR /&gt;
peterC</description>
      <pubDate>Thu, 17 Feb 2011 17:26:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69630#M20012</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-02-17T17:26:07Z</dc:date>
    </item>
    <item>
      <title>Re: average of consecutive numbers in a column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69631#M20013</link>
      <description>Hi.Peter.&lt;BR /&gt;
I know the usage of 'merge' statement without 'by' statement,But I forget the dataset option 'firstobs' which can do it. It enlight my brain, and give me one more approach to process these data.Thanks.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Best Regards.&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 18 Feb 2011 06:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/average-of-consecutive-numbers-in-a-column/m-p/69631#M20013</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-18T06:27:05Z</dc:date>
    </item>
  </channel>
</rss>

