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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: