BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nasman
Calcite | Level 5

Hi,

I am not really sure how to explain this or what term to use so excuse me on the subject Smiley Happy so i have this sample dataset below that i need to assign an incremental field (GROUP_ID), for every time it sees a 'B' status GROUP_ID ill have a +1 please see expected result for more info.

**sample data

ROW_NUM     STATUS    

1                    A

2                    A

3                    B

4                    A

5                    A

6                    A

7                    B

8                    A

9                    A

10                  B

**expected result

ROW_NUM     STATUS     GROUP_ID

1                    A               1

2                    A               1

3                    B               1

4                    A               2

5                    A               2

6                    A               2

7                    B               2

8                    A               3

9                    A               3

10                  B               3

cheers guys.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

No need for excuses. You give us sample data, an explanation and and example of how the desired result should look like. That's all that's normally required to understand.

data have;

input ROW_NUM STATUS $;

datalines;

1 A

2 A

3 B

4 A

5 A

6 A

7 B

8 A

9 A

10 B

;

run;

data want;

  set have;

  by STATUS notsorted;

  if _n_=1 then group_id=1;

  else if first.status and status='A' then group_id+1;

run;

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

No need for excuses. You give us sample data, an explanation and and example of how the desired result should look like. That's all that's normally required to understand.

data have;

input ROW_NUM STATUS $;

datalines;

1 A

2 A

3 B

4 A

5 A

6 A

7 B

8 A

9 A

10 B

;

run;

data want;

  set have;

  by STATUS notsorted;

  if _n_=1 then group_id=1;

  else if first.status and status='A' then group_id+1;

run;

nasman
Calcite | Level 5

That was fast, thank you so much mate! it worked :smileygrin:

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
  • 2 replies
  • 404 views
  • 0 likes
  • 2 in conversation