BookmarkSubscribeRSS Feed
eric2
Calcite | Level 5

data fakedata;

input id var rslt;

datalines;

101 1 .
101 2 0
101 3 .
101 4 1
101 5 1
101 6 0
101 7 1
101 8 0
101 9 0
101 10 0
102 1 .
102 2 0
102 3 0
102 4 1
102 5 1
102 6 1
102 7 0
102 8 0
102 9 0
102 10 0
;

run;

 

i want to count two consecutive 1's and flag that var and then three consecutive 0s and then flag
the top record . so lets say we create 2 new variables flag_1 and flag_0
so for id=101 we will have flag_1=4 and flag_0=7
id=102 we will have flag_1=4 and flag_0=6

once thats flagged i need to count the missing rslt before flag_1 was identified meaning
for id=101 we identified flag_1=4 , so before var=4 we have 2 missing records so i need another variable flag_miss_count=2
for id=101 flag_miss_count=1
any help appreciated.

 

the resulting data should be like 

id var rslt flag_1 flag_0 flag_miss_count
101 1 .
101 2 0
101 3 .                                       2
101 4 1       4
101 5 1
101 6 0
101 7 1
101 8 0                    7
101 9 0
101 10 0
102 1 .                                      1
102 2 0
102 3 0
102 4 1   4
102 5 1
102 6 1
102 7 0                  6
102 8 0
102 9 0
102 10 0

 

thanks,

eric

2 REPLIES 2
Anotherdream
Quartz | Level 8

Can you better explain what you mean when you say i want to count two consecutive 1's and flag that var and then three consecutive 0s and then flag the top record?

 

I can't tell from your example what this means.  For the first flag do you pick observation 4 because it is the starting observation before 2 consecutive 1's?

 

Likewise, why is flag_0 7 in group 101? 

  Is this because you don't have 3 consecutive 0's until observation 8, so you want the code to flag observation 8 and then subtract one?

 

It's a  bit confusion because flag_0 is also the 3rd zero in your 101 group as well as your 102 group... So i can't tell if you are simply trying to find the 3rd occurrence of the value 0, or the start of 3 0's in a row based upon your example data.

 

 

 

If i'm following you, why does flag_1 not subtract 1  but flag_0 subtract one?

eric2
Calcite | Level 5

Hi,

 we can create one variable and capture the result but for easy understanding i created three different variables.

basically first logic we need to count consecutive 1s which occur more than twice within a subject and flag the first record where that started, similarly next logic within the same subject we need to count consecutive 0s which occur more than thrice but for this we need to flag the previous record. so order of checking will be consecutive 1s and then consecutive 0s within same subject

 

thanks

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
  • 2 replies
  • 1066 views
  • 0 likes
  • 2 in conversation