DATA Step, Macro, Functions and more

Array to substitute in Date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Array to substitute in Date

Hi All,

I have a wide data set containing drug names and start dates.

For each individual I want to transfer the start dates for each drug into a new variable. In cases where the drug is the same I want to use the earliest date as the new start date. See example below .

How can this be done using an array?

RX_1

RX_2

RX_3

Start_1

Start_2

Start_3

New_dt1

New_dt2

New_dt3

1

1

2

2

01/20/05

01/29/05

1/30/05

01/20/05

01/29/05

1/29/05

2

1

2

3

01/20/05

01/20/05

1/21/05

01/20/05

01/20/05

1/21/05

Thanks,

Jonathan


Accepted Solutions
Solution
‎07-25-2011 03:38 PM
PROC Star
Posts: 7,365

Array to substitute in Date

You don't really need arrays to do this, but you asked:

data want (drop=i j);

  set have;

  array old(*)Start_1-Start_3;

  array drug(*)RX_1-RX_3;

  array New_dt(3);

  format New_dt1-New_dt3 mmddyy8.;

  do i=1 to dim(old);

    New_dt(i)=old(i);

    do j=1 to i;

      if drug(i) eq drug(j) then do;

              New_dt(i)=min(New_dt(i),New_Dt(j));

              New_dt(j)=New_dt(i);

      end;

    end;

  end;

run;

View solution in original post


All Replies
Solution
‎07-25-2011 03:38 PM
PROC Star
Posts: 7,365

Array to substitute in Date

You don't really need arrays to do this, but you asked:

data want (drop=i j);

  set have;

  array old(*)Start_1-Start_3;

  array drug(*)RX_1-RX_3;

  array New_dt(3);

  format New_dt1-New_dt3 mmddyy8.;

  do i=1 to dim(old);

    New_dt(i)=old(i);

    do j=1 to i;

      if drug(i) eq drug(j) then do;

              New_dt(i)=min(New_dt(i),New_Dt(j));

              New_dt(j)=New_dt(i);

      end;

    end;

  end;

run;

Occasional Contributor
Posts: 11

Array to substitute in Date

Art297,

     Thank you for the swift reply and help.

Jonathan

Respected Advisor
Posts: 3,777

Array to substitute in Date

I thought WHICHN function might be useful.   But I probably don't understand the problem.

data have;

   input id RX_1-RX_3 (Start_1-Start_3)(:mmddyy.);

   array rx

  • rx_:;
  •    array s

  • start_:;
  •    array new[3];

       do i = 1 to dim(rx);

          new = s[whichN(rx,of rx

  • )];
  •       end;

       format start_: new: mmddyy.;

       drop i;

       cards;

    1  1  2  2  01/20/05  01/29/05  1/30/05

    2  1  2  3  01/20/05  01/20/05  1/21/05

    ;;;;

       run;

    proc print;

       run;

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 3 replies
    • 165 views
    • 0 likes
    • 3 in conversation