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

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
;

1 ACCEPTED SOLUTION
3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

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;

 

 

Thanks,
Jag
andreas_lds
Jade | Level 19

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;

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
  • 3 replies
  • 945 views
  • 0 likes
  • 4 in conversation