data have1;
input ln $ setup_dt Date9. ;
format setup_dt mmddyy10.;
datalines;
12000 29Jul2019
12000 30Jul2019
12000 30Jul2019
12000 31Jul2019
12500 31Aug2019
15600 1Aug2019
15700 2Aug2019
;run;
data want;
set have1;
by ln ;
if first.ln then group_id+1;
run;
Output
| ln | setup_dt | group_id |
| 12000 | 07/29/2019 | 1 |
| 12000 | 07/30/2019 | 1 |
| 12000 | 07/30/2019 | 1 |
| 12000 | 07/31/2019 | 1 |
| 12500 | 08/31/2019 | 2 |
| 15600 | 08/01/2019 | 3 |
| 15700 | 08/02/2019 | 4 |
| Desired as follows | ||
| ln | setup_dt | group_id |
| 12000 | 07/29/2019 | 1 |
| 12000 | 07/30/2019 | 2 |
| 12000 | 07/30/2019 | 2 |
| 12000 | 07/31/2019 | 3 |
| 12500 | 08/31/2019 | 1 |
| 15600 | 08/01/2019 | 1 |
| 15700 | 08/02/2019 | 1 |
The desire is that when the same ln shows a new setup_dt, it increments from 1 to 2 or 3 etc depending on the number of dates. It then should reset to 1 when the ln changes
data want;
set have1;
by ln setup_dt;
if first.ln then group_id=1;
else if first.setup_dt then group_id+1;
run;
data want;
set have1;
by ln setup_dt;
if first.ln then group_id=1;
else if first.setup_dt then group_id+1;
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.