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