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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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