BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mgrasmussen
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION
2 REPLIES 2

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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