BookmarkSubscribeRSS Feed
jonatan_velarde
Lapis Lazuli | Level 10

Good night my friends:

 

Nice to see you again, here i go with an exercise that i hope it to be possible to solve. Let get it on!

 

There has been studied 4 dogs in relation to their growth. This study was related to their weights and its evolution during time. So, I have data from their weights as follows in the table 1:

 

TABLE 1. RAW OBSERVED DATA

DOG

AGE_IN_DAYS

WEIGHT

1

0

3.5

1

19

4.8

1

47

11.3

1

75

22

1

103

21.5

1

131

34.8

1

159

32.3

1

190

37.1

1

215

42.4

1

243

.

1

272

.

1

299

.

1

327

.

2

0

3.7

2

23

7.7

2

51

14.3

2

79

18

2

107

23.2

2

135

25

2

163

29.3

2

194

33.9

2

219

37.6

2

247

.

2

276

.

2

303

43.2

2

331

43.0

2

359

43.5

2

387

.

2

415

.

3

0

4.9

3

23

6.1

3

50

13.2

3

78

17.2

3

106

20

3

134

25

3

162

28

3

184

29

3

217

32.8

3

246

34.8

3

274

37.4

3

302

36.4

3

330

38.6

3

358

40.4

3

386

37.8

3

414

37.4

3

442

37.2

3

470

35.4

3

498

36.1

3

526

42

3

554

40.4

3

786

64.4

3

814

65.9

4

0

3.5

4

23

7.5

4

50

10.2

4

78

.

4

106

18

4

134

.

4

162

25

 

 

Here is the thing, what I need to calculate is the average weight gained between each measure, that means between ages, as follows.

 

For example, Dog number 2 at age 0, or at birth, showed the weight of 3.7 kg and the next weight was performed at day 23 being 7.7 kg, it means that in 23 days this dog gained 4 kilograms, and it means also that each day the dog gained:

 

4/23 = 0.17391, it means 173.91 grams gained each day growing. As result of multiplication of this result in grams times 1000, converting this value from kilograms to grams.

 

With the information in table 1, we need to obtain the 3 last columns as shown in the table 2:

 

TABLE 2. RAW OBSERVED DATA, PLUS LAST 2 COLUMNS WITH KILOGRAMS AND GRAMS GAINED BY DAY

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

1

0

3.5

.

.

.

1

19

4.8

19

1.30

68.42

1

47

11.3

28

6.50

232.14

1

75

22

28

10.7

382.14

1

103

21.5

28

-0.5

-17.86

1

131

34.8

28

13.30

475.00

1

159

32.3

28

-2.50

-89.29

1

190

37.1

31

4.80

154.84

1

215

42.4

25

5.30

212.00

2

0

3.7

.

.

.

2

23

7.7

23

4.00

173.91

2

51

14.3

28

6.60

235.71

2

79

18

28

3.70

132.14

2

107

23.2

28

5.20

185.71

2

135

25

28

1.80

64.29

2

163

29.3

28

4.30

153.57

2

194

33.9

31

4.60

148.39

2

219

37.6

25

3.70

148.00

2

247

.

.

.

.

2

276

.

.

.

.

2

303

43.2

84

5.60

66.67

2

331

43

28

-0.20

-7.14

2

359

43.5

28

0.50

17.86

2

387

.

.

.

.

2

415

.

.

.

.

3

0

4.9

.

.

.

3

23

6.1

23

1.20

52.17

3

50

13.2

27

7.10

262.96

3

78

17.2

28

4.00

142.86

3

106

20

28

2.80

100.00

3

134

25

28

5.00

178.57

3

162

28

28

3.00

107.14

3

184

29

22

1.00

45.45

3

217

32.8

33

3.80

115.15

3

246

34.8

29

2.00

68.97

3

274

37.4

28

2.60

92.86

3

302

36.4

28

-1.00

-35.71

3

330

38.6

28

2.20

78.57

3

358

40.4

28

1.80

64.29

3

386

37.8

28

-2.60

-92.86

3

414

37.4

28

-0.40

-14.29

3

442

37.2

28

-0.20

-7.14

3

470

35.4

28

-1.80

-64.29

3

498

36.1

28

0.70

25.00

3

526

42

28

5.90

210.71

3

554

40.4

28

-1.60

-57.14

3

786

64.4

232

24.00

103.45

3

814

65.9

28

1.50

53.57

4

0

3.5

.

.

.

4

23

7.5

23

4.00

