I'm trying to correct the values in a specific columns for rows in which a specific condition is found.
Using the following dataset as example:
data dttest;
input patient_id $ patient_name $ (enttime draw1 tdiff) (time8. +1);
format enttime draw1 tdiff time8.;
datalines;
18C020 Garcia 11:45:00 11:50:00 00:07:00
17C222 Gibson 11:43:00 11:47:00 00:04:00
18C023 Knapp 11:42:00 11:45:00 00:43:00
10C032 Mueller 09:11:00 11:47:00 02:36:00
;
run;
For the rows where patient_id has "18C", I would like to correct the values in column tdiff by subtracting the time in colum draw1 from the time in column enttime.
I have tried IF/THEN and WHERE to select the rows but I always have an error. Not sure if it is a good idea to create an array.
Anyone has a simple solution for this?
Please try
data dttest;
input patient_id $ patient_name $ (enttime draw1 tdiff) (time8. +1);
format enttime draw1 tdiff time8.;
datalines;
18C020 Garcia 11:45:00 11:50:00 00:07:00
17C222 Gibson 11:43:00 11:47:00 00:04:00
18C023 Knapp 11:42:00 11:45:00 00:43:00
10C032 Mueller 09:11:00 11:47:00 02:36:00
;
run;
data want;
set dttest;
if index(patient_id,'18C') then tdiff=draw1-enttime;
format tdiff time8.;
run;
Please try
data dttest;
input patient_id $ patient_name $ (enttime draw1 tdiff) (time8. +1);
format enttime draw1 tdiff time8.;
datalines;
18C020 Garcia 11:45:00 11:50:00 00:07:00
17C222 Gibson 11:43:00 11:47:00 00:04:00
18C023 Knapp 11:42:00 11:45:00 00:43:00
10C032 Mueller 09:11:00 11:47:00 02:36:00
;
run;
data want;
set dttest;
if index(patient_id,'18C') then tdiff=draw1-enttime;
format tdiff time8.;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.