BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Obsidian | Level 7

Hello,

 

I would like to assign seq number for each id's each "stop" activity group .  if two stops are back to back, it will be counted as 1. how can I create "sseq" using example dataset ds1?

 

data ds1 ;
input id seq stop;
cards ;
1 1 0
1 2 1
1 3 0
1 4 0
1 5 1
1 6 1
2 1 0
2 2 0
2 3 1
2 4 1
2 5 1
2 6 0
2 7 1
2 8 0
2 9 1
;
run;

 

The expected output:

stataq_0-1701710045105.png

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Lapis Lazuli | Level 10

data step.

data want;
	set ds1;
	by id;
	temp1=lag(stop);
	if 1=stop=temp1 then temp2=0; 
	else temp2=stop; 
	retain temp3; 
	if first.id then temp3 =temp2;
	else temp3= temp3+temp2;
	if stop=0 then sstop=.;
	else sstop=temp3;  
	drop temp:;
proc print; run; 

View solution in original post

2 REPLIES 2
A_Kh
Lapis Lazuli | Level 10

data step.

data want;
	set ds1;
	by id;
	temp1=lag(stop);
	if 1=stop=temp1 then temp2=0; 
	else temp2=stop; 
	retain temp3; 
	if first.id then temp3 =temp2;
	else temp3= temp3+temp2;
	if stop=0 then sstop=.;
	else sstop=temp3;  
	drop temp:;
proc print; run; 
FreelanceReinh
Jade | Level 19

Hello @stataq,

 

Try this:

data want(drop=_c);
set ds1;
by id seq;
if first.id then _c=0;
_c+(stop & (~lag(stop) | first.id));
if stop then sseq=_c;
run;

and add more test cases, e.g., id=3 beginning with stop=1 (if this is possible in your real data).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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