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.
hi ... another way to reset the COUNT for each new group ...
data have;
set have;
count + (-(lag(var1) eq 'N')*count) + 1;
run;
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;
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;
Another option is DOW,
data want;
do count=1 by 1 until (var1='N');
set have;
output;
end;
run;
hi ... another way to reset the COUNT for each new group ...
data have;
set have;
count + (-(lag(var1) eq 'N')*count) + 1;
run;
Hi Mike,
I will go to the BASUG Q1 meeting on 4/17/2012 in Boston. Looking forward to meeting you there.
Thanks,
Linlin
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.
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
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.
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.