DATA Step, Macro, Functions and more

Removing repeat IDs

Reply
Frequent Contributor
Posts: 78

Removing repeat IDs

I have a data set structured as follows:

 

ID                repeat             firstID

1                     0

2                     0

3                     1                   2

4                     0          

5                     0

6                     1                  4,5

 

 

So I need to keep only the new IDs for patients who were repeats. In this example, I would need to remove 2, 4, 5. Some have multiple previous IDs, so there is a comma separating them. What is the most efficient way to do this?

 

 

Super Contributor
Posts: 441

Re: Removing repeat IDs

Making some assumptions about the format of the data and the variables you want to keep in the output this should give you what you're asking for

 

data have;
	length id $1 repeat 8. firstid $3;
	infile datalines dlm='09'x missover truncover;
	input id repeat firstid;
	datalines;
1	0
2	0
3	1	2
4	0
5	0
6	1	4,5
;
run;

proc sql;
	create table repeats
	as select firstid
	from have
	where firstid ne "";
quit;

data repeatslong(keep=id);
	set repeats;
	if count(firstid,",") > 0 then do;
		do i = 1 to (count(firstid,",")+1);
			id=scan(firstid,i,",");
			output;
		end;
	end;
	else do;
		id=firstid;
		output;
	end;
run;

proc sql;
	create table want
	as select id, repeat
	from have
	where id not in
		(select id
		 from repeatslong)
	;
quit;

 

Respected Advisor
Posts: 4,173

Re: Removing repeat IDs

[ Edited ]

@Melk

I tend to use SAS hash tables to create black-lists or white-lists. Below an example.

data have;
	length id $1 repeat 8. firstid $3;
	infile datalines dlm='09'x truncover;
	input id repeat firstid;
	datalines;
1	0
2	0
3	1	2
4	0
5	0
6	1	4,5
;
run;

data want(drop=_:);

  if _n_=1 then
    do;
      dcl hash h1();
      h1.defineKey('id');
      h1.defineDone();

      do until(last);
        set have(keep=firstid) end=last;
        if not missing(firstid) then
          do _i=1 by 1;
            id=scan(firstid,_i,',');
            if missing(id) then leave;
            h1.ref();
          end;
      end;
    end;

    set have;
    if h1.check() then output;

run;

 

PROC Star
Posts: 102

Re: Removing repeat IDs

@Melk:

You can also do it with a relatively simple datastep solution:

data have;
  length id $1 repeat 8. firstid $3;
  infile cards truncover;
  input id repeat firstid;
cards;
1 0
2 0
3 1 2
4 0
5 0
6 1 4,5
;
run;

data drop;
  set have;
  where firstid;
/* parse firstid */ do _N_=1 to countw(firstid,',');
id=scan(firstid,_N_,','); output; end; keep id; run; proc sort data=drop; by id; run; data want; merge have drop(in=drop); by id; if not drop; run;
Ask a Question
Discussion stats
  • 3 replies
  • 120 views
  • 3 likes
  • 4 in conversation