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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.