Dear community, In my data I want to use a counter variable that counts under special conditions. The counter should start to count everytime primary is equal to 4 until it stops being 4. In the case primary = 5 prior to primary = 4 the counter shouldn't start. The same goes (backwards looking) for a primary=5 which appears after a sequence of primary=4 (the counter should be set to zero for the whole sequence before). Think of it as the employment status where the counter counts the time in unemployment (primary=4). While this duration is just interesting if the individual was in the labor force before and stays in after, I don't want the duration to be count if primary = 5 (appears when the individual is not in the labor force). Here is an example(normally there are 15000 individuals and time is up to 1500): data work.sample; input id $ primary $ time ($); datalines; 1 5 1 1 5 2 1 4 3 1 4 4 1 4 5 1 1 6 2 1 1 2 1 2 2 4 3 2 4 4 2 4 5 2 5 6 3 1 1 3 4 2 3 4 3 3 4 4 3 1 5 3 1 6 ; I am able to set up the counter but so far I have no clue how to bring in my conditions on primary = 5 and the following and earlier periods. data test1; set test1; if primary = 4 and lag(primary) ^= 4 then duration = 1; if primary = 4 and lag(primary) = 4 then duration+1; if primary ^= 4 then duration = 0; run; The output in the end should look like this: input id $ primary $ time $ duration ($) 1 5 1 0 1 5 2 0 1 4 3 0 <- no count after because of the primary=5 1 4 4 0 1 4 5 0 1 1 6 0 2 1 1 0 2 1 2 0 2 4 3 0 2 4 4 0 2 4 5 0 2 5 6 0 <- no count before because of the primary=5 3 1 1 0 3 4 2 1 3 4 3 2 3 4 4 3 3 1 5 0 3 1 6 0 I would be really thankful for any help!
... View more