<?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 Selecting (and averaging) the first few and last few records of a dataset in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Selecting-and-averaging-the-first-few-and-last-few-records-of-a/m-p/64066#M6417</link>
    <description>Hello all SAS guru's&lt;BR /&gt;
&lt;BR /&gt;
I have a data set that has 4 columns.&lt;BR /&gt;
&lt;BR /&gt;
PART_NUM (character)&lt;BR /&gt;
SUB_ASSEMBLY (numeric)&lt;BR /&gt;
DATE (date)&lt;BR /&gt;
VALUE (numeric)&lt;BR /&gt;
&lt;BR /&gt;
What I'm looking to do is get an average of VALUE from the first 2 and an average of VALUE from the last 2 records of the data set based on DATE and SUB_ASSEMBLY for each PART_NUM.&lt;BR /&gt;
Each part_num can have a different number of sub_assembly values.  &lt;BR /&gt;
&lt;BR /&gt;
For example if the data set was: (data is in columns.  doesn't copy very well with just text)&lt;BR /&gt;
PART_NUM	SUB_ASSEMBLY	DATE	VALUE&lt;BR /&gt;
A	0	3/1/2010	5&lt;BR /&gt;
A	1	3/1/2010	4&lt;BR /&gt;
A	2	3/1/2010	5&lt;BR /&gt;
A	0	3/2/2010	4&lt;BR /&gt;
A	1	3/2/2010	4&lt;BR /&gt;
A	2	3/2/2010	5&lt;BR /&gt;
B	0	3/1/2010	5&lt;BR /&gt;
B	1	3/1/2010	4&lt;BR /&gt;
B	0	3/2/2010	4&lt;BR /&gt;
B	1	3/2/2010	4&lt;BR /&gt;
...&lt;BR /&gt;
A	0	3/20/2010	6&lt;BR /&gt;
A	1	3/20/2010	7&lt;BR /&gt;
A	2	3/20/2010	4&lt;BR /&gt;
A	0	3/21/2010	4&lt;BR /&gt;
A	1	3/21/2010	5&lt;BR /&gt;
A	2	3/21/2010	3&lt;BR /&gt;
B	0	3/20/2010	6&lt;BR /&gt;
B	1	3/20/2010	7&lt;BR /&gt;
B	0	3/21/2010	3&lt;BR /&gt;
B	1	3/21/2010	5&lt;BR /&gt;
&lt;BR /&gt;
This would yield a table with 4 columns with unique PART_NUM, SUB_ASSEMBLY and two "averaged" columns.&lt;BR /&gt;
PART_NUM	SUB_ASSEMBLY	Average(First2) Average(Second2)&lt;BR /&gt;
A	0	4.5	5.0&lt;BR /&gt;
A	1	4.0	6.0&lt;BR /&gt;
A	2	5.0	3.5&lt;BR /&gt;
B	0	4.5	4.5&lt;BR /&gt;
B	1	4.0	6.0&lt;BR /&gt;
&lt;BR /&gt;
As an example, the first row of the output table with PART_NUM "A" and SUB_ASSEMBLY "0" &lt;BR /&gt;
	 the average(First 2) is the avg of 5 on 3/1/2010 and 4 on 3/2/2010&lt;BR /&gt;
	the average(Second 2) is the avg of 6 on 3/20/2010 and 3 on 3/20/2010&lt;BR /&gt;
&lt;BR /&gt;
I am using Enterprise Guide and was hoping that there was a way to do this within that environment.  I can't figure one out.  &lt;BR /&gt;
So I'm hoping that some somebody either knows how or can give some advice on SAS code that will solve this.&lt;BR /&gt;
&lt;BR /&gt;
I'm also hoping I explained this clearly enough.&lt;BR /&gt;
&lt;BR /&gt;
thanks</description>
    <pubDate>Wed, 03 Mar 2010 14:47:48 GMT</pubDate>
    <dc:creator>GDA</dc:creator>
    <dc:date>2010-03-03T14:47:48Z</dc:date>
    <item>
      <title>Selecting (and averaging) the first few and last few records of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Selecting-and-averaging-the-first-few-and-last-few-records-of-a/m-p/64066#M6417</link>
      <description>Hello all SAS guru's&lt;BR /&gt;
&lt;BR /&gt;
I have a data set that has 4 columns.&lt;BR /&gt;
&lt;BR /&gt;
PART_NUM (character)&lt;BR /&gt;
SUB_ASSEMBLY (numeric)&lt;BR /&gt;
DATE (date)&lt;BR /&gt;
VALUE (numeric)&lt;BR /&gt;
&lt;BR /&gt;
What I'm looking to do is get an average of VALUE from the first 2 and an average of VALUE from the last 2 records of the data set based on DATE and SUB_ASSEMBLY for each PART_NUM.&lt;BR /&gt;
Each part_num can have a different number of sub_assembly values.  &lt;BR /&gt;
&lt;BR /&gt;
For example if the data set was: (data is in columns.  doesn't copy very well with just text)&lt;BR /&gt;
PART_NUM	SUB_ASSEMBLY	DATE	VALUE&lt;BR /&gt;
A	0	3/1/2010	5&lt;BR /&gt;
A	1	3/1/2010	4&lt;BR /&gt;
A	2	3/1/2010	5&lt;BR /&gt;
A	0	3/2/2010	4&lt;BR /&gt;
A	1	3/2/2010	4&lt;BR /&gt;
A	2	3/2/2010	5&lt;BR /&gt;
B	0	3/1/2010	5&lt;BR /&gt;
B	1	3/1/2010	4&lt;BR /&gt;
B	0	3/2/2010	4&lt;BR /&gt;
B	1	3/2/2010	4&lt;BR /&gt;
...&lt;BR /&gt;
A	0	3/20/2010	6&lt;BR /&gt;
A	1	3/20/2010	7&lt;BR /&gt;
A	2	3/20/2010	4&lt;BR /&gt;
A	0	3/21/2010	4&lt;BR /&gt;
A	1	3/21/2010	5&lt;BR /&gt;
A	2	3/21/2010	3&lt;BR /&gt;
B	0	3/20/2010	6&lt;BR /&gt;
B	1	3/20/2010	7&lt;BR /&gt;
B	0	3/21/2010	3&lt;BR /&gt;
B	1	3/21/2010	5&lt;BR /&gt;
&lt;BR /&gt;
This would yield a table with 4 columns with unique PART_NUM, SUB_ASSEMBLY and two "averaged" columns.&lt;BR /&gt;
PART_NUM	SUB_ASSEMBLY	Average(First2) Average(Second2)&lt;BR /&gt;
A	0	4.5	5.0&lt;BR /&gt;
A	1	4.0	6.0&lt;BR /&gt;
A	2	5.0	3.5&lt;BR /&gt;
B	0	4.5	4.5&lt;BR /&gt;
B	1	4.0	6.0&lt;BR /&gt;
&lt;BR /&gt;
As an example, the first row of the output table with PART_NUM "A" and SUB_ASSEMBLY "0" &lt;BR /&gt;
	 the average(First 2) is the avg of 5 on 3/1/2010 and 4 on 3/2/2010&lt;BR /&gt;
	the average(Second 2) is the avg of 6 on 3/20/2010 and 3 on 3/20/2010&lt;BR /&gt;
&lt;BR /&gt;
I am using Enterprise Guide and was hoping that there was a way to do this within that environment.  I can't figure one out.  &lt;BR /&gt;
So I'm hoping that some somebody either knows how or can give some advice on SAS code that will solve this.&lt;BR /&gt;
&lt;BR /&gt;
I'm also hoping I explained this clearly enough.&lt;BR /&gt;
&lt;BR /&gt;
thanks</description>
      <pubDate>Wed, 03 Mar 2010 14:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Selecting-and-averaging-the-first-few-and-last-few-records-of-a/m-p/64066#M6417</guid>
      <dc:creator>GDA</dc:creator>
      <dc:date>2010-03-03T14:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting (and averaging) the first few and last few records of a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Selecting-and-averaging-the-first-few-and-last-few-records-of-a/m-p/64067#M6418</link>
      <description>data tmp;&lt;BR /&gt;
attrib date format=ddmmyy10.;&lt;BR /&gt;
&lt;BR /&gt;
input PART_NUM $1. SUB_ASSEMBLY DATE mmddyy10.  VALUE;&lt;BR /&gt;
cards;&lt;BR /&gt;
A 0 03/01/2010 5&lt;BR /&gt;
A 1 03/01/2010 4&lt;BR /&gt;
A 2 03/01/2010 5&lt;BR /&gt;
A 0 03/02/2010 4&lt;BR /&gt;
A 1 03/02/2010 4&lt;BR /&gt;
A 2 03/02/2010 5&lt;BR /&gt;
B 0 03/01/2010 5&lt;BR /&gt;
B 1 03/01/2010 4&lt;BR /&gt;
B 0 03/02/2010 4&lt;BR /&gt;
B 1 03/02/2010 4&lt;BR /&gt;
A 0 3/20/2010 6&lt;BR /&gt;
A 1 3/20/2010 7&lt;BR /&gt;
A 2 3/20/2010 4&lt;BR /&gt;
A 0 3/21/2010 4&lt;BR /&gt;
A 1 3/21/2010 5&lt;BR /&gt;
A 2 3/21/2010 3&lt;BR /&gt;
B 0 3/20/2010 6&lt;BR /&gt;
B 1 3/20/2010 7&lt;BR /&gt;
B 0 3/21/2010 3&lt;BR /&gt;
B 1 3/21/2010 5&lt;BR /&gt;
&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=tmp;&lt;BR /&gt;
             by PART_NUM SUB_ASSEMBLY DATE;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data tmp_avg;&lt;BR /&gt;
attrib PART_NUM format=$1.&lt;BR /&gt;
             SUB_ASSEMBLY format=8.&lt;BR /&gt;
             avg_first format=comma22.2&lt;BR /&gt;
             avg_second format=comma22.2&lt;BR /&gt;
             ;&lt;BR /&gt;
             set tmp;&lt;BR /&gt;
             by PART_NUM SUB_ASSEMBLY DATE;&lt;BR /&gt;
             array var{2};&lt;BR /&gt;
             if first.PART_NUM or first.SUB_ASSEMBLY then do;&lt;BR /&gt;
                          num_obs=0;&lt;BR /&gt;
                          do i = 1 to 2;&lt;BR /&gt;
                                       var(i)=0;&lt;BR /&gt;
                          end;&lt;BR /&gt;
             end;&lt;BR /&gt;
             num_obs + 1;&lt;BR /&gt;
&lt;BR /&gt;
             if num_obs le 2 then var(1)+VALUE;&lt;BR /&gt;
             if num_obs ge 3 then var(2)+VALUE;&lt;BR /&gt;
&lt;BR /&gt;
             if last.PART_NUM or last.SUB_ASSEMBLY then do;	&lt;BR /&gt;
                          avg_first=var(1)/2;&lt;BR /&gt;
                          avg_second=var(2)/2;&lt;BR /&gt;
                          output;&lt;BR /&gt;
             end;&lt;BR /&gt;
&lt;BR /&gt;
             keep PART_NUM SUB_ASSEMBLY avg_first avg_second;&lt;BR /&gt;
&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 04 Mar 2010 13:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Selecting-and-averaging-the-first-few-and-last-few-records-of-a/m-p/64067#M6418</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-04T13:13:32Z</dc:date>
    </item>
  </channel>
</rss>

