BookmarkSubscribeRSS Feed
GDA
Calcite | Level 5 GDA
Calcite | Level 5
Hello all SAS guru's

I have a data set that has 4 columns.

PART_NUM (character)
SUB_ASSEMBLY (numeric)
DATE (date)
VALUE (numeric)

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.
Each part_num can have a different number of sub_assembly values.

For example if the data set was: (data is in columns. doesn't copy very well with just text)
PART_NUM SUB_ASSEMBLY DATE VALUE
A 0 3/1/2010 5
A 1 3/1/2010 4
A 2 3/1/2010 5
A 0 3/2/2010 4
A 1 3/2/2010 4
A 2 3/2/2010 5
B 0 3/1/2010 5
B 1 3/1/2010 4
B 0 3/2/2010 4
B 1 3/2/2010 4
...
A 0 3/20/2010 6
A 1 3/20/2010 7
A 2 3/20/2010 4
A 0 3/21/2010 4
A 1 3/21/2010 5
A 2 3/21/2010 3
B 0 3/20/2010 6
B 1 3/20/2010 7
B 0 3/21/2010 3
B 1 3/21/2010 5

This would yield a table with 4 columns with unique PART_NUM, SUB_ASSEMBLY and two "averaged" columns.
PART_NUM SUB_ASSEMBLY Average(First2) Average(Second2)
A 0 4.5 5.0
A 1 4.0 6.0
A 2 5.0 3.5
B 0 4.5 4.5
B 1 4.0 6.0

As an example, the first row of the output table with PART_NUM "A" and SUB_ASSEMBLY "0"
the average(First 2) is the avg of 5 on 3/1/2010 and 4 on 3/2/2010
the average(Second 2) is the avg of 6 on 3/20/2010 and 3 on 3/20/2010

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.
So I'm hoping that some somebody either knows how or can give some advice on SAS code that will solve this.

I'm also hoping I explained this clearly enough.

thanks
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
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.

cynthia
ChrisNZ
Tourmaline | Level 20
I don't see how you could do this in EG without creating many repetitive processes.

Here is how you could do it with a bit of code:
[pre]
data T(index=(a=(PART_NUM SUB_ASSEMBLY)));
input PART_NUM $ SUB_ASSEMBLY $ DATE mmddyy10. VALUE;
cards;
A 0 03/01/2010 5
A 1 03/01/2010 4
A 2 03/01/2010 5
A 0 03/02/2010 4
A 1 03/02/2010 4
A 2 03/02/2010 5
B 0 03/01/2010 5
B 1 03/01/2010 4
B 0 03/02/2010 4
B 1 03/02/2010 4
A 0 03/20/2010 6
A 1 03/20/2010 7
A 2 03/20/2010 4
A 0 03/21/2010 4
A 1 03/21/2010 5
A 2 03/21/2010 3
B 0 03/20/2010 6
B 1 03/20/2010 7
B 0 03/21/2010 3
B 1 03/21/2010 5
run;
proc sort data=T out=LIST nodupkey; * build table of all calculation groups;
by PART_NUM SUB_ASSEMBLY ;
data END;
set LIST; * for each calculation group;
OBS=0; * reset observation counter;
do until (LASTOBS); * start iteration for current group;
set T end=LASTOBS key=A; * read data;
OBS+1; * count observations;
if OBS=2 then AVG1=mean(VALUE,PREV_VALUE); * set avg for first 2 observations;
if LASTOBS then output; * no more observations: save averages;
AVG2=mean(VALUE,PREV_VALUE); * set avg for latest 2 observations;
PREV_VALUE=VALUE; * remember value for next iteration;
end;
if _IORC_=1230015 then _ERROR_=0; * reset error flag ;
keep PART_NUM SUB_ASSEMBLY AVG1 AVG2;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 616 views
  • 0 likes
  • 3 in conversation