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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

7 REPLIES 7
ballardw
Super User

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.

Nikos
Fluorite | Level 6

Sorry..

It is sorted by ID

Thank you

gergely_batho
SAS Employee

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

arodriguez
Lapis Lazuli | Level 10

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;

Nikos
Fluorite | Level 6

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

Nikos
Fluorite | Level 6

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

Ksharp
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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