Sample 51769: Converting SAS® date, time, and datetime values to DS2 date, time, and timestamp data types
Date, time, and datetime values in the DATA step language are stored in numeric variables. Date values represent the number of days since January 1, 1960. Time values represent the number of seconds since midnight. Datetime values represent the number of seconds since January 1, 1960. In the DS2 language, the numeric value is stored in a double data type.
The DS2 language supports SQL style date, time, and timestamp data types common in most databases. To convert SAS date, time, and datetime values to the corresponding date, time, and timestamp data types, use the TO_DATE, TO_TIME, and TO_TIMESTAMP functions, as shown in the sample code 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.
The sample code below uses the TO_DATE, TO_TIME, and TO_TIMESTAMP functions to convert SAS date, time, and datetime values to their corresponding date, time, and timestamp data types.
/* Create sample data set */
data test;
format d_sas mmddyy10. t_sas time18.9 ts_sas datetime18.9;
d_sas = today();
t_sas = time();
ts_sas = datetime();
run;
proc ds2;
data _NULL_;
dcl date d_sql having format mmddyy10.;
dcl time t_sql having format time11.2;
dcl timestamp ts_sql having format datetime19.9;
dcl double d_sas having format mmddyy10.;
dcl double t_sas having format time11.2;
dcl double ts_sas having format datetime19.9;
method init();
put;
put ' SAS SQL';
put '------------------ ------------------';
end;
method run();
set test;
d_sql = to_date(d_sas);
put d_sas ' ' d_sql;
t_sql = to_time(t_sas);
put t_sas ' ' t_sql;
ts_sql = to_timestamp(ts_sas);
put ts_sas '' ts_sql;
end;
method term();
put;
end;
enddata;
run;
quit;
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.
SAS SQL
------------------ -------------------
12/06/2013 12/06/2013
12:48:57.98 12:48:57.98
06DEC13:12:48:57.98 06DEC13:12:48:57.98
Date Modified: | 2013-12-20 15:45:53 |
Date Created: | 2013-12-06 12:13:10 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9.4 TS1M0 | |
Z64 | 9.4 TS1M0 | |
Microsoft® Windows® for x64 | 9.4 TS1M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.4 TS1M0 | |
Microsoft Windows 8 Enterprise 32-bit | 9.4 TS1M0 | |
Microsoft Windows 8 Enterprise x64 | 9.4 TS1M0 | |
Microsoft Windows 8 Pro 32-bit | 9.4 TS1M0 | |
Microsoft Windows 8 Pro x64 | 9.4 TS1M0 | |
Microsoft Windows 8.1 Enterprise 32-bit | 9.4 TS1M0 | |
Microsoft Windows 8.1 Enterprise x64 | 9.4 TS1M0 | |
Microsoft Windows 8.1 Pro | 9.4 TS1M0 | |
Microsoft Windows 8.1 Pro 32-bit | 9.4 TS1M0 | |
Microsoft Windows Server 2008 R2 | 9.4 TS1M0 | |
Microsoft Windows Server 2008 for x64 | 9.4 TS1M0 | |
Microsoft Windows Server 2012 Datacenter | 9.4 TS1M0 | |
Microsoft Windows Server 2012 R2 Datacenter | 9.4 TS1M0 | |
Microsoft Windows Server 2012 R2 Std | 9.4 TS1M0 | |
Microsoft Windows Server 2012 Std | 9.4 TS1M0 | |
Windows 7 Enterprise x64 | 9.4 TS1M0 | |
Windows 7 Professional x64 | 9.4 TS1M0 | |
64-bit Enabled AIX | 9.4 TS1M0 | |
64-bit Enabled HP-UX | 9.4 TS1M0 | |
64-bit Enabled Solaris | 9.4 TS1M0 | |
HP-UX IPF | 9.4 TS1M0 | |
Linux for x64 | 9.4 TS1M0 | |
Solaris for x64 | 9.4 TS1M0 | |