Hi,
I'd like to create a variable that =1 when:
moving starts (first instance of moving=1) and continues being 1 until disease has continued for 3 minutes.Then, it becomes zero again.
Therefore, it only captures the onset of moving and the first 3 minutes of disease.
data WORK.CLASS; infile datalines truncover; input Name:$8. moving disease datetime; informat datetime datetime20.; format datetime datetime20.; datalines;
Alfred 0 0 04MAR14:23:55:00 Alfred 0 0 04MAR14:23:56:00 Alfred 1 0 04MAR14:23:57:00 Alfred 1 0 04MAR14:23:58:00 Alfred 1 0 04MAR14:23:59:00 Alfred 1 1 05MAR14:00:00:00 Alfred 1 1 05MAR14:00:01:00 Alfred 1 1 05MAR14:00:02:00 Alfred 1 1 05MAR14:00:03:00 Alfred 1 1 05MAR14:00:04:00 mary 0 0 04MAR14:23:55:00 mary 0 0 04MAR14:23:56:00 mary 0 0 04MAR14:23:57:00 mary 0 0 04MAR14:23:58:00 mary 0 0 04MAR14:23:59:00 mary 1 0 05MAR14:00:00:00 mary 1 1 05MAR14:00:01:00 mary 1 1 05MAR14:00:02:00 mary 1 1 05MAR14:00:03:00 mary 1 1 05MAR14:00:04:00; So if the variable was created properly, it would look like this: Alfred 0 0 04MAR14:23:55:00 0 Alfred 0 0 04MAR14:23:56:00 0 Alfred 1 0 04MAR14:23:57:00 1 Alfred 1 0 04MAR14:23:58:00 1 Alfred 1 0 04MAR14:23:59:00 1 Alfred 1 1 04MAR14:00:00:00 1 Alfred 1 1 04MAR14:01:01:00 1 Alfred 1 1 04MAR14:01:02:00 1 Alfred 1 1 04MAR14:01:03:00 0 Alfred 1 1 04MAR14:01:04:00 0
Now your data series skips from midnight to 1 hour after midnight. Is that intentional? I've changed them from 01:01:00, 01:02:00 to 00:01:00, 00:02:00, etc:
data WORK.CLASS;
infile datalines truncover;
input Name:$8. moving disease datetime;
informat datetime datetime20.;
format datetime datetime20.;
datalines;
Alfred 0 0 04MAR14:23:55:00
Alfred 0 0 04MAR14:23:56:00
Alfred 1 0 04MAR14:23:57:00
Alfred 1 0 04MAR14:23:58:00
Alfred 1 0 04MAR14:23:59:00
Alfred 1 1 05MAR14:00:00:00
Alfred 1 1 05MAR14:00:01:00
Alfred 1 1 05MAR14:00:02:00
Alfred 1 1 05MAR14:00:03:00
Alfred 1 1 05MAR14:00:04:00
mary 0 0 04MAR14:23:55:00
mary 0 0 04MAR14:23:56:00
mary 0 0 04MAR14:23:57:00
mary 0 0 04MAR14:23:58:00
mary 0 0 04MAR14:23:59:00
mary 1 0 05MAR14:00:00:00
mary 1 1 05MAR14:00:01:00
mary 1 1 05MAR14:00:02:00
mary 1 1 05MAR14:00:03:00
mary 1 1 05MAR14:00:04:00
run;
data want (drop=dis_dt);
set class;
by name;
format dis_dt datetime20.;
retain dummy dis_dt;
if first.name then do;
dummy=0;
dis_dt=constant('bigint'); /* Make a dummy date way in the future*/
end;
if moving=1 then dummy=1;
if dif(disease)=1 then dis_dt=datetime;
if datetime>dis_dt+'00:03:00't then dummy=0; /*Set actual disease date when encountered*/
run;
proc print;run;
This program assumes:
1.Why is everything 04MAR14 even after midnight?
2.Why is Mary excluded when it matches (first instance of moving=1) and continues being 1 until disease has continued for 3 minutes
It was just an example of times. This isn't my actual data, though it is very similar.
I just gave Alfred as a sample.
The sample may be synthetic, which is fine, but it doesn't even follow your description. Specifically, it is not in chronological order. This does not help us help you.
oh! Sorry. I had changed the datepart for the creation of another variable. I edited it! It should be good now!
Now your data series skips from midnight to 1 hour after midnight. Is that intentional? I've changed them from 01:01:00, 01:02:00 to 00:01:00, 00:02:00, etc:
data WORK.CLASS;
infile datalines truncover;
input Name:$8. moving disease datetime;
informat datetime datetime20.;
format datetime datetime20.;
datalines;
Alfred 0 0 04MAR14:23:55:00
Alfred 0 0 04MAR14:23:56:00
Alfred 1 0 04MAR14:23:57:00
Alfred 1 0 04MAR14:23:58:00
Alfred 1 0 04MAR14:23:59:00
Alfred 1 1 05MAR14:00:00:00
Alfred 1 1 05MAR14:00:01:00
Alfred 1 1 05MAR14:00:02:00
Alfred 1 1 05MAR14:00:03:00
Alfred 1 1 05MAR14:00:04:00
mary 0 0 04MAR14:23:55:00
mary 0 0 04MAR14:23:56:00
mary 0 0 04MAR14:23:57:00
mary 0 0 04MAR14:23:58:00
mary 0 0 04MAR14:23:59:00
mary 1 0 05MAR14:00:00:00
mary 1 1 05MAR14:00:01:00
mary 1 1 05MAR14:00:02:00
mary 1 1 05MAR14:00:03:00
mary 1 1 05MAR14:00:04:00
run;
data want (drop=dis_dt);
set class;
by name;
format dis_dt datetime20.;
retain dummy dis_dt;
if first.name then do;
dummy=0;
dis_dt=constant('bigint'); /* Make a dummy date way in the future*/
end;
if moving=1 then dummy=1;
if dif(disease)=1 then dis_dt=datetime;
if datetime>dis_dt+'00:03:00't then dummy=0; /*Set actual disease date when encountered*/
run;
proc print;run;
This program assumes:
This is great, thank you!
Quick question: Is there a way to include a DO UNTIL loop that would stop processing (that day for that person) after we reach the first consecutive 3 minutes of disease?
In other words, if later that night disease becomes 0 and then goes back to 1--I don't want to count them.
data WORK.CLASS;
infile datalines truncover;
input Name:$8. moving disease datetime;
informat datetime datetime20.;
format datetime datetime20.;
datalines;
Alfred 0 0 04MAR14:23:55:00
Alfred 0 0 04MAR14:23:56:00
Alfred 1 0 04MAR14:23:57:00
Alfred 1 0 04MAR14:23:58:00
Alfred 1 0 04MAR14:23:59:00
Alfred 1 1 05MAR14:00:00:00
Alfred 1 1 05MAR14:00:01:00
Alfred 1 1 05MAR14:00:02:00
Alfred 1 0 05MAR14:00:03:00
Alfred 1 1 05MAR14:00:04:00
mary 0 0 04MAR14:23:55:00
mary 0 0 04MAR14:23:56:00
mary 0 0 04MAR14:23:57:00
mary 0 0 04MAR14:23:58:00
mary 0 0 04MAR14:23:59:00
mary 1 0 05MAR14:00:00:00
mary 1 1 05MAR14:00:01:00
mary 1 1 05MAR14:00:02:00
mary 1 1 05MAR14:00:03:00
mary 1 1 05MAR14:00:04:00
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.