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

Hi all,

I had this situation where I need to exclude all the ID's with history is 1 on the first observation. For example, in the below scenario there are multiple observations for each ID.  I want to delete all those ID's which had history of disease (=1) on the first observation. 

Data I have

IDOrder of occurrenceHistory
111
121
131
211
310
321
410
510
521

Data i want to keep

IDOrder of occurrenceHistory
310
321
410
510
521

 I tried so many time but i can't get the result I want. Please help me. Thanks in advance for the support. 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Assuming your data are already sorted properly, try this:

data have;
input ID	Order_of_occurrence	History;
cards;
1	1	1
1	2	1
1	3	1
2	1	1
3	1	0
3	2	1
4	1	0
5	1	0
5	2	1
;
run;

data want;
  do until(last.ID);
    set have;
    by ID;

    if first.ID = history then _N_ = 0;
    if _N_ then output;
  end;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Assuming your data are already sorted properly, try this:

data have;
input ID	Order_of_occurrence	History;
cards;
1	1	1
1	2	1
1	3	1
2	1	1
3	1	0
3	2	1
4	1	0
5	1	0
5	2	1
;
run;

data want;
  do until(last.ID);
    set have;
    by ID;

    if first.ID = history then _N_ = 0;
    if _N_ then output;
  end;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



htht
Calcite | Level 5
Thank you Bart for your support.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 424 views
  • 1 like
  • 2 in conversation