BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lalaktgrau
Fluorite | Level 6

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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. moving always starts no later than disease
  2. Only moving start or disease starts they never go back to zero.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

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

 

lalaktgrau
Fluorite | Level 6

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. 

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
lalaktgrau
Fluorite | Level 6

oh! Sorry. I had changed the datepart for the creation of another variable. I edited it! It should be good now!

mkeintz
PROC Star

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. moving always starts no later than disease
  2. Only moving start or disease starts they never go back to zero.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
lalaktgrau
Fluorite | Level 6

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1147 views
  • 0 likes
  • 3 in conversation