BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jdukes42
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

3 REPLIES 3
art297
Opal | Level 21

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;

jdukes42
Calcite | Level 5

Art297,

     Thank you for the swift reply and help.

Jonathan

data_null__
Jade | Level 19

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;

    hackathon24-white-horiz.png

    The 2025 SAS Hackathon has begun!

    It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

    Latest Updates

    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
    • 3 replies
    • 1558 views
    • 0 likes
    • 3 in conversation