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

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

 

Please help me out for this . Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


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

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User
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.

novinosrin
Tourmaline | Level 20


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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 735 views
  • 1 like
  • 3 in conversation