I have a dataset that has multiple groups of observations. There is a variable, flag, which either takes value 1 or missing. I want to create a new variable to divide each group into subgroups based on the value of flag. Whenever flag = 1, start counting a new subgroup. Please see the code below as an example.
data have;
	input group flag;
	datalines;
101 .
101 .
101 1
101 .
101 1
101 .
101 .
102 .
102 1
102 .
102 .
;
data want;
	input group flag new_var;
	datalines;
101 . 1
101 . 1
101 1 2
101 . 2
101 1 3
101 . 3
101 . 3
102 . 1
102 1 2
102 . 2
102 . 2
;data want;
set have;
by group;
if first.group
then newvar = 1;
else if flag then newvar + 1;
run;
					
				
			
			
				
			
			
			
				
			
			
			
			
			
		please try the below code for flag2
data have;
	input group flag;
	datalines;
101 .
101 .
101 1
101 .
101 1
101 .
101 .
102 .
102 1
102 .
102 .
;
data want;
set have;
by group flag notsorted;
retain new_var;
if first.group then new_var=1;
else new_var+flag;
run;
data want;
set have;
by group;
if first.group
then newvar = 1;
else if flag then newvar + 1;
run;
					
				
			
			
				
			
			
			
			
			
			
			
		If you want a more arcane solution, try:
data want;
	set have;
	by group;
	length new_var 8;
	retain new_var;
		
	new_var = ifn(first.group, 1, new_var) + sum(0, flag);
run;
					
				
			
			
				
			
			
			
			
			
			
			
		It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.