Solved
Contributor
Posts: 32

# Array if do with assignment statement for calculation

I need to calculate the weight differential across doctor visits for 25 unique patients with varying numbers of visits 1-4.  I thought transposing the data with an array and an if do statement assignment statements to calculate the weight differential was the way to go (have not done this yet since data not aligning correct).  I'm having a problem reading the data i.e. data for patient Y has data from patient X because X only had 3 visits instead of 4.

Have 2 questions, is this the best approach to calculate the weight differential by patient and what is the treatment for varying visits by patient.

Data clinincal2;

input Patient visit date date9. weight gender \$ group \$;

CARDS;

1 1 26mar2007 157 male C

1 2 30jun2007 165 male C

1 3 08aug2007 162 male C

2 1 05jun2008 160 female D

3 1 01jan2007 154 female D

3 2 06feb2007 152 female D

3 3 01jun2007 140 female D

3 4 01aug2007 138 female D

4 1 12mar2007 195 male C

4 2 12jun2007 190 male C

5 1 30apr2007 185 female C

;

Data clinical3;

set clinical2;

by patient;

array visits (4) V1-V4;

if first.patient then i = 1;

visits [i] = Weight;

if last.patient then output;

i + 1;

retain V1-V4;

drop i;

RUN

Accepted Solutions
Solution
‎04-03-2018 04:59 PM
Super User
Posts: 6,629

## Re: Array if do with assignment statement for calculation

You are turning an easy problem into a difficult one.  In the long run, you must Must MUST learn to program with data in its original form (as illustrated in your data set CLINICAL2).  Assuming the observations are in the proper order:

data want;

set clinical2;

by patient;

weight_dif = dif(weight);

if first.patient then weight_dif = .;

run;

This gives you visit-by-visit differences.  If you want the difference from the first visit, the tools are similar:

data want;

set clinical2;

by patient;

if first.patient then baseline_weight = weight;

retain baseline_weight;

weight_dif = weight - baseline_weight;

run;

All Replies
Solution
‎04-03-2018 04:59 PM
Super User
Posts: 6,629

## Re: Array if do with assignment statement for calculation

You are turning an easy problem into a difficult one.  In the long run, you must Must MUST learn to program with data in its original form (as illustrated in your data set CLINICAL2).  Assuming the observations are in the proper order:

data want;

set clinical2;

by patient;

weight_dif = dif(weight);

if first.patient then weight_dif = .;

run;

This gives you visit-by-visit differences.  If you want the difference from the first visit, the tools are similar:

data want;

set clinical2;

by patient;

if first.patient then baseline_weight = weight;

retain baseline_weight;

weight_dif = weight - baseline_weight;

run;

☑ This topic is solved.