Using a loop over observations

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Using a loop over observations

Hi all,

 

I posted a couple of days ago and still haven't been able to get this code to work. I need to recalculate times so that my observations are grouped together when they occur very closely to one another. 

 

Please see the example below

 

Here are my times (FOR THIS EXAMPLE) My data set is much more complicated than the example below.

 

1. 3:00

2. 3:01

3. 3:01

4. 3:02

5: 3:03

6: 3:04

7. 3:08

8. 3:10

 

 

I am creating a rule. The rule is that, if two or more obversations are within 3 minutes of the PREVIOUS obeservation then the recalculated time for that observation should match the previous observations:

 

 


Example: Obs 2. is 1 minute from Obs 1. so the Recalculated time for obs 2. should be 3:00. 

The problem is that when we move to obs 3, the rule doesn't work. The previous observations for Obs. 3 is obs. 2. 3:01 is 0 minutes from 3:01 so the rule is met and Obs 3 is recalculated as Obs 2.... or 3:01.

 

I need the rule to refer to the "Recalculated time" of the previous observation not the raw time.

 

So Obs 3. would need to refer to Obs 2.'s recalculated time so that Obs 3. would recalculate as 3:00 and not 3:01.

 

When I use this code: 

 

data raw2;
set raw;
by time;
if time - lag(time) <=180 then time1 = lag(time);
else if time-lag(time) >180 then time1 = time;
format time1 time5.;
run; 

 

I the code doesn't refer to the recalculated time so the goal is not met? 

 

My final output from the example above should be:

 

heygey.png


Accepted Solutions
Solution
‎07-15-2016 01:31 PM
Super User
Posts: 11,107

Re: Using a loop over observations

[ Edited ]

Try this.

 

data raw2;
   set raw;
   retain time1 0;
   If _n_=1 then time1=time; /*initialize time1*/
   if time - time1 > 180 then time1 = time;
   format time1 time5.;
run; 

View solution in original post


All Replies
Super User
Posts: 5,358

Re: Using a loop over observations

[ Edited ]

I can't test this right now, but it looks like it should work: 

 

data want;

set have;

if _n_= 1 then time1=time;

retain time1;

seconds_elapsed + dif(time);

if seconds_elapsed > 180 then do;

time1= time;

seconds_elapsed = 0;

end;

format time1 time5.;

run;

 

 

EDIT:  Sorry.  Saw your comment, and patched the program.  You should have all the observations now.

Contributor
Posts: 34

Re: Using a loop over observations

Hi Astounding,

That does not appear to work. It deletes all but two of the observations.

Thank you for helping though.
Solution
‎07-15-2016 01:31 PM
Super User
Posts: 11,107

Re: Using a loop over observations

[ Edited ]

Try this.

 

data raw2;
   set raw;
   retain time1 0;
   If _n_=1 then time1=time; /*initialize time1*/
   if time - time1 > 180 then time1 = time;
   format time1 time5.;
run; 
Contributor
Posts: 34

Re: Using a loop over observations

This works! Thank you sooooooooo much!!!!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 420 views
  • 3 likes
  • 3 in conversation