DATA Step, Macro, Functions and more

Insert one or many copies of a row when a condition is meet while correcting time stamp

Accepted Solution Solved
Reply
Occasional Contributor EBB
Occasional Contributor
Posts: 11
Accepted Solution

Insert one or many copies of a row when a condition is meet while correcting time stamp

What I have is

 

Value           Date_Time            Repeat_Flag

  20    22JAN18:16:27:29                    0

  10    22JAN18:16:27:30                    0

   5    22JAN18:16:27:31                     1

  20    22JAN18:16:27:33                    1

   5    22JAN18:16:27:36                     0

 

What I want is

 

Value           Date_Time            Repeat_Flag

  20    22JAN18:16:27:29                    0

  10    22JAN18:16:27:30                    0

   5    22JAN18:16:27:31                     1

  5    22JAN18:16:27:32                      0

 20    22JAN18:16:27:33                    1

 20    22JAN18:16:27:34                    0

 20    22JAN18:16:27:35                    0

  5    22JAN18:16:27:36                     0

 

I know that I could use the EST package to do this but I do not have the licence. This means I need to find another way. I already tried several things but nothing is working exactly as I need it to be.

 

Thanks a lot for your help Smiley Surprised)


Accepted Solutions
Solution
‎02-28-2018 01:45 AM
Frequent Contributor
Posts: 109

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

[ Edited ]

 

data have;
input Value:best12. Date_Time:datetime16.  Repeat_Flag:best12.;
format Date_Time datetime16.;
datalines;
20   22JAN18:16:27:29    0
10   22JAN18:16:27:30    0
5    22JAN18:16:27:31    1
20   22JAN18:16:27:33    1
5    22JAN18:16:27:36    0
;
run;

data want(drop=i j);
set have;
retain i;
	if _N_=1 then i=0;
	if Repeat_Flag=1 then do;
		output;
		i=i+1;
		do j = 1 to i by 1;
			Date_Time=Date_Time+1;
			Repeat_Flag=0;
			output;
		end;
	end;
	else do;
		output;
	end;
run;

 

 

 

Please let us know if this worked for you.

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,397

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

Post test data in the form of a datastep!

Also post what you have tried.

At a guess, as no data to work with:

data want;
  set have;
  retain lst_data_time;
  if _n_=1 then lst_date_time=date_time;
  else do;
    if date_time ne lst_date_time + 1 then do;
      old_dt=date_time;
      old_flag=repeat_flag;
      do i=lst_date_time+1 to date_time-1;
        date_time=date_time+i;
        repeat_flag=0;
        output;
      end;
      date_time=old_dt;
      repeat_flag=old_flag;
      output;
    end;
    else output;
  end;
run;
        
Occasional Contributor EBB
Occasional Contributor
Posts: 11

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

Hi RW9, you are right, next time I'll post the test data in a datastep format. 

Solution
‎02-28-2018 01:45 AM
Frequent Contributor
Posts: 109

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

[ Edited ]

 

data have;
input Value:best12. Date_Time:datetime16.  Repeat_Flag:best12.;
format Date_Time datetime16.;
datalines;
20   22JAN18:16:27:29    0
10   22JAN18:16:27:30    0
5    22JAN18:16:27:31    1
20   22JAN18:16:27:33    1
5    22JAN18:16:27:36    0
;
run;

data want(drop=i j);
set have;
retain i;
	if _N_=1 then i=0;
	if Repeat_Flag=1 then do;
		output;
		i=i+1;
		do j = 1 to i by 1;
			Date_Time=Date_Time+1;
			Repeat_Flag=0;
			output;
		end;
	end;
	else do;
		output;
	end;
run;

 

 

 

Please let us know if this worked for you.

 

Occasional Contributor EBB
Occasional Contributor
Posts: 11

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

Posted in reply to Satish_Parida

Thanks a lot Satish!

Super User
Posts: 6,625

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

[ Edited ]

I think you can do this just looking at Date_Time, and don't even need to consider Repeat_Flag.  I'll keep Repeat_Flag around just to be consistent with what you asked for:

 

data want;

set have end=done;

output;

if done=0;

_n_ = _n_ + 1;

set have (keep=Date_Time rename=(Date_Time = next_Date_Time)) point=_n_;

if next_Date_Time - Date_Time > 1;

Repeat_Flag = 0;

do Date_Time = Date_Time + 1 to next_Date_Time - 1;

   output;

end;

drop next_Date_Time;

run;

Super User
Posts: 10,681

Re: Insert one or many copies of a row when a condition is meet while correcting time stamp

data have;
input Value:best12. Date_Time:datetime16.  Repeat_Flag:best12.;
format Date_Time datetime16.;
datalines;
20   22JAN18:16:27:29    0
10   22JAN18:16:27:30    0
5    22JAN18:16:27:31    1
20   22JAN18:16:27:33    1
5    22JAN18:16:27:36    0
;
run;
data want; 
 merge have have(keep=Date_Time rename=(Date_Time=_Date_Time) firstobs=2);
 output;
 do i=Date_Time+1 to coalesce(_Date_Time-1,0);
  Date_Time=i;Repeat_Flag=0;output;
 end;
 drop i _Date_Time;
run;

proc print noobs;run;
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 137 views
  • 3 likes
  • 5 in conversation