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

Hi all I need help in deleting first observation only if a particular ID is seen at Location 0 more than one time. If any ID has just one observation at Location =0 then, I need to keep it (for example ID #2).  I have to keep all observations at Location = 1. If any ID has 3 entries for Location = 0 then, I need to keep the last one and remove the first two (although I may not have any of this situations). Any ID with just Location = 1 will also be retained irrespective of the number of times it occurs. I have indicated the observations that I want to keep under Status.

 

ID                      Location   Status

1                         0              Remove

1                         0              Keep

1                         1             Keep

2                         0             Keep

2                          1           Keep

2                         1            Keep

3                          0         Keep

3                          1         Keep

4                          1            Keep

4                          1          Keep

4                          1            Keep

5                          1            Keep

6                          0        Remove

6                          0       Remove

6                         0           Keep

6                        1          Keep

6                           1     Keep

 

 

 

Thank you for the help in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
franriv
Obsidian | Level 7

this will do:

 

data test;
	input id location status $;
	datalines;
1 0 Remove
1 0 Keep
1 1 Keep
2 0 Keep
2 1 Keep
2 1 Keep
3 0 Keep
3 1 Keep
4 1 Keep
4 1 Keep
4 1 Keep
5 1 Keep
6 0 Remove
6 0 Remove
6 0 Keep
6 1 Keep
6 1 Keep
;
run;

proc sort data=test; by id location;

data test;
	set test;
	by id location;
	if not (location eq 0 and not last.location);
run;

View solution in original post

4 REPLIES 4
franriv
Obsidian | Level 7

this will do:

 

data test;
	input id location status $;
	datalines;
1 0 Remove
1 0 Keep
1 1 Keep
2 0 Keep
2 1 Keep
2 1 Keep
3 0 Keep
3 1 Keep
4 1 Keep
4 1 Keep
4 1 Keep
5 1 Keep
6 0 Remove
6 0 Remove
6 0 Keep
6 1 Keep
6 1 Keep
;
run;

proc sort data=test; by id location;

data test;
	set test;
	by id location;
	if not (location eq 0 and not last.location);
run;
novinosrin
Tourmaline | Level 20


data have;
input ID                      Location   ;
cards;
1                         0              Remove
1                         0              Keep
1                         1             Keep
2                         0             Keep
2                          1           Keep
2                         1            Keep
3                          0         Keep
3                          1         Keep
4                          1            Keep
4                          1          Keep
4                          1            Keep
5                          1            Keep
6                          0        Remove
6                          0       Remove
6                         0           Keep
6                        1          Keep
6                           1     Keep
;
 
proc sql;
create table want as
select distinct *
from have(where=( location=0))
union all
select  *
from have
where location ne 0
order by id,location;
quit;
novinosrin
Tourmaline | Level 20

Hash fun:

 


data have;
input ID                      Location   ;
cards;
1                         0              Remove
1                         0              Keep
1                         1             Keep
2                         0             Keep
2                          1           Keep
2                         1            Keep
3                          0         Keep
3                          1         Keep
4                          1            Keep
4                          1          Keep
4                          1            Keep
5                          1            Keep
6                          0        Remove
6                          0       Remove
6                         0           Keep
6                        1          Keep
6                           1     Keep
;
data _null_;
if _n_=1 then do;
 dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("id","location") ;
   h.definedone () ;
end;
set have end=lr;
if location=0 then h.replace();else h.add();
if lr;
h.output(dataset:'want');
run;
 
novinosrin
Tourmaline | Level 20

And More Hash Fun:

 

data have;
input ID                      Location   ;
cards;
1                         0              Remove
1                         0              Keep
1                         1             Keep
2                         0             Keep
2                          1           Keep
2                         1            Keep
3                          0         Keep
3                          1         Keep
4                          1            Keep
4                          1          Keep
4                          1            Keep
5                          1            Keep
6                          0        Remove
6                          0       Remove
6                         0           Keep
6                        1          Keep
6                           1     Keep
;
data _null_;
if _n_=1 then do;
 dcl hash H (dataset:'have(where=(location=0))',duplicate:'r',ordered: "A",multidata:'y') ;
   h.definekey  ("id","location") ;
   h.definedone () ;
end;
set have(where=(location ne 0)) end=lr;
rc=h.add();
if lr;
h.output(dataset:'want');
run;

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
  • 509 views
  • 0 likes
  • 3 in conversation