DATA Step, Macro, Functions and more

Duplicate Observation

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Duplicate Observation

Hi All,

I need to duplicate an observation while a condition is not true.  I want to duplicate esiid1 when the value in recorder does not equal 1.  Here's my latest attempt.

data esiid3;

     set recorder;

Do while (recorder ne 1);

     output esiid1;

end;

run;


Accepted Solutions
Solution
‎10-15-2014 04:36 PM
Trusted Advisor
Posts: 1,204

Re: Duplicate Observation

May be you can try this after weekend. Have a great long weekend!

data have;
set recorder;
if recorder=1 then grp+1;
run;

data esiid3 (drop=esiid1 grp rename=esiid2=esiid1);
set have;
by grp;
retain esiid2;
if first.grp then esiid2=esiid1;
run;

proc print data=esiid3;
run;

View solution in original post


All Replies
Super User
Posts: 17,868

Re: Duplicate Observation

Without any data, either what you have or need we're throwing darts in the dark:

data esiid3;

     set recorder;

if recorder ne 1 then output;

output;

run;

Contributor
Posts: 33

Re: Duplicate Observation

I need the value in esiid1 to duplicate until it encounters the next esiid1 where the recorder = 1.

esiid.JPG

Trusted Advisor
Posts: 1,204

Re: Duplicate Observation

Something like this may generate desired output.

data esiid3;

set recorder;

retain esiid1;

if recorder = 1 then esiid1=ESIID;

run;

Contributor
Posts: 33

Re: Duplicate Observation

Hi, no that just output a data set that matches the input set recorder.  I think retain is on the right path though! 

Super User
Posts: 17,868

Re: Duplicate Observation

recorder is a character variable (leading zeros), newrecorder is numeric so you're code would have to change according but I think @stat@Sas code would work otherwise.

data esiid3;

set recorder;

retain esiid1;

if newrecorder = 1 then esiid1=ESIID;

run;

Contributor
Posts: 33

Re: Duplicate Observation

Doh!  Sorry my bad on the newrecorder!  I still can't get the desired dataset though.  I'll play with it more Monday.  I'm out for a 4 day weekend!  Thanks for all the help folks!

Steve

Solution
‎10-15-2014 04:36 PM
Trusted Advisor
Posts: 1,204

Re: Duplicate Observation

May be you can try this after weekend. Have a great long weekend!

data have;
set recorder;
if recorder=1 then grp+1;
run;

data esiid3 (drop=esiid1 grp rename=esiid2=esiid1);
set have;
by grp;
retain esiid2;
if first.grp then esiid2=esiid1;
run;

proc print data=esiid3;
run;

Contributor
Posts: 33

Re: Duplicate Observation

Well that did the trick!  Thanks so much!  Have a good weekend!

Steve

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 284 views
  • 0 likes
  • 3 in conversation