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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
art297
Opal | Level 21

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

tekish
Quartz | Level 8

Art297,

 

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

thanks,

kesete

PeterClemmensen
Tourmaline | Level 20

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

art297
Opal | Level 21

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

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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