BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BU2B
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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

8 REPLIES 8
Reeza
Super User

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;

BU2B
Calcite | Level 5

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

esiid.JPG

stat_sas
Ammonite | Level 13

Something like this may generate desired output.

data esiid3;

set recorder;

retain esiid1;

if recorder = 1 then esiid1=ESIID;

run;

BU2B
Calcite | Level 5

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

Reeza
Super User

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;

BU2B
Calcite | Level 5

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

stat_sas
Ammonite | Level 13

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;

BU2B
Calcite | Level 5

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

Steve

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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