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.
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;
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?
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).
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.