ISO 8601 and SAS A Practical Approach Derek

  • Slides: 21
Download presentation
ISO 8601 and SAS : A Practical Approach ® Derek Morgan PAREXEL International

ISO 8601 and SAS : A Practical Approach ® Derek Morgan PAREXEL International

Section 1: What Is ISO 8601?

Section 1: What Is ISO 8601?

The ISO 8601 Standard • Internationally accepted methodology describing dates and times using numbers

The ISO 8601 Standard • Internationally accepted methodology describing dates and times using numbers • Standard covers: – Dates – Time of day – Date and time – Coordinated Universal Time (UTC) – Local time with offset to UTC – Time intervals and recurring time intervals

The ISO 8601 Standard • Removes problems of: – Translation • August can be

The ISO 8601 Standard • Removes problems of: – Translation • August can be expressed as “août”; “август”; “agosto”; “八月” – Local norms for expressing dates • 03 -08 -2016 • Is it March 8, 2016 or August 3, 2016?

Basic vs. Extended Notation • Basic is just the numbers • Extended uses delimiters

Basic vs. Extended Notation • Basic is just the numbers • Extended uses delimiters Basic Notation 20160614 T 1422 20160614 T 14223774 Extended Notation 2016 -06 -14 T 14: 22 2016 -06 -14 T 14: 22: 37. 74

Section 2: SAS and ISO 8601 ®

Section 2: SAS and ISO 8601 ®

A Few Basics • SAS maintains dates and times as numbers relative to a

A Few Basics • SAS maintains dates and times as numbers relative to a reference point – Needs an exact number • ISO 8601 allows for missing components – In standard SAS dates and times, missing components=missing value • SAS can handle ISO dates with missing components – Separate from normal SAS dates and times if you need it

Reading Extended Notation ISO 8601 Dates and Times into SAS ® • Use informats

Reading Extended Notation ISO 8601 Dates and Times into SAS ® • Use informats - No different than any other complete SAS date, time or datetime • Create a SAS datetime from an ISO 8601 datetime: – xxxdtm = INPUT(xx. DTC, E 8601 DT. ); • Create a SAS date from an ISO 8601 date (or datetime): – xxxdt = INPUT(xx. DTC, E 8601 DA. ); • BUT pick the time from the complete SAS datetime created above – xxxtm = TIMEPART(xxxdtm);

Reading Basic Notation ISO 8601 Dates and Times into SAS ® • The informats

Reading Basic Notation ISO 8601 Dates and Times into SAS ® • The informats for basic notation begin with the letter “B” – Create a SAS datetime with B 8601 DT. – Create a SAS date with B 8601 DA. – STILL pick the time from the complete SAS datetime you created using TIMEPART() WARNING!!! Basic notation informats will substitute 0 for missing time components, and 1 for missing day or month components!

Basic vs. Extended Notation Informats ISO 8601 Character String 2014 -03 -26 T 16:

Basic vs. Extended Notation Informats ISO 8601 Character String 2014 -03 -26 T 16: 14 2014 -06 -24 2414 -09 Read with E 8601 DT. Read with B 8601 DT. 26 MAR 2014: 16: 14: 00 24 JUN 2014: 00: 00 01 SEP 2014: 00: 00 You may wind up with an unexpected imputation using the basic notation informat!

Writing ISO 8601 Dates and Times in SAS the Normal Way ® • Use

Writing ISO 8601 Dates and Times in SAS the Normal Way ® • Use formats - No different than any other complete SAS date, time or datetime. • IF dt_value is a valid SAS datetime value: – vsdtc = PUT(dt_value, E 8601 DT. ) creates an ISO 8601 datetime – vsdt = PUT(dt_value, E 8601 DN. ) creates an ISO 8601 date from a datetime – vstm = PUT(TIMEPART(dt_value), E 8601 TM. )

Writing ISO 8601 Dates and Times in SAS the Normal Way ® • IF

Writing ISO 8601 Dates and Times in SAS the Normal Way ® • IF dt_value is a valid SAS date: – vsdt = PUT(dt_value, E 8601 DA. ) creates an ISO 8601 date

SAS ISO 8601 Facility ® • Stored internally in CHARACTER variables – Needs CHARACTER

SAS ISO 8601 Facility ® • Stored internally in CHARACTER variables – Needs CHARACTER formats and informats to make any sense, just like normal dates and times – Can accommodate missing components – Can perform simple, automatic imputation in calculations • This may not be what you want • Recommend doing any imputation before using this for calculations

How SAS Stores ISO 8601 Values ® SAS Formatted Datetime SAS Datetime Value 2015

How SAS Stores ISO 8601 Values ® SAS Formatted Datetime SAS Datetime Value 2015 -04 -20 T 15: 05: 30 1745161530 20 APR 2015: 05: 30 2016 -12 -22 T 06: 40 1798008000 22 DEC 2016: 06: 40: 00 2014 -01 -15 2015 -09 2017 -02 -15 T 02 2016 Original ISO 8601 String SAS ISO 8601 Internal Value 2015420150530 FFD 2016 C 220640 FFFFD 2014115 FFFFD 20159 FFFFFD 201721502 FFFFFFD 2016 FFFFFFD

ISO 8601 Durations and Intervals • Duration describes a period of time: PT 17

ISO 8601 Durations and Intervals • Duration describes a period of time: PT 17 H 41 M P 2 DT 10 H 30 M • • Intervals describe start and end of a period 3 Forms: Datetime/Datetime: 2016 -08 -05 T 17: 19/2016 -08 -06 T 11: 00 Datetime/Duration: 2016 -08 -05 T 17: 19/P 0 Y 0 M 0 DT 17 H 41 M 0 S Duration/Datetime: P 0 Y 0 M 0 DT 17 H 41 M 0 S/2016 -08 -06 T 11: 00

How Do You Calculate ISO Durations and Intervals? 1. Divide end date-start date by

How Do You Calculate ISO Durations and Intervals? 1. Divide end date-start date by several factors to get years, months, days, hours, etc. - or 1. Use the SAS interval function INTCK() to get years, months, days, hours, etc. 2. Concatenate the results with the appropriate delimiters

There’s An Easier Way DATA duration 1; SET full_datetimes; LENGTH aedur $ 32; CALL

There’s An Easier Way DATA duration 1; SET full_datetimes; LENGTH aedur $ 32; CALL IS 8601_CONVERT('dt/dt', 'du', aestdtm, aeendtm, aedur); FORMAT aedur $N 8601 E. ; RUN; Event Start Date/Time Event End Date/Time Duration 22 DEC 2016: 00: 00 23 DEC 2016: 00: 00 P 1 D 15 JAN 2014: 00: 00 04 JUN 2014: 00: 00 P 4 M 20 D 25 FEB 2013: 00: 00 16 MAY 2017: 00: 00 P 4 Y 2 M 19 D

The IS 8601_CONVERT Routine • Syntax: CALL IS 8601_CONVERT(convert-from, convertto, <from-variables>, <to-variables>, <date-timereplacements>); •

The IS 8601_CONVERT Routine • Syntax: CALL IS 8601_CONVERT(convert-from, convertto, <from-variables>, <to-variables>, <date-timereplacements>); • Works with SAS ISO datetimes and SAS datetimes, interchangeably.

And That’s the Practical of it. . . • The ISO 8601 standard is

And That’s the Practical of it. . . • The ISO 8601 standard is an internationally accepted methodology describing dates and times using numbers • There are informats to read ISO 8601 dates and times • There are formats to write ISO 8601 dates and times • SAS can handle missing components in ISO 8601 dates and times - separate facility from normal dates and times • The IS 8601_CONVERT routine calculates ISO durations easily

Any Questions? • References: • Harnessing the Power of SAS® ISO 8601 Informats, Formats,

Any Questions? • References: • Harnessing the Power of SAS® ISO 8601 Informats, Formats, and the CALL IS 8601_CONVERT Routine; Kim Wilson, SAS Institute Inc. , Cary, NC, USA, Pharma. SUG 2012 • The Essential Guide to SAS® Dates and Times, Second Edition. Cary, NC: SAS Institute Inc. ; Morgan, Derek P. 2014.

Thank You! Derek Morgan, PAREXEL International mrdatesandtimes@gmail. com

Thank You! Derek Morgan, PAREXEL International mrdatesandtimes@gmail. com