<?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: Selecting First and Last record(s) from a data set in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63913#M18176</link>
    <description>I don't see how you could do this in EG without creating many repetitive processes.&lt;BR /&gt;
&lt;BR /&gt;
Here is how you could do it with a bit of code:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data T(index=(a=(PART_NUM SUB_ASSEMBLY)));&lt;BR /&gt;
  input PART_NUM $ 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 03/20/2010 6&lt;BR /&gt;
A 1 03/20/2010 7&lt;BR /&gt;
A 2 03/20/2010 4&lt;BR /&gt;
A 0 03/21/2010 4&lt;BR /&gt;
A 1 03/21/2010 5&lt;BR /&gt;
A 2 03/21/2010 3&lt;BR /&gt;
B 0 03/20/2010 6&lt;BR /&gt;
B 1 03/20/2010 7&lt;BR /&gt;
B 0 03/21/2010 3&lt;BR /&gt;
B 1 03/21/2010 5&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=T out=LIST nodupkey;            * build table of all calculation groups;&lt;BR /&gt;
  by PART_NUM SUB_ASSEMBLY ;&lt;BR /&gt;
data END;&lt;BR /&gt;
  set LIST;                                    * for each calculation group;&lt;BR /&gt;
  OBS=0;                                       * reset observation counter;&lt;BR /&gt;
  do until (LASTOBS);                          * start iteration for current group;                          &lt;BR /&gt;
    set T end=LASTOBS key=A;                   * read data;     &lt;BR /&gt;
    OBS+1;                                     * count observations;&lt;BR /&gt;
    if OBS=2 then AVG1=mean(VALUE,PREV_VALUE); * set avg for first 2 observations;&lt;BR /&gt;
    if LASTOBS then output;                    * no more observations: save averages;&lt;BR /&gt;
    AVG2=mean(VALUE,PREV_VALUE);               * set avg for latest 2 observations;&lt;BR /&gt;
    PREV_VALUE=VALUE;                          * remember value for next iteration; &lt;BR /&gt;
  end;&lt;BR /&gt;
  if _IORC_=1230015 then _ERROR_=0;            * reset error flag ;&lt;BR /&gt;
  keep PART_NUM SUB_ASSEMBLY AVG1 AVG2;       &lt;BR /&gt;
run;</description>
    <pubDate>Mon, 08 Mar 2010 20:44:01 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2010-03-08T20:44:01Z</dc:date>
    <item>
      <title>Selecting First and Last record(s) from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63911#M18174</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 01:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63911#M18174</guid>
      <dc:creator>GDA</dc:creator>
      <dc:date>2010-03-03T01:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting First and Last record(s) from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63912#M18175</link>
      <description>Hi:&lt;BR /&gt;
  You might consider posting your question in the SAS Enterprise Guide forum. Most folks in this forum use SAS code to accomplish their tasks and if you're looking for a point and click approach, the EG forum would be the place for this question.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 03 Mar 2010 06:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63912#M18175</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-03-03T06:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting First and Last record(s) from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63913#M18176</link>
      <description>I don't see how you could do this in EG without creating many repetitive processes.&lt;BR /&gt;
&lt;BR /&gt;
Here is how you could do it with a bit of code:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data T(index=(a=(PART_NUM SUB_ASSEMBLY)));&lt;BR /&gt;
  input PART_NUM $ 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 03/20/2010 6&lt;BR /&gt;
A 1 03/20/2010 7&lt;BR /&gt;
A 2 03/20/2010 4&lt;BR /&gt;
A 0 03/21/2010 4&lt;BR /&gt;
A 1 03/21/2010 5&lt;BR /&gt;
A 2 03/21/2010 3&lt;BR /&gt;
B 0 03/20/2010 6&lt;BR /&gt;
B 1 03/20/2010 7&lt;BR /&gt;
B 0 03/21/2010 3&lt;BR /&gt;
B 1 03/21/2010 5&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=T out=LIST nodupkey;            * build table of all calculation groups;&lt;BR /&gt;
  by PART_NUM SUB_ASSEMBLY ;&lt;BR /&gt;
data END;&lt;BR /&gt;
  set LIST;                                    * for each calculation group;&lt;BR /&gt;
  OBS=0;                                       * reset observation counter;&lt;BR /&gt;
  do until (LASTOBS);                          * start iteration for current group;                          &lt;BR /&gt;
    set T end=LASTOBS key=A;                   * read data;     &lt;BR /&gt;
    OBS+1;                                     * count observations;&lt;BR /&gt;
    if OBS=2 then AVG1=mean(VALUE,PREV_VALUE); * set avg for first 2 observations;&lt;BR /&gt;
    if LASTOBS then output;                    * no more observations: save averages;&lt;BR /&gt;
    AVG2=mean(VALUE,PREV_VALUE);               * set avg for latest 2 observations;&lt;BR /&gt;
    PREV_VALUE=VALUE;                          * remember value for next iteration; &lt;BR /&gt;
  end;&lt;BR /&gt;
  if _IORC_=1230015 then _ERROR_=0;            * reset error flag ;&lt;BR /&gt;
  keep PART_NUM SUB_ASSEMBLY AVG1 AVG2;       &lt;BR /&gt;
run;</description>
      <pubDate>Mon, 08 Mar 2010 20:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-First-and-Last-record-s-from-a-data-set/m-p/63913#M18176</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2010-03-08T20:44:01Z</dc:date>
    </item>
  </channel>
</rss>