173.91

4

50

10.2

27

2.70

100.00

4

78

.

.

 

 

4

106

18

56

7.80

139.29

4

134

.

.

 

 

4

162

25

56

7.00

125.00

 

From here, what have to be obtained is the corrected weight each 20 days of live for each dog, i'll try to explain this in the next paragraphs.

 

If you look carefully each measure, at its corresponding line, has its own kilogram gained and grams gained by day. This value could be negative, it does not matter cause dogs often loose weight, this is pretty normal. Therefore, the question is I need to obtain the adjusted weight each 20 days from birth to further ages: 20, 40, 60, 80 and so on until the dog dies and has no more measures. This happens when the next information is at age 0 corresponding to the next dog.

 

For a better representation I will use dog 3 and its measures at day 50 and 78.

 

Dog 3 at 50 days old weighted 13.2 Kg, then at 78 days old he weighted 17.2. It means that in 28 days this do gained 4 kilograms, one more time as follows:

 

4/28 = 0.14285, it means 142.85 grams gained each day growing from 50 to 78 days old.

 

The question is: If I have these, and their corresponding daily gains weights, I can estimate other measures in other ages, for example each 20 days. I wanted to point out these measures because if I want to estimate the weight at 60 days old of the dog I can use this information of the 50 and 78 days old, having the estimate gain in grams each day I can estimate the weight at 60 days like this:

 

 

[(weight gain in grams between 2 measures)*(age to estimate weight-first age measured)] + weight at first age measured

 

[(142.85 grams)*(60-50)]+weight at 50 days = [(142.85)*10]+13.2 = 1.4285+13.2 = 14.6285 kg at 60 days.

 

From now, and using the table above obtained, we need to obtain the corrected weight at corresponding each 20 days, as follows in the next table:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

1

0

3.5

.

.

.

.

 

1

19

4.8

19

1.30

68.42

20

5.03214

1

47

11.3

28

6.50

232.14

40

9.67500

1

75

22

28

10.70

382.14

60

16.26786

1

103

21.5

28

-0.50

-17.86

80

21.91071

1

131

34.8

28

13.30

475.00

100

21.55357

1

159

32.3

28

-2.50

-89.29

120

29.57500

1

190

37.1

31

4.80

154.84

140

33.99643

1

215

42.4

25

5.30

212.00

160

32.45484

1

243

.

28

.

.

180

35.55161

1

272

.

29

.

.

200

39.22000

1

299

.

27

.

.

220

.

1

327

.

28

.

.

240

.

2

0

3.7

.

.

.

20

7.17826

2

23

7.7

23

4.00

173.91

40

11.70714

2

51

14.3

28

6.60

235.71

60

15.48929

2

79

18

28

3.70

132.14

80

18.18571

2

107

23.2

28

5.20

185.71

100

21.90000

2

135

25

28

1.80

64.29

120

24.03571

2

163

29.3

28

4.30

153.57

140

25.76786

2

194

33.9

31

4.60

148.39

160

33.00968

2

219

37.6

25

3.70

148.00

180

36.41600

2

247

.

28

.

.

200

34.78800

2

276

.

29

.

.

220

37.74800

2

303

43.2

27

.

.

240

40.70800

2

331

43

28

-0.20

-7.14

260

43.66800

2

359

43.5

28

0.50

17.86

280

46.62800

2

387

.

28

.

.

300

49.58800

2

415

.

28

.

.

320

43.07857

2

.

.

.

.

.

340

43.16071

3

0

4.9

.

.

.

20

5.94348

3

23

6.1

23

1.20

52.17

40

10.57037

3

50

13.2

27

7.10

262.96

60

14.62857

3

78

17.2

28

4.00

142.86

80

17.40000

3

106

20

28

2.80

100.00

100

19.40000

3

134

25

28

5.00

178.57

120

22.50000

3

162

28

28

3.00

107.14

140

25.64286

3

184

29

22

1.00

45.45

160

27.78571

3

217

32.8

33

3.80

115.15

180

28.81818

3

246

34.8

29

2.00

68.97

200

30.84242

3

274

37.4

28

2.60

92.86

220

33.00690

3

302

36.4

28

-1.00

-35.71

240

34.38621

3

330

38.6

28

2.20

78.57

260

36.10000

3

358

40.4

28

1.80

64.29

280

37.18571

3

386

37.8

28

-2.60

-92.86

300

36.47143

3

414

37.4

28

-0.40

-14.29

320

37.81429

3

442

