DATA Step, Macro, Functions and more

Retain statement

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

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


Accepted Solutions
Solution
‎12-06-2012 01:17 PM
Super Contributor
Posts: 1,636

Re: Retain statement

Posted in reply to robertrao

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


All Replies
Solution
‎12-06-2012 01:17 PM
Super Contributor
Posts: 1,636

Re: Retain statement

Posted in reply to robertrao

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;

Super Contributor
Posts: 1,041

Re: Retain statement

Hi,

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

maybe retain

Regards

Super Contributor
Posts: 1,636

Re: Retain statement

Posted in reply to robertrao

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;

Super Contributor
Posts: 1,041

Re: Retain statement

Hi,

What is notsorted?is it an option?

Whats the use of it?could you please explain

Regards

Respected Advisor
Posts: 4,931

Re: Retain statement

Posted in reply to robertrao

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
Super Contributor
Posts: 1,041

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

Contributor
Posts: 45

Re: Retain statement

Posted in reply to robertrao

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

Respected Advisor
Posts: 3,156

Re: Retain statement

Posted in reply to joehinson

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

Contributor
Posts: 45

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?


See the link below:

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

Respected Advisor
Posts: 3,156

Re: Retain statement

Posted in reply to joehinson

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

Haikuo

Super User
Posts: 10,044

Re: Retain statement

Posted in reply to robertrao

select distinct * from have ;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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