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

I have a table of values with the relevant column containing information on which rows need to be combined in my final table that looks like this:

 

to_combine

1

.

1

1

.

1

1

1

1

.

1

.

.

 

I would like to create a new variable that creates a new group for each of these "groups of 1s" that would look like this

to_combine

group

1

1

.

.

1

2

1

2

.

.

1

3

1

3

1

3

1

3

.

.

1

4

.

.

.

.

 

How would I go about creating this?

 

I tried creating an "obs" variable and setting it so that if to_combine = 1 then obs = lag(obs). However, that seems to set obs to the last row that had to_combine = 1 instead of the immediately preceding row. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Thanks

 

data want;
    set have;
    if _n_=1 then newgroup=1;
    if missing(to_combine) then newgroup+1;
    if not missing(to_combine) then group=newgroup;
    else group=.;
    drop newgroup;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You don't state explicitly what the rule is for creating a new group. It always helps to be clear and not expect us to figure it out (sometimes we get it wrong). So is the separator for a new group one missing value? What if there are multiple missing values in a row? 

--
Paige Miller
Ani7
Obsidian | Level 7

That's my bad. Essentially all consecutive 1s in the to_combine column should have a unique group number (missing rows are indicative of a new group number that needs to be started).

PaigeMiller
Diamond | Level 26

Thanks

 

data want;
    set have;
    if _n_=1 then newgroup=1;
    if missing(to_combine) then newgroup+1;
    if not missing(to_combine) then group=newgroup;
    else group=.;
    drop newgroup;
run;
--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1285 views
  • 1 like
  • 2 in conversation