37.2

28

-0.20

-7.14

340

39.24286

3

470

35.4

28

-1.80

-64.29

360

40.21429

3

498

36.1

28

0.70

25.00

380

38.35714

3

526

42

28

5.90

210.71

400

37.60000

3

554

40.4

28

-1.60

-57.14

420

37.35714

3

786

64.4

232

24.00

103.45

440

37.21429

3

814

65.9

28

1.50

53.57

460

37.32857

3

.

.

.

.

.

480

35.65000

3

.

.

.

.

.

500

36.52143

3

.

.

.

.

.

520

40.73571

3

.

.

.

.

.

540

41.20000

3

.

.

.

.

.

560

41.02069

3

.

.

.

.

.

780

63.77931

3

.

.

.

.

.

800

65.15000

4

0

3.5

.

.

.

20

6.97826

4

23

7.5

23

4.00

173.91

40

9.20000

4

50

10.2

27

2.70

100.00

60

11.59286

4

78

.

.

 

 

80

14.37857

4

106

18

56

7.80

139.29

100

17.16429

4

134

.

.

 

 

120

19.75000

4

162

25

56

7.00

125.00

140

22.25000

 

With the prerogative, IT IS NEEDED TO ESTIMATE THE WEIGHT OF EACH DOG EACH 20 DAYS

 

Each example i post here, i try to make it very useful to be helped easily, so the colors in the table 3, what is thought to be achieved, are going to help in the estimations, because those are special cases because the data of each dog is special. Here we go:

 

As said repetitively, each 20 days must appear the new weight for each dog. As seen in Table 3, dog 1 has its first recorded weight at 19 days old, it means that this weight can not be used to estimate the weight at 20 days old of this dog, meanwhile for dog 2 and 3 they have their respective first weight at day 23, for that reason it can be estimated the weight at day 20, and this measure is allocated at the same line that the weight at age 0, indicating that dogs with its second weight less than 20 days old will be its first estimated weight at 20 days in the second line, and those with the second weight with more than 20 days can have the new estimated weight at 20 days in the same line that the weight at age 0.

 

Else, for dog 1 (for weigh at 19 and 47) and for dog 3 (for weigh at 78 and 47) it can be observed that the new estimated measures at days 20 and 40 for dog 1, and at days 80 and 100 for dog 3, are used to estimate two new weights, using just 2 measures.

 

Next, in green color it can be observed that the new estimated measures are not located in the same line that the weights used to their respective estimation. It can be noted that these two estimations are result of their respective upper and lower ages. Describing this with details:

 

In dog 1 for estimated weights at 80 and 100 days of age, its used the recorded weight at 75 and 103 days, and those new estimates are located not in the same line that the corresponding line, those values are located in new rows below the last new estimated values, for this i will take this to see it here:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

1

75

22

28

10.70

382.14

60

16.26786

1

103

21.5

28

-0.50

-17.86

80

21.91071

1

131

34.8

28

13.30

475.00

100

21.55357

 

This can be observed also for the dog 2, for the estimated values for 140 and 160 ages, as seen in the TABLE 3:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

2

135

25

28

1.80

64.29

120

24.03571

2

163

29.3

28

4.30

153.57

140

25.76786

2

194

33.9

31

4.60

148.39

160

33.00968

 

Related case can be observed for dog 3 and for estimation of the new weight at 220 and 240 ages because the new data are in rows after the original data used to their estimation:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

3

217

32.8

33

3.80

115.15

180

28.81818

3

246

34.8

29

2.00

68.97

200

30.84242

3

274

37.4

28

2.60

92.86

220

33.00690

3

302

36.4

28

-1.00

-35.71

240

34.38621

 

Special cases can be observed in Dog 2 and in Dog 4, where no weights measures were taken for 247 and 276 days for the first one and the day 78 and 134, for the dog 4. In both cases the used values to estimate the middle values were the weights at 219 and 303 days in dog 2 and weight at ages 50, 106 and 134 for dog 4.

 

In dog 2, can be observed that the difference between days 219 and 303 is 84 dais, in these period the dog gained 5.6 kilograms and 66,67 grams per day, and using this information were estimated weights at 220, 240, 260 280 and 300 days of age, as shown in Table 3:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

2

219

37.6

25

3.70

148.00

180

36.41600

2

247

.

28

.

.

200

34.78800

2

276

.

29

.

.

220

37.74800

2

303

43.2

27

.

.

240

40.70800

2

331

43

28

-0.20

-7.14

