Using SAS® Dates and Times

Slides:



Advertisements
Similar presentations
Dates and Times SAS Date, Time and Date- Time Formats.
Advertisements

Examples from SAS Functions by Example Ron Cody
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Basic And Advanced SAS Programming
Creating SAS® Data Sets
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
FORMAT FESTIVAL AN INTRODUCTION TO SAS® FORMATS AND INFORMATS By David Maddox.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Introduction to Using the Data Step Hash Object with Large Data Sets Richard Allen Peak Stat.
Creating and Using Custom Formats for Data Manipulation and Summarization Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
YET ANOTHER TIPS, TRICKS, TRAPS, TECHNIQUES PRESENTATION: A Random Selection of What I Learned From 15+ Years of SAS Programming John Pirnat Kaiser Permanente.
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
Copyright 2009 The Little Engine That Could: Using EXCEL LIBNAME Engine Options to Enhance Data Transfers between SAS® and Microsoft® Excel Files William.
Basic SAS Functions in Version 8.2 Kim Michalski Office of the Actuary Rick Andrews Office of Research, Development, and Information.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
CALENDAR YEAR PLAN JANUARY FEBRUARY MARCH APRIL MAY JUNE.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
DATA TYPES.
Data Manipulation in SAS
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own.
Built-in Functions.
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Open Source Server Side Scripting Permissions & Users
Excel Tutorial 3 Calculating Data with Formulas and Functions
Chapter 3: Working With Your Data
How to… Use Crontab for SFX
DAYS OF THE WEEK.
Instructor: Raul Cruz-Cano
Conditional Processing
Fall 2017 Questions and Answers (Q&A)
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
SAS Essentials How SAS Thinks
Guide To UNIX Using Linux Third Edition
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
A M P M Name: ________ Voice Log Week of __/__/__ Monday Tuesday
Introduction to SAS A SAS program is a list of SAS statements executed in order Every SAS statement ends with a semicolon! SAS statements can be in caps.
Data Upload & Management
Date Functions Farrokh Alemi, Ph.D.
Defining and Calling a Macro
Introduction to DATA Step Programming: SAS Basics II
MY FUNDAY.
DATING FOR SAS PROGRAMMERS – Paper 80 ABOUT THE PRESENTER
SEPTEMBER 2014 Unit 1 Unit 1 Continued Unit 1 Continued
Automating SAS through the Power of VB Script
Unit 1: Quick Quizzes After 5,13
Today: Surviving in a Post-Zombie World
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
SEPTEMBER 2014 Unit 1 Unit 1 Unit 1 Unit 1 Quick Quiz 9,16, 21 Unit 1
SEPTEMBER ½ Day Unit PLC
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Extended Christmas Hours Thursday December 8th 9am -6:30pm Friday December 9th 9am -6:30pm Saturday December 10th 9am-6pm Thursday December.
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
| January Sunday Monday Tuesday Wednesday Thursday Friday
Data Manipulation (with SQL)
JUNE 2010 CALENDAR PROJECT PLANNING 1 Month MONDAY TUESDAY WEDNESDAY
Contact
A M P M Name: ________ Fluency Log Week of __/__/__ Monday Tuesday
HTML Forms What are clients? What are servers?
DECEMBER READING LOG Student: When you have read, record your minutes and have your parent initial the proper box (each day). At the end of the month,
Go COMO route changes June 2019
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
Graphing Study Guide A graph is a way to illustrate information using pictures. A graph has several items that must always be included *A title – tells.
Reviewing Abbreviations

January Monday Tuesday Wednesday Thursday Friday Saturday Sunday 30 31
Working with dates and times
Presentation transcript:

Using SAS® Dates and Times PHILASUG April 19, 2017 Jonas V. Bilenas

