BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
jonatan_velarde
Pyrite | Level 9

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
novinosrin
Tourmaline | Level 20
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;

Astounding
PROC Star

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;

 

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 866 views
  • 0 likes
  • 4 in conversation