SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

Hi Team,

For a particular patient there might be many procedures done.But I want to eliminate the duplicates of the same procedure if any.......

patient      proc_code

101           CPT

101          CPT

101         CPT

101         SPT

101         OMT

I want:

patient      proc_code

101           CPT

101         SPT

101         OMT

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

one way:

data have;

input patient      proc_code $;

cards;

101           CPT

101          CPT

101         CPT

101         SPT

101         OMT

;

proc sort data=have out=want nodupkey;

by patient      proc_code;

run;

proc print;run;

View solution in original post

11 REPLIES 11
Linlin
Lapis Lazuli | Level 10

one way:

data have;

input patient      proc_code $;

cards;

101           CPT

101          CPT

101         CPT

101         SPT

101         OMT

;

proc sort data=have out=want nodupkey;

by patient      proc_code;

run;

proc print;run;

robertrao
Quartz | Level 8

Hi,

Thanks so much...is there any way i can use this with out the nodupkey??

maybe retain

Regards

Linlin
Lapis Lazuli | Level 10

I don't think you can do it by retain.

another way:

data want;

  set have;

  by patient proc_code notsorted;

  if last.proc_code;

proc print;run;

robertrao
Quartz | Level 8

Hi,

What is notsorted?is it an option?

Whats the use of it?could you please explain

Regards

PGStats
Opal | Level 21

Notsorted means just that: the data is not sorted. But you want first. and last. automatic variables to reflect every change in the sequence anyway, as if it was sorted. - PG

PG
robertrao
Quartz | Level 8

Hi,

This code still has duplicates of patient and proc_code.Do not know the reason.but they are intermingled with other data.

I think it is good for me to follow the nodupkey

Regards

joehinson
Calcite | Level 5

-----yet another way:

data have;

input patient      proc_code $;

cards;

101           CPT

101          CPT

101         CPT

101         SPT

101         OMT

;

run;

data _null_;

      if(1=2)then set have;

      declare hash prc(dataset:"have",ordered:"a");

      rc=prc.defineKey("patient","proc_code");

      rc=prc.defineDone();

      rc=prc.output(dataset:"want");

run;

Haikuo
Onyx | Level 15

Joe, I have to ask, Is there a particular reason you choose to use  'if (1=2)' instead of 'if 0', since (1=2) would always be 0?Smiley Wink Haikuo

joehinson
Calcite | Level 5

Aha, Hai.Kuo the observant one!

IF(0) versus IF(1=2):        I'm still brainstorming for suitable answers:

(1) "Different strokes for different folks (like CARDS vs DATALINES)".

(2) "Less mystifying to FORTRAN programmers".

(3) "Better understood by SAS debutantes".

(4) "Personal signature. (if you see that on planet Mars, then you know I'm there)"

(5) "Just being a little mischievous, getting the processor to spend an extra nanosecond".

(6) "More palatable to the human Visual and Prefrontal cortices".

(7) "More mathematically appropriate".

(8) ?

(9) ?

- -

COULD THERE BE A CATCH?


See the link below:

http://www.math.toronto.edu/mathnet/falseProofs/first1eq2.html

Haikuo
Onyx | Level 15

I like your answer, you made my day, Joe.:smileylaugh:

Haikuo

Ksharp
Super User

select distinct * from have ;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 11 replies
  • 2081 views
  • 3 likes
  • 6 in conversation