Using SAS Dates and Time Using DATES and TIMES in SAS can be difficult to grasp for many programmers This tutorial will illustrate how to manipulate dates and times in SAS, focusing on: DATE and TIME constants FORMATS INFORMATS YEARCUTOFF PICTURE FORMATS Speaking of FORMATS: Range Specification for user FORMATS FUNCTIONS Using DATE and TIME in macros

DATE and TIME Constants DATE and TIME Constants: Inserting constant date and time values into SAS variables options nocenter; data _null_; car_svc_dt = '20DEC2006'd; car_scv_tm = '08:48't; car_scb_dt = '20DEC2006:08:48'dt; put _all_; run; ‘ddmmmyyyy’d ‘hh:mm:ss’t ‘ddmmyyyy:hh:mm:ss’dt

DATE and TIME Constants options nocenter; data _null_; car_svc_dt = '20DEC2006'd; car_scv_tm = '08:48't; car_scb_dt = '20DEC2006:08:48'dt; put _all_; run; ‘ddmmmyyyy’d ‘hh:mm:ss’t ‘ddmmyyyy:hh:mm:ss’dt car_svc_dt=17155 car_scv_tm=31680 car_scb_dt=1482223680 _ERROR_=0 _N_=1 Number of Seconds since midnight 1/1/1960 = 17155*86400+31680 Stored as an Numeric Number of DAYS since 1/1/1960 Stored as an Interger Number of Seconds since midnight Stored as an Numeric

DATE and TIME FORMATS 1 options nocenter; 2 data _null_; put car_svc_dt= date9. /car_svc_dt= mmddyy9. /car_svc_dt= worddate18. / /car_scv_tm= hhmm5. /car_scv_tm= time8. /car_scv_tm= tod8. /car_scb_dt= datetime16. ; run; car_svc_dt=20DEC2006 car_svc_dt=12/20/06 car_svc_dt=December 20, 2006 car_scv_tm=8:48 car_scv_tm=8:48:00 car_scv_tm=08:48:00 car_scb_dt=20DEC06:08:48:00 1 options nocenter; 2 data _null_; 3 td='22Jan2015'd; 4 put td= td=date9. td=yymmp7.; 5 stop; 6 run; td=20110 td=22JAN2015 td=2015.01

DATE and TIME Constants DATE and TIME Constants: Inserting constant date and time values into SAS variables options nocenter; data _null_; car_svc_dt = '20DEC06'd; car_scv_tm = '08:48't; car_scb_dt = '20DEC06:08:48'dt; put _all_; run; ‘ddmmmyyyy’d or ‘ddmmmyy’d ‘hh:mm:ss’t ‘ddmmyyyy:hh:mm:ss’dt Same output as page 4

DATE and TIME Constants DATE and TIME Constants: Inserting constant date and time values into SAS variables 16 17 data _null_; 18 car_svc_dt = '20DEC18'd; 19 put car_svc_dt=date9.; 20 run; car_svc_dt=20DEC2018

DATE and TIME Constants DATE and TIME Constants: Inserting constant date and time values into SAS variables 16 17 data _null_; 18 car_svc_dt = '20DEC21'd; 19 put car_svc_dt=date9.; 20 run; car_svc_dt=20DEC1921

DATE and TIME Constants DATE and TIME Constants: Inserting constant date and time values into SAS variables 16 Proc options; run; YEARCUTOFF=1920 Cutoff year for DATE and DATETIME informats and functions

YEARCUTOFF OPTION Default is 1920 1920 00-19: 20YY 20-99: 19YY

DATE and TIME INFORMATS data _null_; input @1 car_svc_dt date7. @10 car_scv_tm time8. @20 car_scb_dt datetime16. ; put car_svc_dt= date9. +5 car_scv_tm= hhmm5. +5 car_scb_dt= datetime16.; datalines; 20DEC06 8:48 20DEC06:08:48:00 11oct06 9:34 20DEC06:09:11:14 run; car_svc_dt=20DEC2006 car_scv_tm=8:48 car_scb_dt=20DEC06:08:48:00 car_svc_dt=11OCT2006 car_scv_tm=9:34 car_scb_dt=20DEC06:09:11:14

