DATA Step, Macro, Functions and more

Calculate new values considering data next column data

Reply
Frequent Contributor
Posts: 115

Calculate new values considering data next column data

Good day friends:

I have this data set:
Calculate new values considering data next column data

data HAVE    ;
input    ANIMAL$    WEEK    WEIGHT    FEED;
cards;
A    0    10    .
A    1    12    40
A    2    15    44
A    3    19    50
A    4    23    50
B    0    8    .
B    1    13    40
B    2    15    42
B    3    17    44
B    4    22    50
C    0    12    .
C    1    15    38
C    2    18    42
C    3    22    45
C    4    25    47
;

as you can see this data set shows the weight and feed intake of an determined animal in a week X, varying from 0 (as initial weight) to week 4. Next to it we have the feed consumption, it does not matter in what unit we well calculate the result. So the table i need is:


data want;

input
ANIMAL    WEEK    WEIGHT    WEIGH_GAIN    FEED    CUMULATIVE_FEED;
cards;
A    0    10    .    .    .
A    1    12    2    40    40
A    2    15    3    44    84
A    3    19    4    50    134
A    4    23    4    50    184
B    0    8         .    .
B    1    13    5    40    40
B    2    15    2    42    82
B    3    17    2    44    126
B    4    22    5    50    176
C    0    12    .    .    .
C    1    15    3    38    40
C    2    18    3    42    82
C    3    22    4    45    127
C    4    25    3    47    174
;

As you can see,at week 0 it canot existe weight gain because this is the initial weigh, but at week 1, we can obser that the aninalA gain 2 units comparein to the week 0. In the other hand, we need the cumulative feed intake. As seen in table WANT, the cumulative feed intake begins as week 1, having its same value, finishing in week 4. this has to be calculatefor animal A, B and C.

Can anybody help please?

thanks

Super User
Super User
Posts: 9,211

Re: Calculate new values considering data next column data

Posted in reply to jonatan_velarde

So at a rough guess from what you post its just retaining two sums per by group:

data want;
  set have;
  retain weight_gain cumulative_feed;
  by animal;
  if first.animal then do;
    weight_gain=.;
    cumulative_feed=.;
  end;
  else do;
    weight_gain=sum(weight_gain,weight);
    cumulative_feed=sum(cumulative_feed,feed);
  end;
run;
Frequent Contributor
Posts: 115

Re: Calculate new values considering data next column data

nice answer my friend.

 

But still the weight gain cumulative, we need the non cumulative in this case:

 

12 - 10 = 2, it means that this animal gained 2 grams in 1 week and so on.

Super User
Super User
Posts: 9,211

Re: Calculate new values considering data next column data

Posted in reply to jonatan_velarde

Slight update then:

data want;
  set have;
  retain weight_gain;
  by animal;
  if first.animal then do;
    weight_gain=.;
    cumulative_feed=.;
  end;
  else do;
    weight_gain=sum(weight_gain,weight);
    cumulative_feed=dif(feed);
  end;
run;
Frequent Contributor
Posts: 115

Re: Calculate new values considering data next column data

to close!
PROC Star
Posts: 1,322

Re: Calculate new values considering data next column data

Posted in reply to jonatan_velarde
data HAVE    ;
input    ANIMAL$    WEEK    WEIGHT    FEED;
cards;
A    0    10    .
A    1    12    40
A    2    15    44
A    3    19    50
A    4    23    50
B    0    8    .
B    1    13    40
B    2    15    42
B    3    17    44
B    4    22    50
C    0    12    .
C    1    15    38
C    2    18    42
C    3    22    45
C    4    25    47
;

data want;
retain ANIMAL    WEEK    WEIGHT    WEIGH_GAIN    FEED    CUMULATIVE_FEED;
call missing(_weight,CUMULATIVE_FEED,WEIGH_GAIN);
do until(last.animal);
set have;
by animal;
if not first.animal then WEIGH_GAIN=weight-_weight;
CUMULATIVE_FEED+feed;
_weight=weight;
output;
end;
drop _:;
run;

Super User
Posts: 6,540

Re: Calculate new values considering data next column data

Posted in reply to jonatan_velarde

Here's a variation that should handle the remaining issues:

 

data want;

   set have;

   by animal;

   retain cumulative_feed;

   weight_gain = dif(weight);

   if first.animal then do;

      weight_gain = .;

      cumulative_feed = .;

   end;

   else cumulative_feed = sum(cumulative_feed, feed);

run;

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 133 views
  • 0 likes
  • 4 in conversation