Help using Base SAS procedures

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

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

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,023

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

View solution in original post


All Replies
Super User
Posts: 11,343

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: 144

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

Posted in reply to gergely_batho

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

Posted in reply to arodriguez

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

Posted in reply to gergely_batho

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,023

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 278 views
  • 0 likes
  • 5 in conversation