Sample 24620: Create a SAS datetime variable from multiple variables
Use the DHMS function to create a SAS datetime from a SAS date, along with hour, minute, and second variables. See Sample 1 on the Full Code tab. To create a SAS datetime value from a SAS date and a SAS time, see Sample 2 on the Full Code tab.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
In sample 1 and 2, the DATE function creates Sdate as today's SAS date. This variable along with Hr, Min, and Sec are combined in the DHMS function to create a SAS datetime in Sample 1. In Sample 2, the SAS date and SAS time variables create the datetime value.
/* Sample 1: Variables for date, hour, minute and seconds */
data datetime;
if _n_=1 then sdate=date();
retain sdate;
format sasdt dateampm22. bid dollar6.0;
input hr min sec bid dollar6.;
sasdt=dhms(sdate,hr,min,sec);
keep sasdt bid ;
datalines;
8 00 25 $200
8 10 14 $300
9 24 30 $400
;
run;
proc print;
run;
/* Sample 2 : Variables for date and time */
data datetime;
if _n_=1 then sdate=date();
retain sdate;
format sasdt dateampm22. bid dollar6.0;
input stime time8. +1 bid dollar6.;
/* Use zero placeholders for the H and M parameters */
sasdt=dhms(sdate,0,0,stime);
keep sasdt bid ;
datalines;
10:30:30 $450
11:49:20 $465
13:44:12 $475
;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Sample 1: Variables for date, hour, minute and seconds
Obs sasdt bid
1 14AUG2004:08:00:25 AM $200
2 14AUG2004:08:10:14 AM $300
3 14AUG2004:09:24:30 AM $400
Sample 2: Variables for date and time
Obs sasdt bid
1 14AUG2004:10:30:30 AM $450
2 14AUG2004:11:49:20 AM $465
3 14AUG2004:01:44:12 PM $475
Use the DHMS function to create a SAS datetime value.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Date and Time
|
| Date Modified: | 2005-12-16 03:02:55 |
| Date Created: | 2004-09-30 14:08:58 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |