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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 5190 views
  • 9 likes
  • 6 in conversation