Dear SAS experts
I would like to create a variable that includes running numbers (1, 2 ..) of consecutive 1s according to a variabel (num), but I want to do this seperately for the values of each id. I.e. when the consecutive 1s cross two ids in the ordered dataset I would like running numbering to start over (as is the case in the variable want_var). The difference between the variable want_var and have_var can be seen below after running the code. Here, the 1s cross id 2 and 3, but using my code the numbering does not start over. Rather, it continues the count. Is there some simple way to modify my code such that I can get the variable that I am looking for?
data have;
input id order num want_var;
datalines;
1 1 0 0
1 2 1 1
1 3 1 2
1 4 0 0
2 1 0 0
2 2 1 1
3 1 1 1
3 2 1 2
3 3 0 0
;
run;
data have;
set have;
have_var+num;
if num=0 then have_var=0;
run;
Thank you
data want;
set have;
by id;
if first.id or num = 0 then want_var = 0;
want_var + num;
run;
data want;
set have;
by id;
if first.id or num = 0 then want_var = 0;
want_var + num;
run;
Perfect. So simple. Thank you.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.