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;
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!
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.