Solved
Contributor
Posts: 68

# Flagging out the first occurence of a sub-bygroup based on different values

Hi all,

I have the data set

ID     Status

1         V

1         V

1         V

1         I

1         I

2         I

2         I

2         I

3         V

3         I

3         I

3         I

and I would like to flag out the first occurrence for each different value of STATUS within the by - group (:by ID)

ID     Status      Flag

1         V            1

1         V            0

1         V            0

1         I             1

1         I             0

2         I             1

2         I             0

2         I             0

3         V           1

3         I            1

3         I            0

3         I            0

I would very much appreciate any help

Thank you

Nikos

Accepted Solutions
Solution
‎10-03-2014 07:32 AM
Super User
Posts: 10,770

## Re: Flagging out the first occurence of a sub-bygroup based on different values

don't want sort dataset ?

```data have;
input ID     Status     \$;
cards;
1         V
1         V
1         V
1         I
1         I
2         I
2         I
2         I
3         V
3         I
3         I
3         I
;
run;
data want(drop=n);
set have;
by id;
array x{99999} \$32 _temporary_ ;
flag=0;
if first.id then do;call missing(of x{*});n=0;end;
if status not in x then do; flag=1;n+1;x{n}=status;end;
run;

```

Xia Keshan

All Replies
Super User
Posts: 13,512

## Re: Flagging out the first occurence of a sub-bygroup based on different values

data want;

set have;

by id status notsorted;

if first.status then flag=1;

else flag=0;

run;

might work. You don't mention if the  data would be sorted on ID or status.

Contributor
Posts: 68

## Re: Flagging out the first occurence of a sub-bygroup based on different values

Sorry..

It is sorted by ID

Thank you

SAS Employee
Posts: 340

## Re: Flagging out the first occurence of a sub-bygroup based on different values

If the solution provided by ballardw is OK, just ignore this question:

What results do you want fo this:

ID     Status      Flag

1          V           1

1          I            1

1          V           ?

1          I            ?

1          I            0

Frequent Contributor
Posts: 146

## Re: Flagging out the first occurence of a sub-bygroup based on different values

I believe that this kind of table is not the one that it's using. It seems that the dataset is sorted by id and status, and your example no.

proc sort data=xxxx

by id status;

run;

data xxxx;

set xxxx;

by id status;

if first.status then flag=1;

else flag=0;

run;

Contributor
Posts: 68

## Re: Flagging out the first occurence of a sub-bygroup based on different values

Yes, it is sorted by ID .  STATUS is not sorted . Just represents the status in time..i.e.  VVVV until becomes I and subsequently remains I

Thank you

Contributor
Posts: 68

## Re: Flagging out the first occurence of a sub-bygroup based on different values

Hi,

It should be

1      V

1      V

1      I

1      I

V's are in succession until I occurs which continues as I unti the end of the by group

Thank you

Solution
‎10-03-2014 07:32 AM
Super User
Posts: 10,770

## Re: Flagging out the first occurence of a sub-bygroup based on different values

don't want sort dataset ?

```data have;
input ID     Status     \$;
cards;
1         V
1         V
1         V
1         I
1         I
2         I
2         I
2         I
3         V
3         I
3         I
3         I
;
run;
data want(drop=n);
set have;
by id;
array x{99999} \$32 _temporary_ ;
flag=0;
if first.id then do;call missing(of x{*});n=0;end;
if status not in x then do; flag=1;n+1;x{n}=status;end;
run;

```

Xia Keshan

🔒 This topic is solved and locked.