260

43.66800

2

359

43.5

28

0.50

17.86

280

46.62800

2

387

.

28

.

.

300

49.58800

 

The other special case is shown in dog 4 where the is many missing data, where

 

In this case, the dog was not measured in relation to it weight at 78 and 134 days of age, but the other measures are recorded. In this case to estimate weights for 60, 80 and 100 days were used the recorded weights at 50 and 106 days, witch difference was 56 days with an gained weight of 7.8 kilograms and 139.29 grams per day. In the same context, to estimate weight for 120, 140 and 160 days of age were user the weights at 106 and 162 years of age.

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

4

0

3.5

.

.

.

20

6.97826

4

23

7.5

23

4.00

173.91

40

9.20000

4

50

10.2

27

2.70

100.00

60

11.59286

4

78

.

.

 

 

80

14.37857

4

106

18

56

7.80

139.29

100

17.16429

4

134

.

.

 

 

120

19.75000

4

162

25

56

7.00

125.00

140

22.25000

 

 

Finally, it can be pointed out that the observations for each dog ends where the value for age of the next dog is equal to zero, so i guess the loop to be applied for each animal. With this, the table to be obtained, again, is:

 

DOG

AGE_IN_DAYS

WEIGHT

days_between_measure

kg_gained

gr_gained_by_day

new_age

weight_at_new_age

1

0

3.5

.

.

.

.

 

1

19

4.8

19

1.30

68.42

20

5.03214

1

47

11.3

28

6.50

232.14

40

9.67500

1

75

22

28

10.70

382.14

60

16.26786

1

103

21.5

28

-0.50

-17.86

80

21.91071

1

131

34.8

28

13.30

475.00

100

21.55357

1

159

32.3

28

-2.50

-89.29

120

29.57500

1

190

37.1

31

4.80

154.84

140

33.99643

1

215

42.4

25

5.30

212.00

160

32.45484

1

243

.

28

.

.

180

35.55161

1

272

.

29

.

.

200

39.22000

1

299

.

27

.

.

220

.

1

327

.

28

.

.

240

.

2

0

3.7

.

.

.

20

7.17826

2

23

7.7

23

4.00

173.91

40

11.70714

2

51

14.3

28

6.60

235.71

60

15.48929

2

79

18

28

3.70

132.14

80

18.18571

2

107

23.2

28

5.20

185.71

100

21.90000

2

135

25

28

1.80

64.29

120

24.03571

2

163

29.3

28

4.30

153.57

140

25.76786

2

194

33.9

31

4.60

148.39

160

33.00968

2

219

37.6

25

3.70

148.00

180

36.41600

2

247

.

28

.

.

200

34.78800

2

276

.

29

.

.

220

37.74800

2

303

43.2

27

.

.

240

40.70800

2

331

43

28

-0.20

-7.14

260

43.66800

2

359

43.5

28

0.50

17.86

280

46.62800

2

387

.

28

.

.

300

49.58800

2

415

.

28

.

.

320

43.07857

2

.

.

.

.

.

340

43.16071

3

0

4.9

.

.

.

20

5.94348

3

23

6.1

23

1.20

52.17

40

10.57037

3

50

13.2

27

7.10

262.96

60

14.62857

3

78

17.2

28

4.00

142.86

80

17.40000

3

106

20

28

2.80

100.00

100

19.40000

3

134

25

28

5.00

178.57

120

22.50000

3

162

28

28

3.00

107.14

140

25.64286

3

184

29

22

1.00

45.45

160

27.78571

3

217

32.8

33

3.80

115.15

180

28.81818

3

246

34.8

29

2.00

68.97

200

30.84242

3

274

37.4

28

2.60

92.86

220

33.00690

3

302

36.4

28

-1.00

-35.71

240

34.38621

3

330

38.6

28

2.20

78.57

260

36.10000

3

358

40.4

28

1.80

64.29

280

37.18571

3

386

37.8

28

-2.60

-92.86

300

36.47143

3

414

37.4

28

-0.40

-14.29

320

37.81429

3

442

37.2

28

-0.20

-7.14

340

39.24286

3

470

35.4

28

-1.80

-64.29

360

40.21429

3

498

36.1

28

0.70

25.00

380

38.35714

3

526

42

28

5.90

210.71

400

37.60000

3

554

40.4

28

-1.60

-57.14

420

37.35714

3

786

64.4

232

24.00

103.45

440

37.21429

3

814

65.9

28

1.50

53.57

460