DATE & TIME FUNCTIONS Creating dates from numeric or character variables data _null_; month='06'; day='15'; year='02'; date = mdy(input(month,2.),input(day,2.),input(year,2.)); put date=; run; date=15506

DATE & TIME FUNCTIONS Works with TIME elements data _null_; hrid=hms(12,45,10); put hrid / hrid time. ; run; 45910 12:45:10

DATE & TIME FUNCTIONS Going the other way (getting components of a SAS date) data _null_; dt = today(); day = day(dt); month = month(dt); year = year(dt); qtr = qtr(dt); weekday = weekday(dt); put _all_; run; dt=17186 day=20 month=1 year=2007 qtr=1 weekday=7 _ERROR_=0 _N_=1

DATE & TIME FUNCTIONS DATE functions work on DATES. TIME function works on TIME Variables 239 data _null_; 240 day = day('01feb94:8:45'dt); 241 put day=; 242 run; NOTE: Invalid argument to function DAY at line 240 column 9. day=. day=. _ERROR_=1 _N_=1 245 day = day(datepart('01feb94:8:45'dt)); 246 put day=; 247 run; day=1

DATE & TIME FUNCTIONS Interval Functions 3rd argument: basis. data _null_; dob = '15dec1998'd; age = yrdif(dob,today(),'act/act'); daysold = datdif(dob,today(),'act/act'); put dob=DOWNAME.; put _all_; run; dob=Tuesday dob=14228 age=18.249315068 daysold=6666 _ERROR_=0 _N_=1 3rd argument: basis. datdiff: 30/360: 30 days in month and 360 in a year act/act: actual days in each month and year

DATE & TIME FUNCTIONS Interval Function: INTCK data _null_; lp = '28feb2008'd; lp1 = '01mar2008'd; day_dif = intck('day',lp,lp1); mon_dif = intck('month',lp,lp1); yr_dif = intck('year',lp,lp1); put _all_; run; lp=17590 lp1=17592 day_dif=2 mon_dif=1 yr_dif=0 _ERROR_=0 _N_=1

DATE & TIME FUNCTIONS Interval Function: INTNX data _null_; lp = '28feb2008'd; lpb = intnx('month',lp,1); /*default is 'b' */ lpm = intnx('month',lp,1,'m'); lpe = intnx('month',lp,1,'e'); put lp=date9. lpe=date9. lpem=date9. lpee=date9.; run; lp=28FEB2008 lpb=01MAR2008 lpm=16MAR2008 lpe=31MAR2008

DATE & TIME FUNCTIONS Anchoring dates data _null_; Your macroeconomic data maybe anchored to the first of the month or last but your matching date may not be anchored to those dates. data _null_; asofdate = ’01AUG1994’d; refdate = ’04AUG1994’d; refdate = intnx('month', refdate ,0); put asofdate=date9. refdate=date9.; run; asofdate=01AUG1994 refdate=01AUG1994

DATE & TIME FUNCTIONS Anchoring dates data _null_; Your macroeconomic data maybe anchored to the first of the month or last but your matching date may not be anchored to those dates. data _null_; asofdate= '31AUG1994'd; refdate = '04AUG1994'd; refdate1 = intnx('month', refdate ,0,'e'); refdate2 = intnx('month', refdate, 1) -1; put asofdate=date9. refdate=date9. refdate1=date9. refdate2=date9.; run; asofdate=31AUG1994 refdate=04AUG1994 refdate1=31AUG1994 refdate2=31AUG1994

Picture Formats Date and Time Directives in PICTURE FORMATS proc format; picture dt low-high = 'TIME STAMP: %A %B %d, %Y.' (datatype=date) ; picture tm low-high = '%I:%M.%S%p' (datatype=time) data _null_; file print; now = today(); tm = time(); put now dt40. tm tm.; run; TIME STAMP: Thursday March 16, 2017. 2:2.23PM

