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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 351 views
  • 0 likes
  • 2 in conversation