DATA Step, Macro, Functions and more

Add datetime

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Add datetime

Dear Experts,

 

I have an existing dataset with datetime format. I want to add a datetime range 03/18/2017-03/20/2017.

 

data have1;

input datevalues ;

informat datevalues datetime22.;

format datevalues datetime22.;

cards;

11MAR2017:00:00:00.000

12MAR2017:00:00:00.000

13MAR2017:00:00:00.000

14MAR2017:00:00:00.000

15MAR2017:00:00:00.000

16MAR2017:00:00:00.000

17MAR2017:00:00:00.000

;

run;

 

 

 

%let start_date=11MAR2017:00:00:00.000;

%let end_date=16MAR2017:00:00:00.000;

 

data have2;

do datevalues="&start_date"dt to "&end_date"dt;

output;

end;

format datevalues datetime22.3;

run;

proc append base=have1 data=have2;

run;

 

 

desired output.

11MAR2017:00:00:00.000

12MAR2017:00:00:00.000

13MAR2017:00:00:00.000

14MAR2017:00:00:00.000

15MAR2017:00:00:00.000

16MAR2017:00:00:00.000

17MAR2017:00:00:00.000

18MAR2017:00:00:00.000

19MAR2017:00:00:00.000

20MAR2017:00:00:00.000

 

thanks,


Accepted Solutions
Solution
‎04-17-2017 03:06 PM
PROC Star
Posts: 7,492

Re: Add datetime

Explanation: Since I used a loop with a datetime variable, the by 86400 (the number of seconds in a day) is included to ensure that only one record is output for each day.

 

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
PROC Star
Posts: 7,492

Re: Add datetime

I think the following does what you want:

 

data have1;
  input datevalues;
  informat datevalues datetime22.;
  format datevalues datetime22.;
  cards;
11MAR2017:00:00:00.000
12MAR2017:00:00:00.000
13MAR2017:00:00:00.000
14MAR2017:00:00:00.000
15MAR2017:00:00:00.000
16MAR2017:00:00:00.000
17MAR2017:00:00:00.000
;
run;

%let start_date=18MAR2017:00:00:00.000;
%let end_date=20MAR2017:00:00:00.000;

data have1;
  set have1 end=eof;
  output;
  if eof then do datevalues="&start_date"dt to "&end_date"dt by 86400;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

Contributor
Posts: 58

Re: Add datetime

Art297,

 

what does it mean by 86400; could you give me some clarification.

thanks,

kesete

PROC Star
Posts: 768

Re: Add datetime

It is the amount of seconds in a day. 24 hours * 60 minutes * 60 seconds = 86400 seconds Smiley Happy

Solution
‎04-17-2017 03:06 PM
PROC Star
Posts: 7,492

Re: Add datetime

Explanation: Since I used a loop with a datetime variable, the by 86400 (the number of seconds in a day) is included to ensure that only one record is output for each day.

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 147 views
  • 1 like
  • 3 in conversation