Picture Formats Available Directives %a Locale's abbreviated weekday name %A Locale's full weekday name %b Locale's abbreviated month name %B Locale's full month name %d Day of the month as a decimal number (1-31), with no leading zero %H Hour (24-hour clock) as a decimal number (0-23), with no leading zero %I Hour (12-hour clock) as a decimal number (1-12), with no leading zero %j Day of the year as a decimal number (1-366), with no leading zero %m Month as a decimal number (1-12), with no leading zero %M Minute as a decimal number (0-59), with no leading zero %p Locale's equivalent of either AM or PM %S Second as a decimal number (0-59), with no leading zero %U Week number of the year (Sunday as the first day of the week) as a decimal number (0,53), with no leading zero %w Weekday as a decimal number (1= Sunday, 7=Saturday) %y Year without century as a decimal number (0-99), with no leading zero %Y Year with century as a decimal number

Speaking of FORMATS Specifying DATE Ranges in FORMATS 27JAN07 cost=1.3 proc format; value cost '01jan2007'd - '10jan2007'd = '1.10' '11jan2007'd - '20jan2007'd = '1.20' '21jan2007'd - '31jan2007'd = '1.30' ; data _null_; cost = input(put(today(),cost.),4.2); put "&sysdate " cost=; run; 27JAN07 cost=1.3

Using Date/Time with MACROS Global Macro Variable: &SYSDATE data SGF_&sysdate; … run; NOTE: The data set WORK.SGF_27JAN07 has 100 observations and 2 variables.

Using Date/Time with MACROS Adding date and time to ODS Output ODS TAGSETS.EXCELXP file=“MORE_REPORTS.&sysdate..%sysfunc(compress(&systime.,':')).xls“ style=SASWeb; UNIX listing: -rw-rw-r-- 1 sy1pjb6 saslic 3487833 14 Mar 21:07 Summary.14MAR17.2107.xls -rw-rw-r-- 1 sy1pjb6 saslic 109137 14 Mar 21:07 OTHER_REPORTS.14MAR17.2107.xls -rw-rw-r-- 1 sy1pjb6 saslic 108732 14 Mar 21:08 MODELLING_TEAM_INPUTS.14MAR17.2107.xls -rw-rw-r-- 1 sy1pjb6 saslic 190537 14 Mar 21:08 RATES.14MAR17.2107.xls -rw-rw-r-- 1 sy1pjb6 saslic 110963 14 Mar 21:08 MORE_REPORTS.14MAR17.2107.xls

Using Date/Time with MACROS %SYSFUNC functions with Macro variables as arguments proc contents data=work._all_; title "Contents of data created today: %sysfunc(date(),worddate.)"; title2 "The time is: %sysfunc(time(),time.)"; run;

Using Date/Time with MACROS %SYSFUNC functions with Macro variables as arguments Contents of data created today: January 27, 2007 13:50 Saturday, January 27, 2007 2 The time is: 13:50:19 The CONTENTS Procedure Directory Libref WORK Engine V9 Physical Name C:\DOCUME~1\Owner\LOCALS~1\Temp\SAS Temporary Files\_TD2032 File Name C:\DOCUME~1\Owner\LOCALS~1\Temp\SAS Temporary Files\_TD2032 Member File # Name Type Size Last Modified 1 FORMATS CATALOG 17408 27Jan07:13:50:19 2 SGF_27JAN07 DATA 5120 27Jan07:13:50:19

Planning for Mother’s Day data _null_; dt = '14may2017'd; put dt=weekdatx29.; run; dt=Sunday, 14 May 2017

Planning for Mother’s Day data _null_; dt = '14may2017'd; *put dt=weekdatx29.; /* go back 8 workdays */ back_8_workdays = intnx('weekday',dt,-8); put back_8_workdays=weekdatx29.; run; back_8_workdays=Tuesday, 2 May 2017