37.32857

3

.

.

.

.

.

480

35.65000

3

.

.

.

.

.

500

36.52143

3

.

.

.

.

.

520

40.73571

3

.

.

.

.

.

540

41.20000

3

.

.

.

.

.

560

41.02069

3

.

.

.

.

.

780

63.77931

3

.

.

.

.

.

800

65.15000

4

0

3.5

.

.

.

20

6.97826

4

23

7.5

23

4.00

173.91

40

9.20000

4

50

10.2

27

2.70

100.00

60

11.59286

4

78

.

.

.

.

80

14.37857

4

106

18

56

7.80

139.29

100

17.16429

4

134

.

.

.

.

120

19.75000

4

162

25

56

7.00

125.00

140

22.25000

 

After this, and transposing this table, the new data would be:

 

DOG 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400 420 440 460 480 500 520 540 560 580 600 620 640 660 680 700 720 740 760 780 800
1 3.5000 5.0321 9.6750 16.2679 21.9107 21.5536 29.5750 33.9964 32.4548 35.5516 39.2200 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 3.7000 7.1783 11.7071 15.4893 18.1857 21.9000 24.0357 25.7679 33.0097 36.4160 34.7880 37.7480 40.7080 43.6680 46.6280 49.5880 43.0786 43.1607 . . . . . . . . . . . . . . . . . . . . . . .
3 4.9000 5.9435 10.5704 14.6286 17.4000 19.4000 22.5000 25.6429 27.7857 28.8182 30.8424 33.0069 34.3862 36.1000 37.1857 36.4714 37.8143 39.2429 40.2143 38.3571 37.6000 37.3571 37.2143 37.3286 35.6500 36.5214 40.7357 41.2000 41.0207 . . . . . . . . . . 63.7793 65.1500
4 3.5000 6.9783 9.2000 11.5929 14.3786 17.1643 19.7500 22.2500 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 

where in the first column appears the dogs evaluated, and in the next columns are the new estimated weights each 20 days.

 

I hope to be helped.

 

Thank you very much 

 

 

 

 

 

 

 

 

 

 

 

6 REPLIES 6
ballardw
Super User

Excel files are not data sets.

Please provide such in the form of a data step.

 

What  you are requesting is called Interpolation. Here is one way:

data have;
   input dog age_in_days weight;
datalines;
1  0 3.5
1 19 4.8
1 47 11.3
1 75 22
;

data interpolated;
   set have;
   by dog;
   lage= lag(age_in_days);
   lwt = lag(weight);
   if first.dog then ;
   else do ;
      deltawt= (weight - lwt) /(age_in_days - lage);
      newweight= lwt;
      do newage=lage to age_in_days;
         if mod(newage,20)=0 then output;
         newweight = newweight + deltawt;
      end;
   end;
   keep newweight newage;
run;

This actually calculates a weight each day and does have an end point /start point issue that will be masked for anything except an actual age multiple of 20 in the data. Exercise for the interested reader to remove it.

jonatan_velarde
Lapis Lazuli | Level 10

Thank you very much for the answer:

 

as requested, here is the data set:

 

data have;

input DOG AGE_IN_DAYS WEIGHT;

cards;
1 0 3.5
1 19 4.8
1 47 11.3
1 75 22
1 103 21.5
1 131 34.8
1 159 32.3
1 190 37.1
1 215 42.4
1 243 .
1 272 .
1 299 .
1 327 .
2 0 3.7
2 23 7.7
2 51 14.3
2 79 18
2 107 23.2
2 135 25
2 163 29.3
2 194 33.9
2 219 37.6
2 247 .
2 276 .
2 303 43.2
2 331 43
2 359 43.5
2 387 .
2 415 .
3 0 4.9
3 23 6.1
3 50 13.2
3 78 17.2
3 106 20
3 134 25
3 162 28
3 184 29
3 217 32.8
3 246 34.8
3 274 37.4
3 302 36.4
3 330 38.6
3 358 40.4
3 386 37.8
3 414 37.4
3 442 37.2
3 470 35.4
3 498 36.1
3 526 42
3 554 40.4
3 786 64.4
3 814 65.9
4 0 3.5
4 23 7.5
4 50 10.2
4 78 .
4 106 18
4 134 .
4 162 25

;

mkeintz
PROC Star

