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

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
  • 3 replies
  • 405 views
  • 1 like
  • 2 in conversation