BookmarkSubscribeRSS Feed
Victor_Guer_93
Fluorite | Level 6

I have the next data set(example):

 

ID delay

1  1  

1  2

1  3

1  4

1  3

1  4

1  0

1  1

1  2

 

I am trying to compute the number of times the variable delay reaches 4 or more since the first time that variable is 4 or more on the next 12 observations. If the observation is 13 or more since the first delay=4 then initialize the count.

3 REPLIES 3
ballardw
Super User

And what do you expect the output to look like?

 

Since you include rules like "on the next 12 observations. If the observation is 13 or more " you example data should include enough values to exercise those rules and should show the result for when you have fewer than 12 as well as more than 13.

 

Also, what role does ID play in the process? Do you have multiple values for ID? If so should this count or whatever be only within levels of the ID or not?

 

 

Victor_Guer_93
Fluorite | Level 6

Here it is a more detailed data, there are multiple ID's:

ID delay      

1  1  

1  2

1  3

1  4

1  3

1  4

1  0

1  1

1  2

1 3

1 4

1 0 

1 1

1 2

1 3

1 4

1 1

1 2

1 3

1 4

1 0

1 1

2 0 

2 0 

2 1

. .

. .

. .

 

Output:

 

(Missing in the count variable is  like a mark of initialization of the count)

 

ID delay Count

1  1          0 

1  2          0

1  3          0

1  4          .

1  3          0

1  4          1

1  0          1

1  1          1

1  2          1

1 3           1

1 4           2

1 0           2

1 1          2

1 2          2

1 3          2

1 3          2

1 2         0

1 4          .

1 3          0

1 4          1

1 0          1

1 1          1

2 0           0

2 0           0

2 1           0

. .             .

. .             .

. .            .

 

 

ballardw
Super User

This may get you started. Note use of the code box opened with the forum's {I} icon for code and a data step for example data.

data have;
input ID delay  ;
datalines;
1  1  
1  2
1  3
1  4
1  3
1  4
1  0
1  1
1  2
1 3
1 4
1 0 
1 1
1 2
1 3
1 4
1 1
1 2
1 3
1 4
1 0
1 1
2 0 
2 0 
2 1
;
run;

data want;
   set have;
   by notsorted id;
   retain count first4 .;
   if first.id then do;
      count=0;
      first4=0;
   end;
   if delay=4 then do;
      if first4=0 then do;
         count=.;
         first4=1;
      end;
      else count+1;
   end;
   else count=coalesce(count,0);
   drop first4;
run;

I don't see anything that demonstrates what you meant by the greater than 13 though. So I'm not sure what that does.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 682 views
  • 0 likes
  • 2 in conversation