🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## choosing observations based on first occurence of a varaiable

Hi all,

I have a dataset as:

Id.no  Number  Flag

101      543         0

101      657         0

101      723         1

101      123         0

101       432        0

102        564        0

102        634       1

102       327        0

103        765       0

103         567      0

103        908       0

103        432       1

103       231        0

Can I get the observations upto the first occurrence of flag variable as '1' for each id no? I mean the output dataset as:

Id.no  Number  Flag

101      543         0

101      657         0

101      723         1

102        564        0

102        634       1

103        765       0

103         567      0

103        908       0

103        432       1

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: choosing observations based on first occurence of a varaiable

``````

data have;
input Id_no  Number  Flag;
cards;
101      543         0

101      657         0

101      723         1

101      123         0

101       432        0

102        564        0

102        634       1

102       327        0

103        765       0

103         567      0

103        908       0

103        432       1

103       231        0
;

data want;
do until(last.id_no);
set have;
by id_no;
if _n_ then output;
if flag then _n_=0;
end;
run;
proc print noobs;run;``````
Id_no Number Flag
101 543 0
101 657 0
101 723 1
102 564 0
102 634 1
103 765 0
103 567 0
103 908 0
103 432 1
4 REPLIES 4
Super User

## Re: choosing observations based on first occurence of a varaiable

``````data want;
set have;
by id;
retain write;
if first.id then write = 1;
if write then output;
if flag then write = 0;
run;``````

This makes use of the fact that 0 and missing are considered false, and any other non-missing value true.

Super User

## Re: choosing observations based on first occurence of a varaiable

Please take note that a variable does not "occur". It's either present in a dataset (and therefore the data step that reads it) or not. Values occur.

Tourmaline | Level 20

## Re: choosing observations based on first occurence of a varaiable

``````

data have;
input Id_no  Number  Flag;
cards;
101      543         0

101      657         0

101      723         1

101      123         0

101       432        0

102        564        0

102        634       1

102       327        0

103        765       0

103         567      0

103        908       0

103        432       1

103       231        0
;

data want;
do until(last.id_no);
set have;
by id_no;
if _n_ then output;
if flag then _n_=0;
end;
run;
proc print noobs;run;``````
Id_no Number Flag
101 543 0
101 657 0
101 723 1
102 564 0
102 634 1
103 765 0
103 567 0
103 908 0
103 432 1
Obsidian | Level 7

## Re: choosing observations based on first occurence of a varaiable

Thank you so much!!
Discussion stats
• 4 replies
• 457 views
• 1 like
• 3 in conversation