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,
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
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
Art297,
what does it mean by 86400; could you give me some clarification.
thanks,
kesete
It is the amount of seconds in a day. 24 hours * 60 minutes * 60 seconds = 86400 seconds 🙂
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
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.
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.