turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Data Management
- /
- Forum
- /
- Assign ID to pairs of consecutive rows

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 01:04 AM

Hi,

I have a dataset of individuals already sorted on a value. I would like to assign a pairID to the first two individuals, then the next pair, and so on. If the file contains an odd number of individuals, the last three are grouped to form a triplet. I hope that makes sense and would appreciate any tips.

Thanks!

HAVE | WANT | ||

ID | ID | pairID | |

1 | 1 | 1 | |

2 | 2 | 1 | |

3 | 3 | 2 | |

4 | 4 | 2 | |

5 | 5 | 3 | |

6 | 6 | 3 | |

7 | 7 | 4 | |

8 | 8 | 4 | |

9 | 9 | 5 | |

10 | 10 | 5 | |

11 | 11 | 5 |

Accepted Solutions

Solution

01-30-2017
12:15 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 01:16 AM

Playing around with some basic math this is relatively straightforward.

Functions:

MOD()

Use NOBS option on SET statement to get the number of observations.

Use RETAIN

Data want;

set have NOBS=num end =eof;

RETAIN count;

If mod(_n_, 2) ne 0 and not eof then count+1;

run;

All Replies

Solution

01-30-2017
12:15 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 01:16 AM

Playing around with some basic math this is relatively straightforward.

Functions:

MOD()

Use NOBS option on SET statement to get the number of observations.

Use RETAIN

Data want;

set have NOBS=num end =eof;

RETAIN count;

If mod(_n_, 2) ne 0 and not eof then count+1;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 10:42 AM

Of course I would choose to use @Reeza's solution myself, but FWIW, here is another approach outrageously not using MOD() ,

```
data want;
do _error_=1 to 2;
set sashelp.class end=last;
pairID=ifn(last and _error_=1,_n_-1,_n_);
output;
end;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2017 12:18 PM

Thank you, both, for taking time to resolve this for me. I can't believe how few lines of codes are needed. I understand the logic, but am just not advanced enough to come up with it myself. Thanks again for the help!