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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1440 views
  • 1 like
  • 3 in conversation