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

Hi,

What can I do to reset the counter on a given condition.

Example Data;

Variable1

a

b

c

N

q

r

a

w

e

N

usually you would use _N_ to sequence but how do I get this output

a 1

b 2

c 3

N 4

q 1

r  2

a 3

w 4

e 5

N 6

How do I get it to reset. Maybe I do not understand what _N_ denotes properly. Please help. Thanks in advance.

-Akber.

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

hi ... another way to reset the COUNT for each new group ...

data have;

set have;

count + (-(lag(var1) eq 'N')*count) + 1;

run;  

View solution in original post

7 REPLIES 7
Linlin
Lapis Lazuli | Level 10

how about:

data have;

input var1$;

cards;

a

b

c

N

q

r

a

w

e

N

;

data temp;

  set have;

retain count;

if lag(var1)='N' then count=1;

else count+1;

run;

proc print;run;

Tom
Super User Tom
Super User

Another method is to reset the counter on the record that marks the end of the group.  But do it AFTER writing the current count to the output dataset.

data want;

   set have;

   count+1;

   output;

   if var1='N' then count=0;

run;

Haikuo
Onyx | Level 15

Another option is DOW,

data want;

do count=1 by 1  until (var1='N');

   set have;

   output;

end;

run;

MikeZdeb
Rhodochrosite | Level 12

hi ... another way to reset the COUNT for each new group ...

data have;

set have;

count + (-(lag(var1) eq 'N')*count) + 1;

run;  

Linlin
Lapis Lazuli | Level 10

Hi Mike,

I will go to the BASUG Q1 meeting on 4/17/2012 in Boston. Looking forward to meeting you there.

Thanks,

Linlin

akberali67
Calcite | Level 5

This does work but it is very inefficient. It takes hours to run on even a small dataset. Probably because it assigns the values manually.

Ksharp
Super User
data have;
input var1 $;
cards;
a
b
c
N
q
r
a
w
e
N
;
run;
data have;
 set have;
 if lag(var1) eq 'N' then count=0;
 count+1;
run;


Ksharp

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 6839 views
  • 9 likes
  • 6 in conversation