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
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;
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;
Art297,
Thank you for the swift reply and help.
Jonathan
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
array s
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;
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.
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.