Planning for Mother’s Day data _null_; dt = '14may2017'd; *put dt=weekdatx29.; /* go back 8 workdays */ back_8_workdays = intnx('weekday',dt,-8); *put back_8_workdays=weekdatx29.; /* You do not want to go out on mothers day. How about the Saturday 1 week before Mothers Day */ eopw = intnx('week',dt,-1,'e'); put eopw=weekdatx29.; run; eopw=Saturday, 13 May 2017

ANY DATE? options datestyle=mdy; data new; input date anydtdte10.; put date date9.; datalines; 01/13/2003 13/01/2003 13jan2003 13jan03 13/01/03 01/02/03 03/02/01 ; run; 13JAN2003 03JAN2013 02JAN2003 02MAR2001

Code to join macroeconmic data Data is coming in from a spreadsheet. Make sure the date field is defined as a DATE element in Excel. PROC IMPORT OUT=lookup DATAFILE= "macroeco_data. xls" DBMS=xls REPLACE; SHEET="Sheet1"; GETNAMES=YES; MIXED=NO; RUN;

Code to join macroeconmic data Using PROC FORMAT to generate a table look-up. Key values are ASOFDATEs. Labels are macroeconomic variables at the specific ASOFDATE. %macro fmt(var); data fmt; set lookup (rename=(&VAR.=LABEL)) end=eof; start = intnx('month', ASOFDATE,0); /* or start = intnx('month', ASOFDATE,0,'b') */ retain fmtname '&VAR.x' type 'N'; output; if eof then do; HLO='O'; START=.; label=.; end; run; proc format cntlin=fmt fmtlib; run; %mend;

Code to read in macroeconmic data Reading the FORMAT TABLE: %fmt(CPI); %fmt(HHINC) data model; set model; CPI = input(put(ASOFDATE,CPIX.),best16.); HHINC = input(put(ASOFDATE,HHINCX.),best16.); run;

------------------------------------------------------------------------------------------------------------------------------- | | | May 2017 | |-----------------------------------------------------------------------------------------------------------------------------| | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | |-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------| | | 1 | 2 | 3 | 4 | 5 | 6 | | | | | | | | | | | CIGAR | | CIGAR | | | | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | CIGAR | CIGAR | | | | | | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | Mothers Day | | | | | | CIGAR | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | | | | | | | CIGAR ===~ | | 28 | 29 | 30 | 31 | | | | This slide is a set of questions the group can keep in mind as you’re moving through the material. Near the end of the presentation, this slide is repeated with answers. Some individuals may want to jot down the answers as they are covered. Note, these questions are different from the “pop-quiz” questions that are interspersed in the deck. Those questions are designed to break up the presentation and get quick audience feedback, and are generally very high level. 35

References Bilenas, J. (2007) Using SAS® Dates and Times – A Tutorial, SAS Global Forum. Morgan, D. (2006) The Essential Guide to SAS Dates and Times, SAS Press. Kothamasu, S. and Bilenas, J. (2010). Passing date ranges to Oracle® made simple using SAS® picture formats. NESUG. This slide is a set of questions the group can keep in mind as you’re moving through the material. Near the end of the presentation, this slide is repeated with answers. Some individuals may want to jot down the answers as they are covered. Note, these questions are different from the “pop-quiz” questions that are interspersed in the deck. Those questions are designed to break up the presentation and get quick audience feedback, and are generally very high level. 36

Questions? Comments? Concerns? This slide is a set of questions the group can keep in mind as you’re moving through the material. Near the end of the presentation, this slide is repeated with answers. Some individuals may want to jot down the answers as they are covered. Note, these questions are different from the “pop-quiz” questions that are interspersed in the deck. Those questions are designed to break up the presentation and get quick audience feedback, and are generally very high level. 37