Edit note: This program has been modified to:

  1. Read in only DOG, AGE_IN_DAYS, and WEIGHT variables into dataset HAVE.
  2. Fix the problem of extra observations in the NEW_AGES data set due to extra loop invoked when WEIGHT=.  The new statement now is (the bold italics is new):  

    if nxt_dog=dog and weight^=. then do new_age= 20*ceil(age_in_days/20) to nxt_age by 20;

 

Instead of creating 1 table with both weight gain history and weight at new ages, why not create two datasets:

  1. weight_gain_history
  2. new_ages

 

Now I understand all the new age weight estimates will be for 20, 40, 60, ... days, so the new_weight values are interpolated between the nearest preceding and nearest following recorded ages.  If so, then :

 

 


data have;
 input DOG	AGE_IN_DAYS	WEIGHT	;
datalines;
1	  0	 3.5
1    19	 4.8
1	 47	11.3
1	 75	22
1	103	21.5
1	131	34.8
1	159	32.3
1	190	37.1
1	215	42.4
2	  0	 3.7
2	 23	 7.7
2	 51	14.3
2	 79	18
2	107	23.2
2	135	25
2	163	29.3
2	194	33.9
2	219	37.6
2	247	  .
2	276   .
2	303	43.2
2	331	43
2	359	43.5
2	387	  .
2	415	  .
3	  0	 4.9
3	 23	 6.1
3	 50	13.2
3	 78	17.2
3	106	20
3	134	25
3	162	28
3	184	29
3	217	32.8
3	246	34.8
3	274	37.4
3	302	36.4
3	330	38.6
3	358	40.4
3	386	37.8
3	414	37.4
3	442	37.2
3	470	35.4
3	498	36.1
3	526	42
3	554	40.4
3	786	64.4
3	814	65.9
4	  0	 3.5	
4	 23	 7.5	
4	 50	10.2
4	 78	  .
4	106	18
4	134	  .	
4	162	25
run;

data weight_history  (drop=nxt_: new_:)
     new_ages (keep=dog new_:);
  set have;
  by dog;
  if weight^=. then do;
    days_between_measure=dif(age_in_days);
    kg_gained=round(dif(weight),.1);
	if first.dog then call missing(days_between_measure,kg_gained);
	if kg_gained^=. then gr_gained_by_day=  round(1000*kg_gained/days_between_measure,.01);
  end;
  output weight_history;
  if weight^=. then do until (nxt_weight^=.);
    set have (firstobs=2 keep=dog age_in_days weight rename=(dog=nxt_dog age_in_days=nxt_age weight=nxt_weight));
  end;
  if nxt_dog=dog and weight^=. then do new_age= 20*ceil(age_in_days/20) to nxt_age by 20;    
    new_weight=  round(weight + (nxt_weight-weight)*((new_age-age_in_days)/(nxt_age-age_in_days)),.00001);
    output new_ages;
  end;
run;

notes:

  1.   The DIF function is defined as  dif(x)=x-lag(x).  But "lag" is a misnomer.  It really means "the most recent update in a queue of values of X.   So when you have "if weight^=. then kg_gained=dif(weight)", the "lag" value is only updated when non-missing values of weight are in the record.  I.e. it is never contaminated with missing weight values - so when a record follows a missing weight, then the "lag" value goes back 2 records, not 1.
  2. Notice you can declare multiple output datasets in the DATA statement, and you can selectively output them.
  3. The "if first.dog then call missing ..." statement ensures that you don't contaminate the starting value of one dog with the ending value of the preceding dog.
  4. The "if weight^=. then do until (nxt_weight^=.) reads ahead one record until a non-missing "next" weight is encountered.
  5. The following loop generates, through interpolation of WEIGHT ad NXT_WEIGHT, all the weights for intervening occurrences of 20, 40, 60, ... age in days.

 

Regards,

Mark

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jonatan_velarde
Lapis Lazuli | Level 10
Very nice code, but before this:

How do i create the column "days_between_measur"?

thanks in advance
mkeintz
PROC Star

It's right there in the code.  I suggest a cup of coffee and a second look.

 

Regards,

Mark

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jonatan_velarde
Lapis Lazuli | Level 10

Dog number 4 show like that:

 

76 4 0 3.5000
77 4 20 6.9783
78 4 40 9.2000
79 4 60 11.5929
80 4 80 14.3786
81 4 100 17.1643
82 4 80 .
83 4 100 .
84 4 120 19.7500
85 4 140 22.2500
86 4 160 24.7500
87 4 140 .
88 4 160 .

 

Could it be the loop?

 

Thanks in advance

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 896 views
  • 1 like
  • 3 in conversation