SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
abhisas1
Fluorite | Level 6

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 

]

 

 

Thank you in advance. 

1 REPLY 1
Shmuel
Garnet | Level 18

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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 428 views
  • 0 likes
  • 2 in conversation