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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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