BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
deleted_user
Not applicable
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;

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

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1417 views
  • 0 likes
  • 2 in conversation