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;

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    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.

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