Quartz | Level 8

## Retain statement

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
Lapis Lazuli | Level 10

## Re: Retain statement

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;

11 REPLIES 11
Lapis Lazuli | Level 10

## Re: Retain statement

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;

Quartz | Level 8

## Re: Retain statement

Hi,

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

maybe retain

Regards

Lapis Lazuli | Level 10

## Re: Retain statement

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;

Quartz | Level 8

## Re: Retain statement

Hi,

What is notsorted?is it an option?

Whats the use of it?could you please explain

Regards

Opal | Level 21

## Re: Retain statement

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
Quartz | Level 8

## Re: Retain statement

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

Calcite | Level 5

## Re: Retain statement

-----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;

Onyx | Level 15

## Re: Retain statement

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? Haikuo

Calcite | Level 5

## Re: Retain statement

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?

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

Onyx | Level 15

Haikuo

Super User

## Re: Retain statement

select distinct * from have ;

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