Fluorite | Level 6

## Need help with setting an array for an algorithm

Hello Community.

I have this algorithm given to me and was asked to generate new variable . I'm not finding right way to do it. Could you please help me with it.

data chk;
input subject\$ week\$ numdisp pilre dose_taken;
datalines;
1 week1 10 . .
1 week2 10 3 100
1 week3 10 1 200
2 week2 20 . .
2 week3 10 3 300
3 week1 10 . .
3 week2 30 0 100
3 week3 20 2 200
3 week4 30 5 300
;
run;

The weeks in this gets added every week. From this dataset i want to create a new variable called CDOSE for each subject(populated at the last visit). and it should be derived as follows.

[

( NUMDISP(WEEK1) - PILRE(WEEK2) +

NUMDISP(WEEK2) - PILRE(WEEK3)

NUMDISP(WEEK3) - PILRE(WEEK4)   ) *100

+ dose_taken of the last week (example subject-3 's last week is 4 so we will take 300)

]

Note: some subjects do not have all weeks . ex: subject-2 doesnt have week 1 so for this algorithm would be like this:

[

( NUMDISP(WEEK2) - PILRE(WEEK3)  ) *100

+ dose_taken of the last week

]

Garnet | Level 18

## Re: Need help with setting an array for an algorithm

Here is a code to create the arrays you need.

I leave it to you to add the computation.

Data is assumed to be sorted by subject;

``````data computed;
set have;
by subject;
retain ndisp1-ndisp4 pil1-pil4;
array nd {4} ndisp1-ndisp4;
array pl {4} pil1-pil4;
if first.subject then do i=1 to 4;  /* clear arrays */
nd(i)=.;  pl(i)=.;
end;

/* do for every observation per subject = save data in the arrays */
i = input(substr(week,1,1),1.);    /* week nomber 1 to 4 */
nd(i) = numdisp;
pl(i)  = pilre;

if last.subject then do;
/*  compute the CDOSE by formula */            ......
output;
end;         keep subject cdose;
run;
``````

You may need to merge HAVE and COMPUTED tables by SUBJECT to have CDOSE variable on each observation.

Discussion stats