DATA Step, Macro, Functions and more

incremental counting until variable is ne 1

Reply
N/A
Posts: 0

incremental counting until variable is ne 1

I'd like to create a count variable that incrementally counts successive observations until my counted variable is not equal to 1, and then restarts counting when the variable is equal to 1 again.
So here's my dataset:

date time turb
03/17/2008 1200 1
03/17/2008 1215 1
03/17/2008 1230 1
03/17/2008 1245 0
03/17/2008 1300 1
03/17/2008 1315 1

What I want is:
date time turb count
03/17/2008 1200 1 1
03/17/2008 1215 1 2
03/17/2008 1230 1 3
03/17/2008 1245 0 0
03/17/2008 1300 1 1
03/17/2008 1315 1 2

Any ideas for how to do this?
kellyv
Super Contributor
Super Contributor
Posts: 3,174

Re: incremental counting until variable is ne 1

Posted in reply to deleted_user
Using a DATA step, a conditional assignment statement, illustrated as:

IF THEN ;

Since you will be incrementing as mentioned, you can use SAS implicit RETAIN logic - have a look at the RETAIN statement for your counter variable.

And when you reach the ".. is not equal t 1" condition, reset your counter variable to zero.

Scott Barry
SBBWorks, Inc.
Super Contributor
Super Contributor
Posts: 3,174

Re: incremental counting until variable is ne 1

From the SAS support http://support.sas.com/ website, here is some SAS DATA Step programming documentation:

Adding Information to a SAS Data Set

Understanding the Assignment Statement
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001315210.htm


Understanding DATA Step Processing
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304324.htm
N/A
Posts: 0

Re: incremental counting until variable is ne 1

Posted in reply to deleted_user
You could try something like this:

data test;
infile datalines delimiter = "" ;
input date anydtdte10. time 5. turb ;
datalines ;
03/17/2008 1200 1
03/17/2008 1215 1
03/17/2008 1230 1
03/17/2008 1245 0
03/17/2008 1300 1
03/17/2008 1315 1
;
DATA TEST1;
SET TEST;
RETAIN count;
if turb eq 1 then do;
count+1;
end;
if turb ne 1 then do;
count = 0;
end;
Format date date9.;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 150 views
  • 0 likes
  • 2 in conversation