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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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