The Essential Guide to SAS® Dates and...

2429

Transcript of The Essential Guide to SAS® Dates and...

Page 1: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 2: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 3: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 4: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheEssentialGuidetoSAS®DatesandTimesSecond

Page 5: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Edition

DerekP.Morgan

Page 6: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

support.sas.com/bookstore

Page 7: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thecorrectbibliographiccitationforthismanualisasfollows:Morgan,DerekP.2014.TheEssentialGuidetoSAS®DatesandTimes,SecondEdition.Cary,NC:SASInstituteInc.

TheEssentialGuidetoSAS®DatesandTimes,SecondEdition

Copyright©2014,SASInstituteInc.,Cary,NC,USA

ISBN978-1-62959-066-0

Page 8: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(Hardcopy)ISBN978-1-62959-489-7(EPUB)ISBN978-1-62959-490-3(MOBI)ISBN978-1-62959-491-0(PDF)

Allrightsreserved.ProducedintheUnitedStatesofAmerica.

Forahard-copybook:Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inanyformorbyanymeans,electronic,mechanical,

Page 9: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

photocopying,orotherwise,withoutthepriorwrittenpermissionofthepublisher,SASInstituteInc.

Forawebdownloadore-book:Youruseofthispublicationshallbegovernedbythetermsestablishedbythevendoratthetimeyouacquirethispublication.

Thescanning,uploading,anddistributionofthisbookviatheInternetoranyothermeanswithoutthepermissionofthe

Page 10: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

publisherisillegalandpunishablebylaw.Pleasepurchaseonlyauthorizedelectroniceditionsanddonotparticipateinorencourageelectronicpiracyofcopyrightedmaterials.Yoursupportofothers’rightsisappreciated.

U.S.GovernmentLicenseRights;RestrictedRights:TheSoftwareanditsdocumentationiscommercialcomputersoftwaredevelopedatprivateexpenseandisprovidedwithRESTRICTEDRIGHTStothe

Page 11: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

UnitedStatesGovernment.Use,duplicationordisclosureoftheSoftwarebytheUnitedStatesGovernmentissubjecttothelicensetermsofthisAgreementpursuantto,asapplicable,FAR12.212,DFAR227.7202-1(a),DFAR227.7202-3(a)andDFAR227.7202-4and,totheextentrequiredunderU.S.federallaw,theminimumrestrictedrightsassetoutinFAR52.227-19(DEC2007).IfFAR52.227-19isapplicable,thisprovisionservesasnoticeunderclause(c)thereofandnoothernotice

Page 12: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isrequiredtobeaffixedtotheSoftwareordocumentation.TheGovernment'srightsinSoftwareanddocumentationshallbeonlythosesetforthinthisAgreement.

SASInstituteInc.,SASCampusDrive,Cary,NorthCarolina27513-2414.

December2014

SASprovidesacompleteselectionofbooksandelectronicproductstohelp

Page 13: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

customersuseSAS®softwaretoitsfullestpotential.Formoreinformationaboutourofferings,visitsas.com/store/booksorcall1-800-727-0025.

SAS®andallotherSASInstituteInc.productorservicenamesareregisteredtrademarksortrademarksofSASInstituteInc.intheUSAandothercountries.®indicatesUSAregistration.

Otherbrandandproductnames

Page 14: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aretrademarksoftheirrespectivecompanies.

Page 15: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Contents

AboutThisBookAcknowledgmentsChapter1:Introductionto

Page 16: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatesandTimesinSAS1.1HowDoesItWork?(January1,1960,andMidnightasZero)1.2InternalRepresentation1.3ExternalRepresentation(BasicFORMATConcepts)1.4DateandTimeas

Page 17: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NumericConstantsinSAS1.5LengthandNumericRequirementsforDate,Time,andDatetime1.6GeneralSASOptionsforDates

Chapter2:DisplayingSASDate,Time,andDatetimeValuesas

Page 18: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatesandTimesasWeKnowThem2.1HowDoIUseaFormat?2.2HowManyBuilt-InFormatsAreThereforDatesandTimes?2.3DateFormats,Justification,andODS2.4DetailedDiscussionofEachFormat

Page 19: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.4.1DateFormats2.4.2TimeFormats2.4.3DatetimeFormats

2.5CreatingCustomDateFormatsUsingtheVALUEStatementofPROCFORMAT2.6CreatingCustomDateFormatsUsingthePICTUREStatementofPROCFORMAT

Page 20: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.7CreatingCustomFormatsUsingPROCFCMPforProcessing2.8ThePUT()FunctionandFormats

Chapter3:ConvertingDatesandTimesintoSASDate,Time,andDatetimeValues

Page 21: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.1AvoidingtheTwo-DigitYearTrap3.2UsingInformats3.3TheINFORMATStatement3.3.1UsingInformatswiththeINPUTStatement3.3.2InformatswiththeINPUT()Function3.3.3WhentheInformatDoesNot

Page 22: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MatchtheDataBeingRead

3.4ListingandDiscussionofInformats3.4.1DateInformats3.4.2TimeInformats3.4.3DatetimeInformats3.4.4The"ANYDATE"SeriesofInformats3.4.5SoWhyNotJustUsethe"ANYDATE"

Page 23: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SeriesofInformats?

Chapter4:ISO8601Dates,Times,Datetimes,Durations,andFunctions4.1WhatIsISO8601?4.2ISO8601Formats4.2.1ISODateFormats

Page 24: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.2.2ISOTimeFormats4.2.3ISODatetimeFormats

4.3ISO8601Informats4.3.1ISODateInformats4.3.2ISOTimeInformats4.3.3ISODatetimeInformats

4.4TimeZoneFunctions

Page 25: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.4.1Introduction4.4.2TheTIMEZONE=Option4.4.3ListofTimeZoneFunctions

4.5ISO8601DurationsandIntervals4.5.1ISODurationandIntervalRepresentations4.5.2ISO8601DurationandInterval

Page 26: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Formats4.5.3ISO8601DurationandIntervalInformats4.5.4CALLIS8601_CONVERT

4.6Conclusion

Chapter5:DateandTimeFunctions

Page 27: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.1CurrentDateandTimeFunctions5.2ExtractingPiecesfromSASDate,Time,andDatetimeValues5.3CreatingDates,Times,andDatetimesfromNumbersorOtherInformation5.3.1Introduction5.3.2ListofFunctionsandTheirDescriptions

Page 28: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.4CalculatingElapsedTime,andtheHOLIDAY()Function5.4.1CalculatingElapsedTimewithDATDIF()andYRDIF()

5.5TheBasicsofSASIntervals5.5.1TheIntervalCalculationFunctions:INTCK()andINTNX()

5.6ModifyingSAS

Page 29: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Intervals5.7CreatingYourOwnSASIntervals5.8IntervalFunctionsaboutIntervals5.8.1INTFIT(argument-1,argument-2,type)5.8.2INTFMT('interval','size')5.8.3INTGET(argument1,argument2,argument3)

Page 30: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.8.4INTSHIFT('interval')5.8.5INTTEST('interval')

5.9RetailCalendarIntervalsandSeasonality5.9.1RetailCalendarIntervals5.9.2SeasonalityFunctions

Chapter6Deeper

Page 31: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intoDatesandTimeswithSAS6.1MacroVariablesandDates6.1.1AutomaticMacroVariables6.1.2PuttingDatesintoTitles6.1.3Using%SYSFUNC()toCreateDates,Times,and

Page 32: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatetimesinMacroVariables6.1.4UsingDatesinMacros

6.2GraphingDatesJohnny'sSavingsAccount

6.3TheBasicsofPROCEXPAND6.3.1CapabilitiesofPROCEXPAND6.3.2UsingPROC

Page 33: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

EXPANDtoConverttoaHigherFrequency6.3.3UsingPROCEXPANDtoConverttoaLowerFrequency6.3.4UsingPROCEXPANDtoInterpolateMissingValues6.3.5TheOBSERVED=OptionfortheCONVERTStatementinPROC

Page 34: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

EXPAND6.4InternationalDate,Time,andDatetimeFormatsandInformats6.4.1"EUR"FormatsandInformats6.4.2NLSFormats

6.5NLSDate,Time,andDatetimeConversionFunctions6.6DateFormatsandInformatsforOther

Page 35: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Calendars6.6.1HebrewDateFormats6.6.2JapaneseandTaiwaneseDateFormats6.6.3JapaneseandTaiwaneseDateInformats

6.7OtherSoftwareandTheirDates(Excel,Oracle,DB2)

Page 36: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.7.1TheSASDATEFMT=SystemOption

6.8Conclusion

AppendixA:AQuickReferenceGuidetoSASDate,Time,andDatetimeFormatsAppendixB:A

Page 37: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

QuickReferenceGuidetoNLSDate,Time,andDatetimeFormatsAppendixC:TroubleshootingDates101Index

Page 38: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AboutThisBook

PurposeThisbookisdesignedtoprovideadetailedlookathowtheSASdatefacilityworks,includinganin-depthlookatintervalsand

Page 39: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theintervalfunctions,ISO8601dateanddatetimehandling,andtheNLSformatsandinformats.Itisintendedtoserveasbothareferenceandateachingtool.Ultimately,thisbookwillallowthereadertobecomemoreconfidentintheirdailyworkwithdates,times,and

Page 40: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimesinSAS.

IsThisBookforYou?ThisbookisaimedatbeginningtointermediateSASprogrammers,orthosewhoworkwithISO8601data,intervals,and/orreportingin

Page 41: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

multiplelanguages.

What’sNewinThisEditionThisneweditionincludesupdatedinformationtoreflectthechangesinversion9ofSAS;anexpandeddiscussionofintervals,includingtheabilityto

Page 42: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defineyourownintervals;asectiononhowSASworkswiththeISO8601datestandards;andatroubleshootingappendixforbeginners.

ScopeofThisBookThisbookdoesnotcovertheSAS/ETSproduct,exceptforan

Page 43: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

overviewoftheEXPANDprocedure.

AbouttheExamplesSoftwareUsedtoDeveloptheBook'sContentSASversion9.4(TSlevel1M0)wasusedto

Page 44: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

producealltheexamplesinthisbook.

ExampleCodeandDataManyoftheexamplesusedinthisbookhaveaccompanyingcodeanddata.

Youcanaccesstheexamplecodeanddata

Page 45: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

forthisbookbylinkingtoitsauthorpageathttp://support.sas.com/publishing/authorsSelectthenameoftheauthor.Then,lookforthecoverthumbnailofthisbook,andselectExampleCodeandDatatodisplaytheSASprogramsthatareincludedinthisbook.

Foranalphabetical

Page 46: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

listingofallbooksforwhichexamplecodeanddataisavailable,seehttp://support.sas.com/bookcodeSelectatitletodisplaythebook’sexamplecode.

Ifyouareunabletoaccessthecodethroughthewebsite,sende-mailto

Page 47: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

[email protected].

OutputandGraphicsUsedinThisBookTablesinthisbookweregeneratedusingODSRTF,whilegraphicsweregeneratedasPNGfilesdirectlyinSASusingtheGPLOTand

Page 48: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SGPLOTprocedures.ScreencaptureswereusedtoshowtheVIEWTABLEdisplays.

AdditionalHelpAlthoughthisbookillustratesmanyanalysesregularlyperformedinbusinessesacrossindustries,questions

Page 49: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specifictoyouraimsandissuesmayarise.Tofullysupportyou,SASInstituteandSASPressofferyouthefollowinghelpresources:

•Forquestionsabouttopicscoveredinthisbook,contacttheauthorthrough

Page 50: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASPress:◦Sendquestionsbyemailtosaspress@sas.comincludethebooktitleinyourcorrespondence.

◦Submitfeedbackontheauthor’spageathttp://support.sas.com/author_feedback

Page 51: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

•Forquestionsabouttopicsinorbeyondthescopeofthisbook,postqueriestotherelevantSASSupportCommunitiesathttps://communities.sas.com/welcome•SASInstitutemaintainsacomprehensivewebsitewithup-to-

Page 52: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateinformation.OnepagethatisparticularlyusefultoboththenoviceandtheseasonedSASuserisitsKnowledgeBase.Searchforrelevantnotesinthe“SamplesandSASNotes”sectionoftheKnowledgeBaseat

Page 53: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

http://support.sas.com/resources•RegisteredSASusersortheirorganizationscanaccessSASCustomerSupportathttp://support.sas.comHereyoucanposespecificquestionstoSASCustomerSupport;underSupport,click

Page 54: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SubmitaProblem.Youwillneedtoprovideanemailaddresstowhichrepliescanbesent,identifyyourorganization,andprovideacustomersitenumberorlicenseinformation.ThisinformationcanbefoundinyourSAS

Page 55: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

logs.

MeettheAuthor

DerekMorganisaseniorSASprogrammer

Page 56: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inthepharmaceuticalindustrywhohasbeenprogrammingprofessionallyinSASforover27years.Hespent23ofthoseyearsatWashingtonUniversityinSt.Louis,wherehereceivedanA.B.inbiologyin1985andhisfirstintroductiontoSASasastudent.Duringhis

Page 57: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

careerhehasusedSAStocreateinteractivedataentryandmanagementsystemsandtobuildandmaintainresearchdatabasesforanalysis.Inthelate1980s,hecreatedamacrolibrarytoallowtheuseofnonproportionalfontsintablesandlistingsonPostScriptprinters.He

Page 58: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hastaughtintroductorySASprogrammingandhaspresentedmanypapersatlocal,regional,andnationalSASUsersGroupconferences.Derekismarriedandhasoneson,andinhissparetimeheplayselectricbassaroundtheSt.Louisarea.

Page 59: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Learnmoreaboutthisauthorbyvisitinghisauthorpageathttp://support.sas.com/publishing/authors/morgan.htmlThereyoucandownloadfreebookexcerpts,accessexamplecodeanddata,readthelatestreviews,getupdates,andmore.

KeepinTouch

Page 60: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Welookforwardtohearingfromyou.Weinvitequestions,comments,andconcerns.Ifyouwanttocontactusaboutaspecificbook,pleaseincludethebooktitleinyourcorrespondence.

Contactthe

Page 61: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AuthorthroughSASPress•Bye-mail:[email protected]

•ViatheWeb:http://support.sas.com/author_feedback

PurchaseSASBooksForacompletelistofbooksavailable

Page 62: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

throughSAS,visitsas.com/store/books.

•Phone:1-800-727-0025

•E-mail:[email protected]

SubscribetotheSASTrainingandBookReportReceiveup-to-date

Page 63: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informationaboutSAStraining,certification,andpublicationsviaemailbysubscribingtotheSASTraining&BookReportmonthlyeNewsletter.Readthearchivesandsubscribetodayathttp://support.sas.com/community/newsletters/training

Publishwith

Page 64: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASSASisrecruitingauthors!Areyouinterestedinwritingabook?Visithttp://support.sas.com/saspressformoreinformation.

Page 65: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Acknowledgments

Abookisalwaysateameffort,andIcanhonestlysaythatIhavehadagreatone.StartingwiththepeopleatSASPresswhoseeffortsandpatienceonmybehalfhaveresultedinthis

Page 66: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

secondedition,IwouldliketothankJuliePlatt,ShelleySessoms,andStaceyHamiltonbutmostespeciallymydevelopmentaleditor,StephenieJoyner,whokeptmeontaskandmovingforward.SASguruArtCarpenterhasbeenmorethanjustaneditor;hehasbeenamentor,andeverytime

Page 67: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Irunintohim,IfindmyselfknowinghowtodosomethingbetterthanIdidbefore.Thissecondeditionwouldnotexistwithouthissupport.AnotherbigthankyougoestoDeniseT.Jones,StacySuggs,andRobertHarris,allfromSASPress,whoareresponsiblefor

Page 68: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

producingthisbookfrommymanuscript.

ThankstoAndrewKarpforintroducingmetotheworldofPROCEXPAND,andtoMikeFornoofSASInstituteforansweringmyquestionsonit.IgreatlyappreciatetheAmericanPublicTransportation

Page 69: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Association(http://www.apta.com),whichallowedmetousedatacompiledfromitsmembertransitagenciesforthePROCEXPANDexamples.ThetechnicalsupportgroupatSASInstitutedeservesspecialmention;after27yearsofworkingasacomputerprogrammer,

Page 70: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Ihavecometotheconclusionthatyouare,withoutadoubt,thebestinthebusiness.ThankyouforallofyourhelpthroughoutmySAScareer.

I’dalsoliketothankthetechnicalreviewersfromSASInstitute—RichardBell,ChrisDeHart,Johnny

Page 71: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Johnson,RickLangston,andKimWilson—fortheirthoughtfulcommentsandcorrections.Inaddition,PaulRowlanddeservesabigthankyouforhiswillingnesstoprovideaSASuser’sperspective.

Ultimately,thisprojectwouldnothavebeen

Page 72: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

possiblewithoutthesupportofmyfamily,pastandpresent.Tomyson,Terec,thankyouforunderstandingmydistractionduringFormulaIseason,andthankyouforthephoto.Tomywife,Billie,thankyouforlettingmeworkonourweekendsandagoodnumberofour

Page 73: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

evenings,andforcontributingyourknowledgeofMicrosoftWordtohelpmewritethemanuscript.

Page 74: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter1:IntroductiontoDatesandTimesinSAS

1.1HowDoesItWork?(January1,1960,and

Page 75: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MidnightasZero)1.2InternalRepresentation1.3ExternalRepresentation(BasicFORMATConcepts)1.4DateandTimeasNumeric

Page 76: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ConstantsinSAS1.5LengthandNumericRequirementsforDate,Time,andDatetime1.6GeneralSASOptionsforDates

IntheyearsthatI've

Page 77: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beenworkingwithSASandteachingstudentshowtouseit,IfindthattwothingsconsistentlyconfusethosewhoarenewtoSAS.FirstisthedefaultwaythattheDATAstepworks.ItsimpliedDO-until-end-of-datageneratesmany"HowdoItellithowmuchdatatoreadandwhen

Page 78: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tostop?"questions.ThesecondmostconfusingconceptinSASisthatofhowdates(andtimes)workwithinthesoftware.I'veseenmanymisusesofcharacterstringsmasqueradingasdatesand/ortimesovertheyears,aswellasunexpectedresultsduetoafailureto

Page 79: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

understandthisfundamentalpartofSAS.

However,thewaythatSASreads,stores,anddisplaysdatesandtimesisonlythetipoftheicebergwhenitcomestothepowerandflexibilityofSASinhandlingthisinformation.Thereisso

Page 80: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

muchmorethanjusthavingnumbersrepresentdateandtimevalues.We'llstartwiththebasicsinthefirstthreechapters,andthenprogresstosomemoreadvancedusesofthosedateandtimevalues,takingadvantageofmanyofthefeaturesavailableinSASforthatpurpose.

Page 81: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.1HowDoesItWork?(January1,1960,andMidnightasZero)SAScountsdates,times,anddatetimevaluesseparately.ThedatecounterstartedatzeroonJanuary1,1960.AnydaybeforeJanuary1,1960,isa

Page 82: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

negativenumber,andanydayafterthatisapositivenumber.Everydayatmidnight,thedatecounterisincreasedbyone.Thetimecounterismeasuredinsecondsandrunsfromzero(atmidnight)to86,400(thenumberofsecondsinaday),whenitresetstozero.SAS

Page 83: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

calculatesdatetimesasthenumberofsecondssincemidnight,January1,1960.WhyJanuary1,1960?ThefoundersofSASwantedtousetheapproximatebirthdateoftheIBM370systemtorepresentthebeginningofthemoderncomputingera,andtheychoseJanuary1,1960,asaneasy-to-

Page 84: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

rememberapproximation.

Indecidingwhethertouseadate,atime,oradatetime,youshouldconsiderhowyouaregoingtouseit.Datetimesarealwaysdateandtimecombined;therefore,ifyouwillnotalwayshaveatimeavailable

Page 85: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

foreachdate,youshouldstronglyconsiderusingseparatedateandtimevariablesandthencalculateadatetimevariablefromthetwocomponentswhenneeded.Normally,attemptingtocreateadatetimewithoutbothadateandatimewillcauseanerror,andtheresult

Page 86: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willbeamissingvalueforthedatetime.However,inspecificcircumstances,itispossibletocreateadatetimevaluefromadateandamissingtime(seeSection4.3.3,"ISODatetimeInformats,"foranexample).Inthesecases,thetimewillbesetautomaticallyto

Page 87: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

midnight(0secondsofthegivendate).Youmaywantyourdatetimevaluetobemissingwhenthereisadatebutnotimeavailable.Inthesespecificcircumstances,itisimportanttokeeptrackofdateandtimeseparately.Manyprogramsthathandledates(suchas

Page 88: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

databasesandspreadsheets)maintaintheirdatesandtimesasanumericvaluerelativetosomefixedpointintime,althoughthedatethatrepresentszeroisdifferentacrosseachpackage,andpackagesmayvaryinhowtheykeeptrackoftimeofday.Ultimately,thismakescalculating

Page 89: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

durationseasy,andworkingwithdatesandtimesstoredinthisfashionbecomesamatterofaddition,subtraction,multiplication,anddivision.

1.2InternalRepresentation

Page 90: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASstoresdatesasintegers,whilethedatetimeandtimecountersarestoredasrealnumberstoaccountforfractionalseconds.TheoriginofthealgorithmusedforSASdateprocessingcomesfromaJanuary14,1980,ComputerworldarticlebyDr.BhairavJoshiof

Page 91: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SUNY-Geneseo.TheearliestdatethatSAScanhandlewiththisalgorithmisJanuary1,1582(essentiallytheimplementationdateofthecurrentGregoriancalendarsystem).Thelatestdateisfarenoughintothefuturethatatleastfivedigitswillberequiredtodisplaytheyear.

Page 92: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatesasstoredbySASdon'tdousmuchgoodintherealworld.Thestatement"Iwasbornon-242"won'tmeanmuchtoanyoneelse.However,"IwasbornonMay4,1959,"caneasilybetranslatedintosomethingthatmostpeoplecanunderstand,oritcanbeusedasis.Fortunately,

Page 93: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAShasanumberofbuilt-infacilitiestoperformautomatictranslationbetweentheinternalnumbersstoredinSASanddatesandtimesandtheirrepresentationasunderstoodbytherestoftheworld.Thesebuilt-intoolsincludeformatsandinformats(introducedinSection

Page 94: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.3andcoveredextensivelyinChapters2,3,and4),dateandtimeconstants(Section1.4),andfunctions(Chapter5).

1.3ExternalRepresentation(BasicFORMAT

Page 95: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Concepts)Formatsperformanautomatictranslationbetweentheactualvalueandthevaluetobedisplayed.Formatsdisplaythedate,time,anddatetimevaluesinafashionthatismuchmoreeasilyunderstood.Formatsdonotchangethevalues

Page 96: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themselves;theyarejustawaytodisplaythevaluesinanyoutput.

WhenyouhavedatesortimesandwanttotranslatethemintoSASdateandtimevalues,youwilluseinformats.Althoughyouwillneedastatement,procedure,orafunctionto

Page 97: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

actuallycreatetheSASvalues,informatsdescribewhatthedatalooklikesothatSAScantranslateitcorrectlyforstorage.WewilldiscussformatsandinformatsindetailinChapters2,3,and4becausetherearedozensofthem.Threeofthemostcommonlyusedformatsthatwork

Page 98: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withSASdate,time,anddatetimevaluesareusedinthefollowingsection.

1.4DateandTimeasNumericConstantsinSASWe'vetalkedaboutinternalandexternal

Page 99: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representationofdatesandtimes.Howdoyouputaspecificdateintoaprogramasaconstant?Formatsonlychangethewaythevaluesaredisplayedinoutput,soyoucan'tusethem.InformatsneedafunctionoraSASstatementtotranslatethecharacterstheyaregiven,soyoucoulduse

Page 100: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

them,butthenyouwouldalwaysneedtousetheINPUT()functiontocreateaSASdateinaDATAsteporPROCSQL.TheINPUTfunctiontakesaseriesofcharactersthatyougiveitandtranslatesitusingtheinformatthatdescribeswhattheseriesofcharacterslooklike.

Page 101: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

That'sveryinefficientifyoujustwantonespecificdate.

date=INPUT("04AUG2013",DATE9.);

LookattheprograminExample1.1toseehowdate,time,anddatetimeconstantsarewrittenintoaSASprogram.Takenoteof

Page 102: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thequotationmarksaroundthevaluesfordate,time,anddatetimeandthelettersthatfolloweachclosingquote.

Example1.1:DateConstants

DATAdate_constants;

date='04aug2013'd;/*Thisisadateconstant*/

Page 103: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time='07:15:00't;/*Thisisatimeconstant*/datetime='07aug1904:21:31:00'dt;/*Thisisadatetimeconstant*/RUN;

TITLE"UnformattedConstants";PROCPRINTDATA=date_constants;VARdatetimedatetime;RUN;

Page 104: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TITLE"FormattedConstants";PROCPRINTDATA=date_constants;VARdatetimedatetime;FORMATdateworddate32.timetimeampm9.datetimedatetime19.;/*Formattheconstants*/RUN;

Thequotesareusedtocreatealiteralvalue.Youmayuseapairofsingleordoublequotes

Page 105: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tospecifytheliteralvalue.Dateshavetobewrittenasddmonyyyy;timesashh:mm:ss(addadecimalpointandmoredigitstorepresentfractionalsecondsifnecessary);anddatetimesasthedateddmonyyyy,followedbyaseparator(frequentlyseenasacolon[:])andthenthe

Page 106: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time(hh:mm:ss).Asidefromthecorrectformattingoftheseliteralvalues,themostimportantpartofadate/time/datetimeconstantistheletterthatimmediatelyfollowsthelastquote.Theletter"D"standsfordate,"T"fortime,and"DT"fordatetime.Upperorlowercaseis

Page 107: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valid.Ifyouputoneofthesestringsinquoteswithouttheletterattheend,youwillcreateacharactervariable,notanumericvariablewithadate,time,ordatetimevalue.Thedifferencemightnotbecomeapparentuntilyoutrytodosomethingwiththevariableyoucreated

Page 108: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatinvolvesacalculation.Don'tforgetyour"D,""T,"or"DT"!ThisexampledemonstrateshowtheseconstantsaredefinedandthenautomaticallyconvertedtotheirequivalentSASvalues.

ThefirstPROCPRINTstatementdisplaysthe

Page 109: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date,time,anddatetimevalueswecreatedwithourconstantswithoutformats,sowecanseethevaluesastheyarestoredinthedataset.

UnformattedConstants

date time datetime

19574 26100 -1748226540

Page 110: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThesecondPROCPRINTshowstheeffectofassociatingthevariableDATEwiththeWORDDATE.format,thevariableTIMEwiththeTIMEAMPM.format,andthevariableDATETIMEwiththeDATETIME.format.

Page 111: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormattedConstants

date time datetime

August4,2013

7:15AM

07AUG1904:21:31:00

Withouttheformats,youcanseethatthedateconstantsweusedtocreatethevaluesstoredinthedataset

Page 112: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aredisplayedastheiractualSASdate,time,anddatetimevalues.Theydon'tmakemuchsensetousuntilaformatisassociatedwiththevariable.

Whathappensifyouforgettoputthe"D,""T,"or"DT"afteryourdateconstant?InExample1.2,the"D,"

Page 113: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

"T,"and"DT"havebeenremovedfromthesamedate,time,anddatetimeinExample1.1.

Example1.2:IncorrectDateConstants

DATAbad_date_constants;

date='04aug2013';/*ThisisNOTadateconstant*/time='07:15:00';/*

Page 114: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThisisNOTatimeconstant*/datetime='07aug1904:21:31:00';/*ThisisNOTadatetimeconstant*/RUN;

TITLE"UnformattedConstants";PROCPRINTDATA=bad_date_constants;VARdatetimedatetime;RUN;

Page 115: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Nowweprintoutthevalueswithoutformats.Whiletheproblemmaynotbeapparentatfirstglance,thisresultdoesnotlookliketheunformattedSASdate,time,anddatetimevaluesinthepreviousexample.

Unformattedconstants

Page 116: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date time

04aug2013 07:15:00

Nowlet'strytoaddonedaytothedate,andaminute(60seconds)toboththetimeanddatetime.HereisapartiallogofwhathappenswhenwetrythiswiththecodeinExample1.2.

Page 117: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12DATAbad_date_constants;13date='04aug2013'+1;14time='07:15:00'+60;15datetime='07aug1904:21:31:00'+60;16RUN;

NOTE:Charactervalueshavebeenconvertedtonumericvaluesattheplacesgivenby:(Line):(Column).

Page 118: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

13:814:815:12NOTE:Invalidnumericdata,'04aug2013',atline13column8.NOTE:Invalidnumericdata,'07:15:00',atline14column8.NOTE:Invalidnumericdata,'07aug1904:21:31:00',atline15column12.

❶date=.time=.datetime=._ERROR_=1_N_=1

Page 119: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

The"invalidnumericdata"noteinthelogtellsyouthatyoutriedtouseacharactervaluetodosomethingthatrequiresanumericvalue.Theboldfacelastlinetellsyouthatyouhavemissingvaluesforallthreevariables,becauseyouweretryingtodomathwithacharactervalue.

Page 120: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RememberthatSASdates,times,anddatetimesarealwaysstoredasnumbers.Whenyousee"invalidnumericdata"whereyouintendedtouseadateconstant,itishighlyprobablethatyourdateconstantismissingitsidentifying"D,""T,"or"DT."

Page 121: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.5LengthandNumericRequirementsforDate,Time,andDatetimeYoucantakeadvantageofthefactthatdatesarestoredasintegerstosavespacewhenyoucreatevariablestostorethem.Insteadofusing

Page 122: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedefaultlengthof8fornumericvariables,setthelengthofthenumericvariableswhereyouarestoringdatesto4.ThiswillsafelystoredatesfromJanuary1,1582(theearliestdateSAScanhandle),toOctober23,7701.Alengthof5isoverkill,althoughthatwouldextendthe

Page 123: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

endingdateanother534,773,760days!Alengthof3willnotaccuratelystoredatesoutsidetherangeofJanuary1,1960,andSeptember13,1960.Ifyoudeclareyourdatevariablestobealengthof4,youwillbeabletostoretwodatesinthespaceitwouldtaketostoreoneifyouwere

Page 124: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usingtheSASdefaultlengthfornumericvariables.Thiscansaveyouagreatdealofstoragespaceinalargedatawarehouse.

Timesmaypresentabitofaproblem,becauseyoumayneedtostorefractionalseconds.Theruleissimpleenough:Ifyou

Page 125: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttostoretimevalueswithfractionalseconds,youmustusealengthof8tostorethemaccurately.Otherwise,thelengthof4islongenoughtostoreeverypossibletimevaluefrommidnighttomidnightdowntothesecond.Inthesecases,notusingthedefaultlengthwill

Page 126: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

allowyoutostoretwotimesinthesameamountofspaceasone.

Datetimevaluesrequiremorespace,becausealengthof4willnotstoreadatetimevaluewithaccuracy,regardlessofwhetheryouwantfractionalseconds.Thenumberis

Page 127: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

justtoobig.Aslongasyouarenotstoringfractionalseconds,alengthof6willstoredatetimesthataccuratelyrepresentvaluesfrommidnightonJanuary1,1582,to3:04:31p.m.onApril9,6315.Changingtherangefromthedefaultof8to6fordatetimevaluesresultsina25

Page 128: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

percentsavingsinspace,whichstillmaybesignificantdependingonhowmuchdatayouhave.Ofcourse,ifyouaregoingtomaintaindecimalplacesinyourdatetimevalues,youmustusethedefaultlengthof8.

Ihavejustprovidedthe

Page 129: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

absoluteminimumlengthsrequiredforaccuracy.DONOTattempttosaveadditionalspacebyshrinkingthevariablelengthsbeyond4,6,or8aslisted.Youwillloseprecision,whichcouldleadtounexpectedresults.Example1.3showswhatcanhappenifyou

Page 130: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

donotuseenoughbytestostoreyourdatevalues.Thisexampleusesthevalue19941,whichrepresentsthedateofAugust6,2014,anditisinvariablesoflengths3,4,and5.

Example1.3:TheEffectofLENGTHStatementsonDates

DATAdate_length;

Page 131: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LENGTHlen33len44len55;len3=19941;len4=19941;len5=19941;FORMATlen3len4len5mmddyy10.;RUN;

Asthetablebelowshows,whenyoutrytostoreadateinfewerthan4bytes,youdonotgetthecorrect

Page 132: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

value.Usingalengthof4tostoreyourdatesandtimes(withoutfractionalseconds)isstillasignificant(50percent)savingsintheamountofstoragerequired.Youwillcreateinaccuraciesinyourdataifyoutrytosavemorethanthat.Savingadditionalspaceisnotworththeriskof

Page 133: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inaccuratedata.

len3 len4

08/05/2014 08/06/2014

1.6GeneralSASOptionsforDatesTwooptionsinfluencethedefaultdateand

Page 134: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timestampthatSASplacesonpagesofoutputandtheSASlog.TheDATE/NODATEoptioncausesthestartdateandtimeoftheSASjob(orsession)toappearoneachpageoftheSASlogandSASoutput.Thesevaluesareobtainedfromtheoperatingsystemclockandaredisplayedas

Page 135: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

24-hourclocktime,followedbythedayoftheweek,month,day,andfour-digityear.IfyouarerunningSASinteractively,thenthedateandtimeareprintedonlyontheoutput,notthelog.Bydefault,theDATEsystemoptionisineffectwhenyoustartSAS.However,ifyou

Page 136: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

donotwantthisdefaultdisplay,thenusetheNODATEoption.Youprobablydon'twantSAStodisplayitsdefaultdatestampifyouaregoingtoputyourowndateand/ortimestampinthetitleorinafootnote(seeChapter6).

Page 137: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Asmentionedinthepreviousparagraph,iftheDATEoptionisenabled,SASprintsthedateandtimethatthecurrentSASsessionstartedoneachpage.Ifyouwantamoreexactdateandtimeonthosepages,youcanusetheDTRESETsystemoption,whichwillcauseSAStoretrieve

Page 138: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedateandtimefromtheoperatingsystemclockeachtimeapageiswritten.ThatdateandtimewillthenbeplacedonthepageinsteadofthetimethattheSASjobstarted.Sincethetimeisdisplayedinhoursandminutes,youwillonlyseeitchangeeveryminute.TheDTRESET

Page 139: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

optioncanbeusefulininteractiveapplicationsorSASprogramsthatmayhavebeenrunningfordaysorweeks,whereknowingwhentheoutputwasgeneratedismoreimportantthanknowingwhentheSASsessionbegan.SincetheDTRESEToptionaffectsthedefaultSAS

Page 140: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateandtimestamp,itworksonlyiftheDATEoptionisenabled.WhenyouusetheNODATEoption,usingDTRESETwillhavenoeffectbecauseyouaren'tusingtheSASdateandtimestamponyouroutput.

Page 141: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter2:DisplayingSASDate,Time,andDatetimeValuesasDatesand

Page 142: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TimesasWeKnowThem

2.1HowDoIUseaFormat?2.2HowManyBuilt-InFormatsAreThereforDates

Page 143: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andTimes?2.3DateFormats,Justification,andODS2.4DetailedDiscussionofEachFormat2.5CreatingCustomDate

Page 144: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatsUsingtheVALUEStatementofPROCFORMAT2.6CreatingCustomDateFormatsUsingthePICTUREStatementofPROCFORMAT

Page 145: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.7CreatingCustomFormatsUsingPROCFCMPforProcessing2.8ThePUT()FunctionandFormats

InSAS,date,time,anddatetimevaluesare

Page 146: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

storedasintegers(unlessyouarestoringfractionalpartsofseconds).Theyareallcountedfromafixedreferencepoint.SASdatevaluesincrementby1atmidnightofeachday,whileSASdatetimevaluesincrementby1everysecond.SAStimevaluesstartatzeroat

Page 147: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

midnightofeachday,andalsoincrementby1eachsecond.

Thisschememakesiteasytocalculatedurationsindaysandseconds,butitdoesnotdomuchforfiguringoutwhatagivenSASdate,time,ordatetimevaluemeansintermsofhowwetalkabout

Page 148: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

them.Therefore,SASprovidesafacilitythatmakesiteasytoperformthetranslationfromSASintothecommonterminologyofmonths,days,years,hours,andseconds.Thetranslationisdonethroughformats.

FormatsarewhatSASusestocontroltheway

Page 149: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datavaluesaredisplayed.Theycanalsobeusedtogroupdatavaluestogetherforanalysis.TheyareessentialtodatesandtimesinSASbecauseSASdoesnotstoredatesandtimesinaneasilyrecognizableform,aswediscussedinChapter1.SAShasmanybuilt-informats

Page 150: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

todisplaydates,times,anddatetimevalues.Thischapterprovidesadetailedguidetoallofthedate,time,anddatetimeformatsreadilyavailableinSAS.Inaddition,ifanyofthesebuilt-informatsdon'tfityourneeds,youhavetheabilitytocreate(andstoreforfutureuse)

Page 151: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yourownformats.CreatingyourownformatsiscoveredinSections2.5and2.6.

Ifyouarelookingforaquickreference,youcangotoAppendixA,whichlistsallofthedate,time,anddatetimeformatsandprovidesasampledisplayusingtheir

Page 152: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultlengths.Ifthedefaultdoesnotgiveyouwhatyouwant,Section2.4discusseseachdate,time,anddatetimeformatindetail,includinghowtospecifythelengthoftheformatandhowthatlengthaffectsthedisplay.

Page 153: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.1HowDoIUseaFormat?Formatsareeasytouse.YoucanpermanentlyassociateaformatwithavariablebyusingaFORMATstatementinaDATAstep,asshowninExample2.1.

Example2.1:Permanently

Page 154: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AssociatingaFormatwithaVariable

DATAtest;LENGTHdate1time14;date1=19781;time1=73000;FORMATdate1MMDDYY10.time1TIMEAMPM11.;RUN;

Example2.1createsadatasetcalledTEST,whichhastwovariables:date1and

Page 155: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time1.ByusingtheFORMATstatementhere,youhavespecifiedthatwheneverthevaluesfromthisdatasetaredisplayed,thevaluesstoredinthevariabledate1willalwaysbedisplayedwiththeformatMMDDYY10.,andthosestoredintime1willalwaysbe

Page 156: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayedusingtheTIMEAMPM11.format.

date1 time1

02/27/20148:16:40PM

Ifyoudon'twanttohaveyourdatavaluespermanentlyassociatedwithaformat,thenyou

Page 157: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canjustapplytheformatwhenyouareactuallywritingthevaluestoyouroutput.ThesameFORMATstatementisused,butthelocationhaschanged,fromtheDATAsteptothePROCstep.Example2.2illustratesthis.

Example2.2:Associatinga

Page 158: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatwithaVariablefortheDurationofaProcedure

DATAtest2;LENGTHdate2time24;date2=19781;time2=73000;RUN;

PROCPRINTDATA=test2;FORMATdate2DATE9.time2TIMEAMPM11.;RUN;

Page 159: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date2 time2

02/27/20148:16:40PM

Althoughthereisnoformatassignedtoeitherdate2ortime2intheDATAstep,youhavetoldthePRINTproceduretowritethesevaluesusingthe

Page 160: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

twoformatslisted,sothereisnodifferencebetweentheoutputfromExample2.1andthatfromExample2.2.AnotherhandythingaboutusingtheFORMATstatementwithaSASprocedureisthatifyouusetheFORMATstatementinaSASprocedure,itwilloverrideanyformat

Page 161: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thathasbeenpermanentlyassociatedwiththevariablesforthedurationofthatprocedure.Toillustrate,we'lltakethedatasetTESTfromExample2.1.Thevariablesdate1andtime1havebeenassociatedwiththeformatsMMDDYY10.andTIMEAMPM11.,

Page 162: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

respectively.Whatifyourreportneedsthedateprintedoutwiththedayoftheweek,alongwiththenameofthemonth,day,andyear,whilethetimeneedstobesecondsaftermidnight?ThePROCPRINTstepwilllooklikethis:

PROCPRINTDATA=test;

Page 163: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FORMATdate1WEEKDATE37.time1;RUN;

date1 time1

Thursday,February27,2014 73000

AllSASprocedureswillusetheformatsspecifiedinthe

Page 164: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FORMATstatementthatispartofthePROCstepinsteadoftheformatsassociatedwiththevariableinthedataset.Therefore,intheaboveexample,date1isprintedusingtheWEEKDATE.format.Whatabouttime1?There'snoformatnamegivenafterthevariablenameintheFORMAT

Page 165: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statement.ThisishowtotellSASnottouseanyformatsthatmightbeassociatedwiththevariable.ToremoveaFORMATfromavariable,makesurethatnoformatnamesofanykindfollowitanywhereintheFORMATstatement.

Example2.3:Removing

Page 166: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AssociatedFormatsinaProcedure

DATAtest3;date3=19067;time3=18479;date4=18833;time4=45187;FORMATdate3date4DATE9.time3time4TIME5.;RUN;

PROCPRINTDATA=test3NOOBS;FORMATdate3date4time3time4;RUN;

Page 167: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Datasettest3withallformatsremoved

date3 time3 date4

19067 18479 18833

Whathappensifthereareformatnamesaftertheonethatyouwanttoremove?Lookatthecodesegmentthatfollowsthisparagraph.

Page 168: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thegoalistodisplaythevariabledate3withtheMMDDYY10.format,removetheformatfromthevariabledate4,andapplytheTIMEAMPM11.formattothetimevariablestime3andtime4.SotheFORMATstatementhasMMDDYY10.fordate3,nothingfor

Page 169: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date4,andTIMEAMPM11.fortime3andtime4,right?

PROCPRINTDATA=test3NOOBS;FORMATdate3MMDDYY10.date4time3time4TIMEAMPM11.;RUN;

date3 time3

5:07:59

Page 170: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3/15/2012 AM

Whathappened?Thevariabledate4isdisplayedasatime,whenyoudidn'tputaformatnameafterthevariablenameintheFORMATstatement.TheansweristhatyougavealistofthreevariablestoSASand

Page 171: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toldittoapplytheTIMEAMPM11.formattothem(seeboldfacecodebelow):

FORMATdate3MMDDYY10.date4time3time4TIMEAMPM11.;

Howdoyoufixthis?Youhavetomakesurethatdate4isthelastvariablelistedinthe

Page 172: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FORMATstatement.

PROCPRINTDATA=test3NOOBS;FORMATdate3MMDDYY10.time3time4TIMEAMPM11.date4;RUN;

date3 time3

3/15/20125:07:59AM

Page 173: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.2HowManyBuilt-InFormatsAreThereforDatesandTimes?SAShasmorethan70ready-to-useformatstodisplaydates,times,anddatetimes.WewilldiscusseachoneindetailinSection2.4,butifyou'relookingfor

Page 174: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aquickreferenceguide,seeAppendixA.SAScontinuestodevelopformatsandinformats,soitisalwaysagoodideatocheckthedocumentationthatcamewithyourreleaseofSAS,ortheonlinedocumentationatsupport.sas.com.AllSASformatshavea

Page 175: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

commonsyntaxstructure,beginningwiththeformatname,followedbyanoptionalwidthspecification,andendingwithaperiod.Theperiodiscritical.ItiswhatallowsSAStorecognizethewordasaformatandnotsomeotherSASkeywordortext.Thewidthspecification

Page 176: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

varieswitheachformat,andallformatshaveadefaultwidththatisusedifthereisnowidthspecificationgiven.ThewidthspecificationisveryimportanttodatesbecauseSASwillabbreviatethedisplayedvalueifyoudonotspecifyenoughcharactersforthe

Page 177: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

width,andtheabbreviationthatSASusesmightnotgiveyoutheoutputthatyouwant.ThedefaultwidthisnotedinthedescriptionforeachformatinSection2.4,anditisusuallythewidththatwillaccommodatethelongestvaluetobedisplayed.Forexample,

Page 178: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedefaultwidthfortheDOWNAME.(day-of-week)formatis9.Thatwillaccommodatethestring“Wednesday”,whichisthelongestEnglishday-of-weekname.

2.3DateFormats,Justification,and

Page 179: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODSEachdateformathasadefaultjustificationwithrespecttothewidthspecificationthatyougiveit.Sincenumericvaluesareright-justifiedinSAS,mostoftheformatsthatareappliedtodate,time,ordatetimeformatsarealsoright-

Page 180: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

justified,withafewexceptions(whichwillbeclearlynotedinthedetailedexplanationsthatfollow).InODSdestinationsotherthanLISTING,valuesarejustifiedwithinatablecolumnbySASproceduredefaultorbyauser-definedODStemplate.Bydefault,SASmakesitscolumns

Page 181: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wideenoughtofitthewidestiteminagivencolumn.Therefore,anyleadingspacescausedbyspecifyingawidththatistoowidetofittheformattedvaluewon'tshowupinODSoutput.

However,priortoODSorversion9.3intheLISTINGdestination,

Page 182: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usingawidthspecificationthatiswiderthantheoutputrequirescausesSAStofilltheemptyspaceswithblanks.Forvaluesthatareright-justified,thismightcausesomeoftheoutputtoshifttotherightbyanumberofspaces.InExample2.4,weusetheMONNAME.format,

Page 183: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichdisplaysthetextcorrespondingtothemonthofthedate,toillustrate.

Example2.4:HowJustificationWorksintheLISTINGDestination

FormatName Result

MONNAME9. September

MONNAME10. September

Page 184: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MONNAME11. September

MONNAME15. September

Asyoucansee,making

Page 185: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thewidthspecificationlargeronlyaddsleadingspaces,andyoucouldextendthisallthewaytothemaximumwidthfortheformat.

WhyshouldIworryaboutjustification?I'mnotusing

Page 186: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODSLISTING,andI'musingSAS9.3orhigher.

WhileitistruethatjustificationismoreofaconcerninthetraditionalLISTINGdestinationandonlyapplicable

Page 187: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

totraditionalcolumn-basedoutput,leadingspacescanshowupifyouusethePUT()(orPUTN())functiontocreatecharacterstringsfromSASdate,time,ordatetimevalues.Incasessuchasthese,the

Page 188: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingspacesarepartoftheoutputandassuchmightbedisplayed.YoucanuseSTRIP()orCOMPRESS()toremovetheleadingspacesexplicitly.Ifyouaregoingtoconcatenatemultipleitems,

Page 189: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usetheCATX(),CATS(),orCATT()functions,allofwhichremoveleadingandtrailingspacesofeachitembeingconcatenated.

Ifyoudonotspecifycolumnalignmentinan

Page 190: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODStemplateorbyusingSTYLE=directives,certainODSdestinations(suchasRTFandPDF)willjustifyvalueswithinacolumnaccordingtothejustificationoftheformatusedinthecolumn,withoutleadingspaces.

Page 191: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.4DetailedDiscussionofEachFormatThissectiongivesadetailedexplanationofallthecurrentstandardformatsavailableforSASdate,time,anddatetimevalues.Inadditiontothedisplaythatresultsfromusing

Page 192: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

agivenformat,theexplanationincludesinformationaboutthedefaultwidthspecificationanditspossiblevalues,annotatedexamplesofthedisplaywithvaryingwidthspecifications,andusagenotes.Dateformatswillbecoveredfirst,thentimeand

Page 193: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimeformats.Eachsubsectionisarrangedalphabetically.

2.4.1DateFormatsAdateformatprovidesasetofinstructionsforhowaSASdateisdisplayedsothatitlookslikeadateinthewaywenormally

Page 194: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

expressthem.Youcanspecifythewidth(numberofcharacters)thatthetranslatedtextwilloccupy,buteachformathasitsowndefaultwidthspecification,shownaswinthistext.Thedefaultwidthspecificationisgiveninthedescriptionofeachformat.Some,butnot

Page 195: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

all,ofthedateformatsallowyoutospecifythecharacterthatseparateseachelementofthedate.Youmustnotuseadateformattotranslatedatetimevalues.Ifyoutrytotranslateadatetimevaluewithadateformat,youwillgetincorrectoutput.(Foranexample,see

Page 196: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example2.5.)

DATEw.DATEw.writesdatesasthenumericaldayofthemonthfollowedbythethree-lettermonthabbreviationandtheyear,withoutanyseparatingcharacters.Itisright-justifiedwithinthefield.wcan

Page 197: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

befrom5to11,andthedefaultwidthis7.Ifyouwanttodisplayfour-digityears,useDATE9.orDATE11.DATE11.willdisplayfour-digityearswithahyphenbetweentheday,monthabbreviation,andyear.Thefollowingtableshowstheresultwhenthedatevalueis

Page 198: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

19715,whichcorrespondstoDecember23,2013.

FormatName Result

DATE. 23DEC13

DATE5. 23DEC

Page 199: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATE7. 23DEC13

DATE9. 23DEC2013

DATE11. 23-DEC-

Page 200: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2013

ThisformatisanalogoustotheDTDATE.format,whichdisplaysdatetimevaluesinthesamemanner.

DAYw.

Page 201: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DAYw.writesthenumericaldayofthemonth,anditisright-justifiedwithinthefield.wcanbefrom2to32,andthedefaultwidthis2.Specifyinganythinglongerthan2willonlyplacemorespacesinthefieldtotheleftofthenumber,soitisnotnecessarytospecifymorethan2.

Page 202: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thefollowingtableshowstheresultwhenthedatevalueis16739,whichcorrespondstoOctober30,2005.

FormatName Result Comment

DAY2. 30

Page 203: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYYw.

DDMMYYw.writesdatesasday/numericalmonth/year,wheretheslash(/)istheseparator,anditisright-justifiedwithinthefield.wcanbefrom2to10,andthedefaultwidthis8.Ifyouspecifyawidthfrom2to5,thedatewillbe

Page 204: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

truncatedontheright,asSAStriestofitasmuchofthedayandmonthaspossibleinthespaceallowed.Ifyouuse6,noslasheswillbeprinted.Awidthof8willuseatwo-digityearaftertheslashes.Use10togetafour-digityearwithslashes.Thefollowingtableshowstheresult

Page 205: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whenthedatevalueis19869,whichcorrespondstoMay26,2014.

FormatName Result

DDMMYY5. 26/05

DDMMYY6. 260514

DDMMYY8. 26/05/14

Page 206: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYY10. 26/05/2014

DDMMYYxw.

DDMMYYxw.issimilartotheDDMMYY.format.Itisalsoright-justified.However,withthisformat,youcanspecifywhatcharacterseparatestheday,numericalmonth,

Page 207: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andyear.Thexintheformatnamerepresentstheseparatorbetweentheday,month,andyear.Thefollowingtablelistswhatxcanbe.

x

CharacterDisplayedinOutput Comment

Page 208: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B blank

C colon(:)

D dash(-)

N noseparator

wisamaximumof8,not10.

P period(.)

Page 209: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

S slash(/) EffectivelythesameasusingtheDDMMYY.format.

wcanbefrom2to10,withthedefaultbeing

Page 210: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8.ThisworksthesamewayastheDDMMYY.formatwithrespecttowhatSASfitsinthespacespecified.Again,ifyouspecifyawidthfrom2to5,thedatewillbetruncatedontheright,asSAStriestofitasmuchofthedayandmonthaspossibleinthespaceallowed.Ifyouuse6,

Page 211: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

noseparatorwillbeused.At8,SASwillprintatwo-digityear.Use10togetafour-digityearwithyourseparator.Thefollowingtableshowstheresultwhenthedatevalueis19398,whichcorrespondstoFebruary9,2013.

Page 212: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

DDMMYYP5. 09.02

DDMMYYB6. 090213

DDMMYYD8. 09-02-13

Page 213: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYYD8. 09-02-13

DDMMYYS8. 09/02/13

Page 214: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYYC10. 09:02:2013

DOWNAMEw.

DOWNAMEw.writesthedateasthenameof

Page 215: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedayoftheweek.Itisright-justified,soifyougiveittoomuchspace,therewillbeleadingblanks.wcanbefrom1to32,andthedefaultis9.Ifyoudon'tspecifyw,SASwillalwaysprinttheentirenameoftheday.However,ifyouspecifywlessthan9,thenSASwilltruncatethename

Page 216: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthedaytofitasnecessary.Thefollowingtableshowstheresultwhenthedatevalueis20280,whichcorrespondstoSaturday,July11,2015.

FormatName Result

Page 217: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DOWNAME3. Sat

DOWNAME6. Saturd

DOWNAME8. Saturday

JULDAYw.JULDAYw.writesthedateastheJuliandayoftheyear,whichisavaluefrom1to366.Itisright-justified.wcan

Page 218: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

befrom3to32,andthedefaultis3.NotethattheminimumwidthoftheformatwillcauseleadingspacesiftheJuliandateislessthan100.ThiswouldbecomeobviousifyouarecreatingacharacterstringusingthePUT()orPUTN()functionsanddonotremove

Page 219: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingblanks.

FormatName Result Comment

JULDAY3. 9 Thereare2leadingspacesherebecausetherearefewerthan

Page 220: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3digitsinthevaluedisplayed.Thedatevalueusedhereis374,whichcorrespondstoJanuary

Page 221: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9,1961.

JULDAY3. 76 Thereisaleadingspaceherebecausetherearefewerthan3digitsinthevalue

Page 222: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayed.Thedatevalueusedhereis19068,whichcorrespondstoMarch16,2012.

Page 223: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JULDAY3. 107 Thereisnoleadingspaceherebecausethereare3digitsinthevaluedisplayed.Thedate

Page 224: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueusedhereis19465,whichcorrespondstoApril17,2013.

JULIANw.

Page 225: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JULIANw.writesyourdatevalueasaJuliandate,withtheyearprecedingtheJulianday.Itisright-justified.wcanbefrom5to7,andthedefaultis5.Ifyouspecifyawidthof5,theyearportionoftheJuliandateistwodigitslong.Ifyouspecifyawidthof7,theyearportionisfour

Page 226: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digitslong.Thefollowingtableshowstheresultwhenthedatevalueis18514,whichcorrespondstoSeptember9,2010.

FormatName Result Comment

JULIAN5. 100252

JULIAN7. 2010252

Page 227: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYYw.

MMDDYYw.writesthedateasnumericalmonth/day/year,whereaslash(/)istheseparator.Itisright-justifiedwithinthefield.wcanbefrom2to10,andthedefaultis8.Itissimilartothe

Page 228: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYY.formatinthatifyouspecify2–5forthewidth,thedatewillbetruncatedontheright,asSAStriestofitasmuchofthedayandmonthaspossibleinthespaceallowed.Ifyouuse6,noslasheswillbeprinted,butitwillprintatwo-digityear.Awidthof8willputa

Page 229: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

two-digityearaftertheslashes.Useawidthof10togetafour-digityearwithslashes.Thefollowingtableshowstheresultwhenthedatevalueis19655,whichcorrespondstoOctober24,2013.

FormatName Result

Page 230: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY2. 10

MMDDYY4. 1024

MMDDYY5. 10/24

Page 231: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY6. 102413

MMDDYY8. 10/24/13

Page 232: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY10. 10/24/2013

MMDDYYxw.

MMDDYYxw.displaysthedateinthesamewaythattheMMDDYY.formatdoes,exceptthatyoucanspecifytheseparator.Thexintheformatnamespecifiestheseparatorthatyou

Page 233: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttouseaccordingtothefollowingtable.

x

CharacterDisplayedinOutput Comment

B blank

C colon(:)

D dash(-)

Page 234: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

N noseparator

wisamaximumof8,not10.

P period(.)

S slash(/) Effectivelythesameasusingthe

Page 235: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY.format.

Thedatewillberight-justifiedwithinthewidththatyouspecify.wcanbefrom2to10,andthedefaultis8.Ifyouspecify2–5,thedatewillbetruncatedontheright,asSAS

Page 236: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

triestofitasmuchofthedayandmonthaspossibleinthespaceallowed.Ifyouuseawidthof6,noseparatorwillbeused.At7,SASwillprintatwo-digityearwithoutaseparator,andwidthsof8or9willputatwo-digityearaftertheseparator.Use10togetafour-digityearwith

Page 237: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

separators.Thefollowingtableshowstheresultwhenthedatevalueis19188,whichcorrespondstoJuly,14,2012.

FormatName Result

MMDDYYD5. 07-14

MMDDYYS6. 071412

Page 238: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYYC8. 07:14:12

MMDDYYP10. 07.14.2012

Page 239: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYYB10. 07142012

MMYYw.

MMYYw.displaysthezero-filledmonthnumberandyearforthegivendatevalue,separatedbytheletterM.Itisright-justified,andwcanbefrom5to32,withadefault

Page 240: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthof7.Whenwislessthan7,atwo-digityearisused.Otherwise,afullfour-digityearisdisplayed.Sincethisformatonlyneedsamaximumof7characters,anywidthgreaterthan7willjustaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis19756,

Page 241: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichcorrespondstoFebruary2,2014.

FormatName Result Comment

MMYY5. 02M14

MMYY7. 02M2014 Four-digityear.

Page 242: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMYYxw.MMYYxw.displaysthemonthnumberandyearforagivendatevalueinthesamefashionthattheMMYY.formatdoes,exceptthatyoucanspecifytheseparatorwithx,accordingtothetablebelow.Notethattheblankisnotvalidwith

Page 243: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thisformat,whileitisvalidwiththeDDMMYYx.andMMDDYYx.formats.

x

CharacterDisplayedinOutput Comment

C colon(:)

D dash(-)

Page 244: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

N noseparator

wcanbefrom4to32,withadefaultof6.

P period(.)

S slash(/)

Thedisplayeddate

Page 245: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuewillbeisright-justified,andwcanbefrom5to32,withadefaultwidthof7.Whenwisspecifiedas5or6,atwo-digityearisused,unlessyouhavespecified"N,"fornoseparator.Withoutaseparator,thereisenoughspacetodisplaythetwodigitsofthemonth,andfour

Page 246: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digitsfortheyear.Ifwis7ormore,afullfour-digityearisalwaysdisplayed.Sincethisformatwillonlydisplayamaximumof7characters,anyvaluegreaterthan7willjustaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis19628,whichcorrespondsto

Page 247: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

September27,2013.

FormatName Result Comment

MMYYD5. 09-13 Two-digityear.

MMYYN6. 092013 Noseparator,four-digit

Page 248: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

year.

MMYYS6. 09/13 Two-digityearbecauseofseparatorandleadingspace.

Page 249: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMYYC7. 09:2013 Four-digityear.

MMYYP7. 09.2013 Four-digityear.

MONNAMEw.

MONNAMEw.displaysthenameofthemonth.

Page 250: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Itisright-justified,andwcanbefrom1to32,withadefaultof9.Usingavaluegreaterthan9willonlyaddleadingspaces.SASwilltruncatethemonthnameasnecessarytofitinthewidth.Thefollowingtableshowstheresultwhenthedatevalueis18336,whichcorrespondsto

Page 251: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

September15,2010.

FormatName Result

MONNAME3. Sep

Page 252: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MONNAME4. Sept

MONNAME9. September

MONTHw.

MONTHw.displaysthenumberofthemonthoftheyear.Itisright-justified,andwcanbefrom1to21,withadefaultof2.Usingaw

Page 253: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

of1willdisplaythemonthnumberasahexadecimalvalue(1throughC).Thefollowingtableshowstheresultwhenthedatevalueis19341,whichcorrespondstoDecember14,2012.

FormatName Result Comment

Page 254: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MONTH1. C walwaysprintsasinglecharacter,whichisahexadecimaldigit.

MONTH2. 12

Page 255: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MONYYw.

MONYYw.displaysthethree-lettermonthabbreviation,followedbytheyearwithoutanyseparatingcharacters.Itisright-justified,andwcanbefrom5to7,withadefaultof5.Specifyingawidthof5willgiveyouatwo-digityear.A

Page 256: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthof6givesyouatwo-digityearandoneleadingspaceinthedisplayeddate,while7givesyouafour-digityear.ItisanalogoustotheDTMONYY.format,whichisusedwithdatetimevalues.Thefollowingtableshowstheresultwhenthedatevalueis19718,whichcorrespondsto

Page 257: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

December26,2013.

FormatName Result Comment

MONYY5. DEC13

MONYY7. DEC2013

PDJULGw.

PDJULGw.writesa

Page 258: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

packedJuliandateinhexadecimalformatforIBMcomputers.Justificationisnotanissue,andwcanrangefrom3to16.Thedefaultwidthis4.TheJuliandateiswrittenasfollows:Thefour-digitGregorianyeariswritteninthefirsttwobytes,andthethree-digitintegerthat

Page 259: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representsthedayoftheyearisinthenextone-and-a-halfbytes.Thelasthalf-bytecontainsallbinary1s,whichindicatesthevalueispositive.

IftheSASdatevaluebeingtranslatedbythisformatisadateconstantwithatwo-digityear,itwillbe

Page 260: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

affectedbytheYEARCUTOFFoption.SeethefollowingSASlog.

246OPTIONSYEARCUTOFF=1880;247DATA_NULL_;248date1="15JUN2004"d;249date2="15JUN04"d;/*AffectedbyYEARCUTOFFoption*/250juldate1=put(date1,PDJULG4.);

Page 261: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

251juldate2=put(date2,PDJULG4.);252PUTjuldate1=$HEX8.;253PUTjuldate2=$HEX8.;254RUN;

juldate1=2004167Fjuldate2=1904167F

PDJULIw.PDJULIw.writesapackedJuliandateinhexadecimalformatfor

Page 262: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IBMcomputers.ItonlydiffersfromthePDJULG.formatinthatitwritesthecenturyinthefirstbyteasatwo-digitinteger,followedbytwodigitsoftheyearinthesecondbyte.Thenextone-and-a-half-bytesstorethethree-digitintegerthatcorrespondstothedayoftheyear,whilethe

Page 263: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

lasthalf-byteisfilledwithhexadecimal1sthatindicateapositivenumber.AswiththePDJULG.format,justificationisnotanissue,andthedefaultwidthis4,withawidthrangeof3to16.

Thecenturyandyeararecalculatedbysubtracting1900from

Page 264: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thefour-digitGregorianyear.Ayearvalueof1980givesacentury/yearvalueof0080(1980-1900=80),while2015gives0115(2015-1900=115).Beawarethatthisformatwillnotproducecorrectresultsforyearspreceding1900.Theexamplebelowdemonstrates.

Page 265: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OPTIONSYEARCUTOFF=2000;DATA_NULL_;date1="15JUN1804"d;date2="15JUN1996"d;date3="15JUN96"d;juldate1=PUT(date1,pdjuli4.);juldate2=PUT(date2,pdjuli4.);juldate3=PUT(date3,pdjuli4.);PUTjuldate1=$hex8.;PUTjuldate2=$hex8.;PUTjuldate3=$hex8.;should_be_date1=INPUT(juldate1,pdjuli4.);

Page 266: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUTshould_be_date1=mmddyy10.;PUTshould_be_date1=;RUN;

Hereistheresultingoutput:

juldate1=009609DFjuldate2=0096167Fjuldate3=0196167Fshould_be_date1=04/12/1996should_be_date1=13251

Thevalueofdate1

Page 267: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correspondstoadatein1804,causingthePDJULI.representationofdate1(juldate1)tobeincorrect.Whileitshouldbethesamedayoftheyear(167)asdate2anddate3,youcanseethatthedayisincorrectlywrittenas09D,whilethecenturyvalueisalsoincorrect,markedas00when,by

Page 268: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thealgorithm,itshouldbeexpressedasanegativenumber.ThisisverifiedbyusingthePDJULI.informattoreadjuldate1,whichgivesaresultofApril12,1996,whenitshouldbeJune15,1804.(Thisisbecausethereisnosignbitforjulday1toindicatethatthevalueshouldbe

Page 269: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

negative.)Thedifferencebetweendate2anddate3iscausedbytheYEARCUTOFFoption.Thetwo-digityear96istranslatedas2096,not1996,becauseoftheoption'svalue.

QTRw.QTRw.writesadate

Page 270: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueasthequarteroftheyear.Itisright-justified,andwcanrangefrom1to32,withadefaultof1.Sincethisformatwillonlywrite1character,specifyingawidthgreaterthan1willjustaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis18264,

Page 271: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichcorrespondstoJanuary2,2010.

FormatName Result Comment

QTR1. 1

QTRRw.

QTRRw.alsowritesadatevalueasthe

Page 272: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

quarteroftheyear,exceptthatitdisplaysthequarterasaRomannumeral.Itisright-justified,andwcanrangefrom3to32,withadefaultof3.Thisformatwillwriteamaximumof3characters.Awidthspecificationgreaterthan3willaddleadingspaces.

Page 273: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result Comment

QTRR3. III Thedatevalueusedis17791(September16,2008).

QTRR3. IV Withadatevalue

Page 274: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

of17882(December16,2008),thereis1leadingspace.

QTRR5. III Withadatevalueof17791

Page 275: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(September16,2008),thereare2leadingspaces.

WEEKDATEw.

WEEKDATEw.writesdatevaluesasday-of-

Page 276: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekname,monthname,day,andyear.Itisright-justified,andwcanrangefrom3to37.Thedefaultis29,whichisthemaximumwidthofadateinthisformat.Specifyinganythinglongerthan29willcauseleadingspacestobeadded.Ifthewidthspecifiedistoosmalltodisplaythe

Page 277: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

completedayoftheyearandmonth,SASwillabbreviate.Thefollowingtableshowstheresultwhenthedatevalueis18164,whichcorrespondstoSeptember24,2009.

FormatName Result

WEEKDATE3. Wed

Page 278: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDATE9. Wednesday

Page 279: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDATE17. Wed,Sep24,2009

WEEKDATE23. Wednesday,Sep24,2009

Page 280: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDATE29. Wednesday,September24,2009

WEEKDATXw.WEEKDATXw.writesdatevaluesasday-of-weekname,day,monthname,andyear.

Page 281: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ItdiffersfromtheWEEKDATE.formatinthatthedayofthemonthprecedesthemonthname.Itisright-justified,andwcanrangefrom3to37.Thedefaultis29,whichisthemaximumwidthofadateinthisformat.Specifyinganythinglongerthan29willcauseleadingspacesto

Page 282: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beadded.Ifthewidthspecifiedistoosmalltodisplaythecompletedayoftheyearandmonth,SASwillabbreviate.Thefollowingtableshowstheresultwhenthedatevalueis19260,whichcorrespondstoSeptember24,2012.

Page 283: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

WEEKDATX3. Wed

WEEKDATX9. Wednesday

Page 284: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDATX17. Wed,24Sep,2012

WEEKDATX23. Wednesday,24Sep,2012

Page 285: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012

WEEKDATX29. Wednesday,24September,2012

WEEKDAYw.

Page 286: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDAYw.writesthedatevalueasthenumberofthedayoftheweek,where1=Sunday,2=Monday,andsoon.Itisright-justified,andwcanbefrom1to32.Thedefaultis1.Sincethemaximumwidthofthedisplayisalwaysonecharacter,specifyinganything

Page 287: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

longerwilljustcauseleadingspacestobeadded.Thefollowingtableshowstheresultwhenthedatevalueis20569,whichcorrespondstoMonday,April25,2016.

FormatName Result Comment

Page 288: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDAY1. 2

WEEKUw.WEEKUw.writesthedatevalueasaweeknumberindecimalformatusingtheUalgorithm.Unlikemanyotherdate,time,anddatetimeformats,itisleft-justified.wcanbefrom1to200,andthe

Page 289: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultis11.Specifyinganyvaluegreaterthan11willdisplaythesameresultsasifwwere11.TheUalgorithmcalculatesweeksbasedonSundaybeingthefirstdayoftheweek,andtheweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroif

Page 290: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary.Thedisplaythatthisformatpresentsvaries,basedonthewidthspecification.Thefollowingtableshowstheresultwhenthedatevalueis20885,whichcorrespondstoMarch7,2017,whichisaTuesdayinthetenthweekoftheyear.

Page 291: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

WEEKU3. W10

Page 292: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU4. W10

WEEKU5. 17W10

Page 293: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU6. 17W10

WEEKU7. 17W1003

Page 294: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU8. 17W1003

Page 295: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU9. 2017W1003

Page 296: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU10. 2017W1003

WEEKU11. 2017-W10-03

Page 297: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU12. 2017-W10-03

WEEKVw.

Page 298: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKVw.writesthedatevalueasaweeknumberindecimalformatusingtheValgorithm,whichisInternationalStandardsOrganization(ISO)compliant.Itisleft-justifiedinthesamefashionastheWEEKU.format.wcanbefrom1to200,andthedefaultis11.

Page 299: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Specifyinganyvaluegreaterthan11willdisplaythesameresultsasifwwere11.TheValgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweek,andtheweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroifnecessary.

Page 300: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThisalgorithmdefinesthefirstweekoftheyearascontainingbothJanuary4andthefirstThursdayoftheyear.Therefore,ifthefirstMondayoftheyearfallsonJanuary2,3,or4,theprecedingdaysofthecalendaryearareconsideredtobeapartofweek53ofthepreviouscalendaryear.

Page 301: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thefollowingtableshowstheresultwhenthedatevalueis19723,whichcorrespondstoDecember31,2013.Notethatalthoughthedateisin2013,thealgorithmusedbythisformatplacesthedateintheyear2014.Monday,December30,2013,isconsideredto

Page 302: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

bethefirstdayofthefirstweekfortheyear2014.

FormatName Result

WEEKV3. W01

Page 303: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKV4. W01

Page 304: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKV5. 14W01

WEEKV6. 14W01

WEEKV7. 14W0102

Page 305: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 306: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKV8. 14W0102

WEEKV9. 2014W0102

Page 307: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKV10. 2014W0102

WEEKV11. 2014-W01-02

Page 308: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKV12. 2014-W01-02

Page 309: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKWw.

WEEKWw.writesthedatevalueasaweeknumberindecimalformatusingtheWalgorithm.AswiththeWEEKU.andWEEKV.formats,itisleft-justified.wcanbefrom1to200,andthedefaultis11.Specifyinganyvalue

Page 310: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

greaterthan11willdisplaythesameresultsasifwwere11.TheWalgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweekwithoutanyotherrestriction.Theweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroif

Page 311: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary.Thedisplaythatthisformatpresentsvariesbasedonthewidthspecification.Thefollowingtableshowstheresultwhenthedatevalueis19723,whichcorrespondstoDecember31,2013(thesamedateusedintheValgorithmexample).Notethatthe

Page 312: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Walgorithmassignsthedateastheseconddayofthelastweekofthecalendaryear2013.

FormatName Result

WEEKW3. W52

Page 313: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW4. W52

Page 314: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW5. 13W52

WEEKW6. 13W52

Page 315: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW7. 13W5202

Page 316: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW8. 13W5202

WEEKW9. 2013W5202

Page 317: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW10. 2013W5202

WEEKW11. 2013-W52-02

Page 318: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKW12. 2013-W52-

Page 319: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

02

WORDDATEw.

WORDDATEw.displaysthedatevalueasnameofmonth,day,andyear.Itisright-justified,andwcanrangefrom3to32.Thedefaultis18.Ifthewidthspecifiedisless

Page 320: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

than18,SASwillabbreviatethemonthnameandaddleadingspacesasnecessary,regardlessofwhetherthespecificdatetobedisplayedwillfitintheallocatedspacebecauseofitsvalue.ThismighthaveanimpactifyouaregoingtousethePUT()orPUTN()functionstocreatea

Page 321: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

characterstringusingthisformat.Thefollowingtableshowstheresultwhenthedatevalueis20328,whichcorrespondstoAugust28,2015.

FormatName Result

WORDDATE3. Aug

WORDDATE12. Aug

Page 322: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

28,2015

WORDDATE15. Aug28,2015

WORDDATE18. August28,2015

Page 323: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WORDDATE20. August28,2015

Page 324: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WORDDATXw.

WORDDATXw.displaysthedatevalueasday,nameofmonth,andyear.ItdiffersfromtheWORDDATE.formatinthatthedayprecedesthenameofmonth.Itisright-justified,andwcanrangefrom3to32.Thedefaultis18.Ifthewidthspecifiedisless

Page 325: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

than18,SASwillabbreviatethemonthnameandaddleadingspacesasnecessary,evenifthedatetobedisplayedwillfitinthewidthspecified.ThismighthaveanimpactifyouaregoingtousethePUT()orPUTN()functionstocreateacharacterstringusingthisformat.Inthe

Page 326: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

followingtable,youseethatMarchisabbreviatedforwidthspecificationslessthan18,eventhoughthereisroomtoprinttheentiredatestring.Thetableshowstheresultwhenthedatevalueis21259,whichcorrespondstoMarch16,2018.

Page 327: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

WORDDATX3. Mar

WORDDATX12. 16Mar2018

WORDDATX14. 16Mar2018

Page 328: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 329: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WORDDATX16. 16Mar2018

Page 330: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WORDDATX18. 16March2018

Page 331: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YEARw.

YEARw.displaystheyearforthegivendatevalue.Itisright-justified,andwcanbefrom2to4,withadefaultwidthof4.

Page 332: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whenwisspecifiedas2or3,atwo-digityearisused.Thefollowingtableshowstheresultwhenthedatevalueis18599,whichcorrespondstoDecember3,2010.

FormatName Result Comment

Page 333: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YEAR2. 10 Two-digityear.

YEAR3. 10 Two-digityearwithaleadingspace.

YEAR4. 2010 Four-digityear.

Page 334: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMw.

YYMMw.displaystheyearandmonthnumberforthegivendatevalue,separatedbytheletterM.Itisright-justified,andwcanbefrom5to32,withadefaultwidthof7.Whenwisspecified

Page 335: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

as5or6,atwo-digityearisused.Ifwis7ormore,afullfour-digityearisdisplayed.Sincethisformatcanonlydisplayamaximumof7characters,anythingmorethan7willjustaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis19517,whichcorrespondsto

Page 336: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

June8,2013.

FormatName Result Comment

YYMM5. 13M06 Two-digityear.

YYMM6. 13M06 Leadingspace.

YYMM7. 2013M06 Four-

Page 337: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digityear.

YYMM8. 2013M06 Leadingspace.

YYMMxw.YYMMxw.displaystheyearandmonthnumberforagivendatevalueinthesame

Page 338: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mannerastheYYMM.formatabove,exceptthatyoucanspecifytheseparatorwithxaccordingtothetablebelow.UnliketheDDMMYYx.,MMDDYYx.,andtheYYMMDDx.formats,ablankisnotavalidseparatorwiththisformat.

Page 339: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

x

CharacterDisplayedinOutput Comment

C colon(:)

D dash(-)

N noseparator

wcanbefrom4to32,withadefault

Page 340: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

adefault

of6.

P period(.)

S slash(/)

YYMMxw.isright-justified,andwcanbefrom5to32,withadefaultwidthof7.Whenwisspecifiedas5or6,atwo-digityear

Page 341: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isused.Ifwis7ormore,afullfour-digityearisdisplayed.Specifyingnoseparatorwith"N"willchangetherangeofwfrom4to32,andthedefaultwidthbecomes6.Sincethisformatcanonlydisplayamaximumof7characters,anythingmorethan7willjustaddleadingspaces.The

Page 342: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

followingtableshowstheresultwhenthedatevalueis19237,whichcorrespondstoSeptember1,2012.

FormatName Result Comment

YYMMN4. 1209 Noseparator;minimum

Page 343: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthis4;two-digityear.

YYMMC5. 12:09

YYMMD6. 12-09 Oneleadingspace;two-digit

Page 344: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

year.

YYMMP7. 2012.09 Four-digityear.

YYMMS8. 2012/09 Four-digityear;1leadingspace.

YYMMDDw.

Page 345: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDw.isavariationontheDDMMYY.andMMDDYY.formats.Itwritesthedateasayear,followedbythenumericalmonth,andthentheday,withadash(-)astheseparator.Itisright-justifiedwithinthefield.wcanbefrom2to10,andthedefaultis

Page 346: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8.ItissimilartotheMMDDYY.formatinthatifyouspecifythewidthfrom2to5,thedatewillbetruncatedontheright,asSAStriestofitasmuchoftheyearandmonthaspossibleinthespaceallowed.Ifyouuse6,nodasheswillbeprinted.At7,SASwillprintatwo-digityear

Page 347: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withoutadash,and8or9willproduceatwo-digityearbeforethefirstdash.Useawidthof10togetafour-digityearwithdashes.Thefollowingtableshowstheresultwhenthedatevalueis20031,whichcorrespondstoNovember4,2014.

Page 348: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

YYMMDD4. 1411

Page 349: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDD5. 14-11

Page 350: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDD6. 141104

YYMMDD7. 141104

Page 351: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDD8. 14-11-04

YYMMDD9. 14-11-04

YYMMDD10. 2014-

Page 352: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11-04

YYMMDDxw.

YYMMDDxw.displaysthedateinthesamewaythattheYYMMDD.formatdoes,exceptthatyoucanspecifytheseparator.Thexintheformatnamespecifiestheseparatorthatyou

Page 353: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttouseaccordingtothetablebelow.

x

CharacterDisplayedinOutput Comment

B blank

C colon(:)

D dash(-) Effectively

Page 354: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thesameasusingtheYYMMDD.format.

N noseparator

wisamaximumof8,not10.

Page 355: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

P period(.)

S slash(/)

Thedatewillberight-justifiedwithinthewidththatyouspecify.wcanbefrom2to10,andthedefaultis8.Ifyouspecify2–5,thedatewillbetruncated

Page 356: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ontheright,asSAStriestofitasmuchofthedayandmonthaspossibleinthespaceallowed.Ifyouuse6,noseparatorwillbeused.At7,SASwillprintatwo-digityearwithoutaseparator,and8or9willputatwo-digityearbeforethefirstseparator.Thefollowingtableshows

Page 357: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theresultwhenthedatevalueis19500,whichcorrespondstoMay22,2013.

FormatName Result

YYMMDDN4. 1305

Page 358: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDC5. 13:05

Page 359: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDD6. 130522

Page 360: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDP7. 130522

YYMMDDB8. 130522

Page 361: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDN8. 20130522

Page 362: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDS9. 13/05/22

Page 363: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDD10. 2013-05-22

Page 364: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMONw.

YYMONw.writesdatesasatwo-orfour-digityearfollowedbythethree-lettermonthabbreviation.Itisright-justified.wcanbefrom5to32,andthedefaultis7.Useawidthof7togetafour-digityear.Ifwislessthan7,atwo-digityearwillbe

Page 365: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayed.Ifwislargerthan7,leadingspaceswillbeadded.Thefollowingtableshowstheresultwhenthedatevalueis20796,whichcorrespondstoDecember8,2016.

FormatName Result

Page 366: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMON5. 16DEC

YYMON6. 16DEC

YYMON7. 2016DEC

Page 367: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMON10. 2016DEC

YYQw.

YYQw.writesdate

Page 368: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesasatwo-orfour-digityear,followedbytheletterQ,andasingle-digitrepresentingthequarteroftheyear.Itisright-justified,andwcanbefrom4to32.Thedefaultwidthis6.Use6togetafour-digityear,whileawidthof4or5willgiveyouatwo-digityear.

Page 369: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Specifyingawidthlargerthan6willonlyaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis21384,whichcorrespondstoJuly19,2018.

FormatName Result Comment

Page 370: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQ4. 18Q3 Two-digityear.

YYQ5. 18Q3 Two-digityearwith1leadingspace.

YYQ6. 2018Q3 Four-digityear.

Page 371: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQ8. 2018Q3 Four-digityearwith2leadingspaces.

YYQxw.YYQxw.writesdatevaluesasatwo-or

Page 372: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

four-digityear,followedbyaseparatorthatyouspecify,andasingle-digitrepresentingthequarteroftheyear.xistheletterthatyouusetoindicatetheseparatoraccordingtothetablebelow.UnliketheDDMMYYx.,MMDDYYx.,andtheYYMMDDx.formats,a

Page 373: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

blankisnotavalidseparatorwiththisformat.

x

CharacterDisplayedinOutput Comment

C colon(:)

D dash(-)

Page 374: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

N noseparator

wcanbefrom3to32,withadefaultof4.Whenwis3or4,theyearwillbe

Page 375: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayed

withtwodigits.

P period(.)

S slash(/)

Thisformatisright-justified,andwcanbefrom4to32.The

Page 376: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultwidthis6.Useawidthof6togetafour-digityear,while4or5willgiveyouatwo-digityear.Specifyingawidthlargerthan6willaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis19659,whichcorrespondstoOctober28,2013.

Page 377: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result Comment

YYQN3. 134 Two-digityearwithnoseparator.

YYQC4. 13:4 Two-digityear.

Page 378: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQS5. 13/4 Two-digityearwith1leadingspace.

YYQP6. 2013.4 Four-digityear.

YYQD7. 2013-4

Four-digityearwith1leading

Page 379: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

space.

YYQRw.

YYQRw.writesdatevaluesasatwo-orfour-digityear,followedbytheletterQ,andthequarteroftheyearisrepresentedinRomannumerals.It

Page 380: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isright-justified,andwcanbefrom6to32.Thedefaultwidthis8.Use8togetafour-digityear,while6or7willgiveyouatwo-digityear.Specifyingawidthlargerthan8willaddleadingspaces.

Thefollowingtableshowstheresultwhenthedatevalueis

Page 381: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20312,whichcorrespondstoAugust12,2015.

FormatName Result Comment

YYQR6. 15QIII Two-digityear.

YYQR7. 15QIII Two-digityearwith

Page 382: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1leadingspaceforthethirdquarteronly;four-digityeardisplayedforthe

Page 383: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1st,2nd,and4thquarterswithoutanyleadingspace.

YYQR8. 2015QIII Four-digit

Page 384: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

year.

YYQR12. 2015QIII Four-digityearwith4leadingspaces.

YYQRxw.

YYQRxw.writesdate

Page 385: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesasatwo-orfour-digityear,followedbyaseparatorthatyouspecify,andthequarteroftheyearisdisplayedasaRomannumeral.xistheletterthatyouusetoindicatetheseparatoraccordingtothefollowingtable.Thisisanotherformatthatcannotuseablank

Page 386: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

astheseparator.

x

CharacterDisplayedinOutput Comment

C colon(:)

D dash(-)

N noseparator

wcanbefrom5to

Page 387: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

32,withadefaultof7.Whenwis5or6,theyearwillbedisplayedwithtwo

Page 388: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digits.

P period(.)

S slash(/)

Thisformatisright-justified,andwcanbefrom6to32.Thedefaultwidthis8.Use8togetafour-digityear,while6or7will

Page 389: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayatwo-digityear.Specifyingawidthlargerthan8willaddleadingspaces.Thefollowingtableshowstheresultwhenthedatevalueis19545,whichcorrespondstoJuly6,2013.

FormatName Result Comment

Page 390: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQRP6. 13.III Two-digityear.

YYQRS7. 13/III Two-digityearwith1leadingspaceforthethirdquarteronly;

Page 391: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

four-digityeardisplayedforthe1st,2nd,and4thquarterswithoutany

Page 392: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingspace.

YYQRN8. 2013III Four-digityearwith1leadingspaceandnoseparator.

Page 393: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQRC9. 2013:III Four-digityearwith1leadingspace.

YYQRD10. 2013-III

Four-digityearwith2leadingspaces.

Page 394: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKUw.YYWEEKUw.writesthedatevalueasaweeknumberindecimalformatusingtheUalgorithm.Unlikemanyotherdate,time,anddatetimeformats,itisleft-justified.wcanbefrom3to8,andthedefaultis7.TheUalgorithmcalculates

Page 395: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weeksbasedonSundaybeingthefirstdayoftheweek,andtheweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroifnecessary.Thedisplaythatthisformatpresentsvariesbasedonthewidthspecification.Thisformatissimilartothe

Page 396: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKU.format,exceptthatitdoesnotprovidethenumericaldayoftheweek.Thedatevalueusedinthisexampleis20041,whichcorrespondstoNovember14,2014.

FormatName Result

Page 397: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKU3. W45

Page 398: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKU4. W45

Page 399: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKU5. 14W45

YYWEEKU6. 14W45

Page 400: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKU7. 2014W45

Page 401: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKU8. 2014-W45

YYWEEKVw.

Page 402: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKVw.writesthedatevalueasaweeknumberindecimalformatusingtheValgorithm,whichisInternationalStandardsOrganization(ISO)compliant.Itisleft-justifiedinthesamefashionastheYYWEEKU.format.wcanbefrom3to8,andthedefaultis7.This

Page 403: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatissimilartotheWEEKV.format,exceptthatitdoesnotprovidethenumericaldayoftheweek.TheValgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweek,andtheweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroif

Page 404: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary.

ThisalgorithmdefinesthefirstweekoftheyearascontainingbothJanuary4andthefirstThursdayoftheyear.Therefore,ifthefirstMondayoftheyearfallsonJanuary2,3,or4,theprecedingdaysofthecalendaryearareconsideredtobeapart

Page 405: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofweek53ofthepreviouscalendaryear.Thedatevalueusedinthisexampleis20455,whichcorrespondstoSaturday,January2,2016.Notethatalthoughthedateisin2016,thealgorithmusedbythisformatplacesthedateinweek53oftheyear2015.Thefirstweekofthe

Page 406: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

year2016startsonSunday,January3,2016,asthatcontainsthefirstThursdayoftheyearandJanuary4,2016.

FormatName Result

YYWEEKV3. W53

Page 407: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKV4. W53

Page 408: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKV5. 15W53

Page 409: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKV6. 15W53

YYWEEKV7. 2015W53

Page 410: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKV8. 2015-

Page 411: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

W53

YYWEEKWw.

YYWEEKWw.writesthedatevalueasaweeknumberindecimal

Page 412: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatusingtheWalgorithm.AswiththeYYWEEKU.andYYWEEKV.formats,itisleft-justified.wcanbefrom3to8,andthedefaultis7.ThisformatissimilartotheWEEKW.format,exceptthatitdoesnotprovidethenumericaldayoftheweek.TheWalgorithmcalculates

Page 413: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weeksbasedonMondaybeingthefirstdayoftheweekwithoutanyotherrestriction.Theweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroifnecessary.Thedisplaythatthisformatpresentsvaries,basedonthewidth

Page 414: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specification.Thefollowingtableshowstheresultwhenthedatevalue20455,whichcorrespondstoSaturday,January2,2016(thesamedateusedintheYYWEEKV.algorithmexample).NotethattheWalgorithmassignsthedateasbeinginweekzeroofthecalendar

Page 415: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

year2016.UnliketheValgorithm,itdoesnotconsiderthedateasbeingapartofweek53in2015,butassignsittoweek0in2016.Week1isdefinedasthefirstfullweekin2016.

FormatName Result

Page 416: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKW3. W00

Page 417: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKW4. W00

Page 418: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKW5. 16W00

YYWEEKW6. 16W00

YYWEEKW7. 2016W00

Page 419: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 420: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYWEEKW8. 2016-W00

2.4.2Time

Page 421: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatsTimeformatstranslatesecondsintooneofseveraldifferentwaysofdisplayingtime.OnlytheTIMEAMPM.andTOD.formatsarespecifictoclockvalues,displayingclockvaluesfrom12:00:00a.m.to11:59:59p.m.Allotherformatswilldisplay

Page 422: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hoursgreaterthan23whentranslatingavaluegreaterthanorequalto86400,whichwouldbemidnightofthefollowingday.Thedisplayofminutesalwaysrangesfrom0to59,exceptwhenyouareusingtheMMSSw.dformat.Thebuilt-inSASformatsalwaysdisplaysecondsfrom0

Page 423: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

to59.

Thewidthspecificationfortime(anddatetime)valuesisdifferentfromtheonefordateformatsbecauseithastoallowfordecimalpartsofseconds.Insteadofw,timeanddatetimeformatsarespecifiedasw.d,wherewistheoverallwidth

Page 424: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheentireformat,andthedaccountsforthenumberofdigitstotherightofthedecimalpoint.wmustbegreaterthan(d+1)toaccountforthedecimalpoint.Aswithdateformats,eachoftheseformatshasitsowndefaultwidthspecification,whichisdetailedinthe

Page 425: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

descriptionoftheformat.

HHMMw.dHHMMw.ddisplaysSAStimevaluesashours:minutes.Itisright-justifiedanddoesnotdisplayaleadingzeroinfrontofthehours.wcanbefrom2to20,withadefaultof

Page 426: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5,whiledindicatesthenumberofdecimalplacestotherightoftheminutes.Aspreviouslynoted,wmustbegreaterthand+1,toaccountforthedecimalpoint.ItisdifferentfromtheTIMEw.dformatinthatitdoesnotdisplayseconds.Ifdis0ornotpresent,SASwillround

Page 427: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tothenearestminute.Otherwise,SASwilldisplaythesecondsindecimalminutes(seconds/60).Leadingspaceswillbeaddedtothedisplaybasedonthenumberofdigitsintheresult.Ifthewidthspecifiedisnotlongenoughtoaccommodatethe3spacesforthecolon

Page 428: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtheminutes,SASwillonlydisplaythehoursanddoesnotroundthedisplayedvalue.Thefollowingtableshowstheresultwhenthetimevalueis49794,whichcorrespondsto13hours,49minutes,and54seconds.

Page 429: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result Comment

HHMM2. 13

Page 430: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HHMM3. 13

Page 431: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 432: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HHMM5. 13:50

Page 433: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HHMM5. 13:50

Page 434: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HHMM8. 13:50

HHMM8.2 13:50.90

Page 435: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOURw.d

HOURw.ddisplaysSAStimevaluesashoursanddecimalfractionsofhours.Itisright-justified.wcanbefrom

Page 436: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2to20,withadefaultof2.disthenumberofdecimalplacestotherightofthehours,andwmustbegreaterthand+1toaccountforthedecimalpoint.Ifyoudonotspecifyanydecimalplaces,SASroundstothenearesthour.Thefollowingtableshowstheresultwhenthetimevalueis

Page 437: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

53706,whichcorrespondsto14hours,55minutes,and6seconds.

FormatName Result Comment

HOUR2.0 15 Roundedtothenearest

Page 438: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hour.

HOUR4.2 14.9 55minutes,6seconds,is.92hours;doesnotleave

Page 439: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

enoughspaceforaseconddecimalplace.

HOUR6.2 14.92 Oneleadingspace.

HOUR8.2 14.92 Three

Page 440: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingspaces.

MMSSw.d

MMSSw.ddisplaysSAStimevaluesasminutesandseconds(mm:ss).Itisright-justified.wcanbefrom2to20,withadefaultof5.Ifyoudo

Page 441: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notspecifywlargeenoughtofitminutesandseconds,SASwillroundanddisplaytheminutesonly.dwillprintdecimalfractions(e.g.,tenthsorhundredths)ofseconds.wmustbegreaterthand+1toaccountforthedecimalpoint.Thefollowingtableshowstheresultwhenthe

Page 442: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timevalueis37269,whichcorrespondstothetime10:21:09a.m.(10:21:09).

FormatName Result Comment

MMSS4. 621

MMSS5. 621

Page 443: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMSS8.2 621:09.0

Page 444: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 445: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMSS9.2 621:09.00

Page 446: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEw.d

TIMEw.ddisplaysSAStimevaluesashours:minutes:seconds.Itisright-justifiedanddoesnotprintaleadingzeroinfrontofthehours.wcanbefrom2to20,withadefaultof8,whiledindicatesthenumberofdecimalplacestotherightof

Page 447: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theseconds.wmustbegreaterthand+1toaccountforthedecimalpoint.dwillprintdecimalfractions(e.g.,tenthsorhundredths)ofseconds.

Thisformatisnotrestrictedtoa24-hourday;ifhoursisgreaterthan24,thenitwilldisplaytheactualvalue

Page 448: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofhours,whichmeansthatthedefaultlengthof8maynotbeenoughtoholdtheentireformattedtimevalue.ItisdifferentfromtheHHMM.formatinthatitdisplayssecondsasopposedtodecimalfractionsofminutes.Ifdis0ornotpresent,SASwillroundtothenearestsecond.The

Page 449: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timevalueusedforthefollowingtableis49794seconds,thesamevalueusedfortheHHMMw.dformat.Thetimevaluecorrespondstothetime1:49:54p.m.(13:49:54).

FormatName Result Comment

Page 450: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIME5. 13:49

TIME6. 13:49 Leadingspace.

TIME7. 13:49 Twoleadingspaceswithsecondstruncated.Ifthiswereasingle-digit

Page 451: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hour(i.e.,0through9),theentiretimewouldbedisplayedwithoutleadingspacesbecauseit

Page 452: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wouldfitin7spaces.

TIME8. 13:49:54 Defaultwillaccommodateupto99hours.

TIME9. 13:49:54 Leadingspace;may

Page 453: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

benecessaryforhourvaluesgreaterthan99.

TIMEAMPMw.d

TIMEAMPMw.ddisplaystimein

Page 454: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hours:minutes:secondsfollowedbyaspaceandthenAMorPM.Itisright-justified.wcanbefrom2to20,andthedefaultis11.wmustbegreaterthand+1,toaccountforthedecimalpoint.Anytimevaluegreaterthanorequalto86400(midnight)willbedisplayedasthe12-

Page 455: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hourclocktimeofthenextday.Thisformatdoesnotprintaleadingzero.Ifyouwantthesecondstobeprinted,useatleast11forthewidth.Thefollowingtableshowstheresultwhenthetimevalueis11923,whichcorrespondstothetime3:18:43a.m.

Page 456: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

TIMEAMPM7. 3:18AM

TIMEAMPM9. 3:18AM

Page 457: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEAMPM11. 3:18:43AM

Page 458: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TODw.d

TODw.ddisplaystimeinhours:minutes:secondsusingthe24-hour

Page 459: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

clock.Itisright-justified.wcanbefrom2to20,andthedefaultis11.disthedecimalfractionofseconds.wmustbegreaterthand+1,toaccountforthedecimalpoint.Anytimevaluegreaterthanorequalto86400(midnightofthenextday)willbemarkedasthe24-hourclocktime

Page 460: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthenextday.Thisformatdoesnotprintaleadingzero.Ifyouwantthesecondstobedisplayed,use8forthewidth.Useatleast10ifyouwantdecimalfractionsofsecondsshown.Thefollowingtableshowstheresultwhenthetimevalueis75122,whichcorrespondstothetime

Page 461: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8:52:02p.m.(20:52:02):

FormatName Result Comment

TOD5. 20:52

TOD8. 20:52:02

TOD11. 20:52:02 Threeleading

Page 462: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

spaces;usefulwhenusingfractionsofseconds.

2.4.3Datetime

Page 463: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatsDatetimeformatstranslateSASdatetimevaluesintooneofseveraldifferentformats.SASdatetimevaluesarethenumberofsecondssincemidnight,January1,1960.Youcanuseaformattodisplayboththedateandtime.

Page 464: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Again,youneedtopayattentiontothewidthspecificationindatetimeformatsbecauseitallowsfordecimalfractionsofseconds.Insteadofw,timeanddatetimeformatsarespecifiedasw.d,wherewistheoverallwidthoftheentireformat,andthedaccountsforthe

Page 465: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numberofdigitstotherightofthedecimalpoint.wmustbegreaterthand+1toaccountforthedecimalpoint.Aswithdateformats,eachoftheseformatshasitsowndefaultwidthspecification,whichisdetailedinthedescriptionoftheformat.

Page 466: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

StartingwithSASversion9,therearealsoanumberof"DT"formatsthatwillallowyoutodisplayjustthedateorjustthetimefromadatetimevalue,eliminatingtheneedtousetheDATEPART()orTIMEPART()functionsfordisplaypurposes.Althoughthese"DT"formatsappeartogive

Page 467: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thesameresultastheircorrespondingdateortimeformats,theresultsyougetwillbeverydifferentshouldyouuseadatetimeformatonadatevalueandviceversa.Datetimeformatstranslatesecondssincemidnight,January1,1960,whiledateformatstranslatedays

Page 468: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

sinceJanuary1,1960.

Example2.5showswhathappenswhenyouusedateformatstointerpretdatetimevaluesandviceversa.IfyoutranslatetheSASdatevalue19855usingadateformat,youwillgetthecorrectvalueofMay12,2014(❶and❷).However,ifyouuse

Page 469: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

adatetimeformattotranslatethesamevalue,youwillget5:30:55a.m.onJanuary1,1960,whichcorrespondsto19,855secondsaftermidnight,January1,1960(❸and❹).Insimilarfashion,ifyoutranslatethevalue1705587720usingadatetimeformat,youwillget

Page 470: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2:22p.m.onJanuary17,2014(❺and❻).Thistime,ifyoutrytouseadateformattotranslatethisvalue,youwillgetaseriesofasterisksbecausethevalueistoofarinthefuturefortheSASformatalgorithmtohandle(❼and❽).Evenifyoutrytogetthemonth,day,andyear

Page 471: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthisvalueusingtheappropriatefunctions,itwillnotwork.

Example2.5:TheDifferencebetweenDateandDatetimeValuesinFormatsThatDisplayDates

DATA_NULL_;date=19855;datetime=1705587720;PUT"MMDDYY10.representationofdate="

Page 472: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datemmddyy10.;❶PUT"MONYY7.representationofdate="datemonyy7.;❷PUT"DTMONYY7.representationofdate="datedtmonyy.;❸PUT"WhenvalueofdateisusedasaSAS*datetime*value,thedaterepresentedis:"datedatetime20.;❹PUT"DATETIME20.representationofdatetime="datetimedatetime20.;❺

Page 473: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUT"DTMONYY7.representationofdatetime="datetimedtmonyy7.;❻PUT"MONYY7.representationofdatetime="datetimemonyy7.;❼PUT"WhenvalueofdatetimeisusedasaSAS*date*value,thedaterepresentedis:"datetimemmddyy10.;❽RUN;

Andhereistheresult.

Page 474: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY10.representationofdate=05/12/2014❶MONYY7.representationofdate=MAY2014❷DTMONYY7.representationofdate=JAN60❸WhenvalueofdateisusedasaSAS*datetime*value,thedaterepresentedis:01JAN1960:05:30:55❹DATETIME20.representationofdatetime=17JAN2014:14:22:00❺DTMONYY7.representation

Page 475: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofdatetime=JAN2014❻MONYY7.representationofdatetime=*******❼WhenvalueofdatetimeisusedasaSAS*date*value,thedaterepresentedis:**********❽

Withthatinmind,herearetheformatsthatareapplicabletodatetimevalues.

Page 476: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATEAMPMw.dDATEAMPMw.ddisplaysdatetimevaluesasddmonyy(yy):hh:mm:ss.ssxx,whereddisthedayofthemonth,monisthethree-letterabbreviationforthemonth,andyy(yy)isthetwo-orfour-digityear.Acolonfollows

Page 477: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedate,andthetimeisrepresentedbyhh:mm:ss.ss,followedbyaspaceandthenAMorPM.Itisright-justified.wcanbefrom7to40,andthedefaultis19.disthedecimalfractionofsecondsandmustbelessthanw–1,toaccountforthedecimalpoint.wmustbeatleast13toprint

Page 478: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AMorPM.Ifwis10,11,or12,thetimeisdisplayedasa24-hourclock.Also,ifw–dislessthan17,thedecimalvalueswillbetruncatedtofitthespecifiedfieldwidth.Thisformatproducestwo-digityearsforwidthsof19orless.Thefollowingtableshowstheresultwhen

Page 479: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedatetimevalueis1746745319.6,whichcorrespondstothetime11:01:59.6p.m.onMay8,2015.

Notetheroundingtothenearestsecond,whichcausesthedisplayedresulttoberoundedtothenearestminutewhenthereisn'tenoughroomforthe

Page 480: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

decimal.

FormatName Result

DATEAMPM7. 08MAY15

DATEAMPM12. 08MAY15:11

Page 481: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATEAMPM18. 08MAY15:11:02AM

Page 482: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATEAMPM18.1 08MAY15:11:02PM

DATEAMPM19. 08MAY15:11:02:00

Page 483: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PM

DATEAMPM19.1 08MAY15:11:02:00PM

DATEAMPM21. 08MAY15:11:02:00

Page 484: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PM

DATEAMPM21.1 08MAY15:11:01:59.6PM

Page 485: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIMEw.dDATETIMEw.ddisplays

Page 486: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevaluesasddmonyy(yy):hh:mm:ss.ss,whereddisthedayofthemonth,monisthethree-lettermonthabbreviation,andyy(yy)isthetwo-orfour-digityear.Acolonfollowsthedate,andthetimeisrepresentedbyhh:mm:ss.ss.ItissimilartotheDATEAMPMw.dformat,

Page 487: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

exceptthatitusesthe24-hourclockandthereforedoesnotdisplayAMorPM.Itisright-justified.wcanbefrom7to40,andthedefaultis19,whichwillprovideenoughroomforseconds.Usealengthof15ifyouonlywanthoursandminutestobedisplayedalongwiththedate.d

Page 488: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isthedecimalfractionofsecondsandmustbelessthanw–1toaccountforthedecimalpoint.Ifw–dislessthan17,thedecimalvalueswillbetruncatedtofitthespecifiedfieldwidth.Ifw–dislessthan19,thisformatproducestwo-digityears.Thefollowingtableshows

Page 489: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theresultwhenthedatetimevalueis1698258126.84,whichcorrespondstothetime6:22:07p.m.onOctober24,2013.

FormatName Result

DATETIME16. 24OCT13:18:22:07

DATETIME18. 24OCT13:18:22:07

Page 490: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIME18.1 24OCT13:18:22:06.8

DATETIME19. 24OCT2013:18:22:07

Page 491: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIME19.1 24OCT13:18:22:06.8

Page 492: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIME20. 24OCT2013:18:22:07

Page 493: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIME20.1 24OCT2013:18:22:06.8

DATETIME21. 24OCT2013:18:22:07

Page 494: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIME21.2 24OCT2013:18:22:06.84

Page 495: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTDATEw.

DTDATEw.displaysdatetimevaluesasthenumericaldayofthemonth,followedbythethree-lettermonthabbreviationandtheyearwithoutanyseparatingcharacters.Itisright-justifiedwithinthefield.wcan

Page 496: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

befrom5to9,andthedefaultwidthis7.Ifyouwanttodisplayfour-digityears,useDTDATE9.TheoutputisidenticaltotheoutputusingtheDATE.format.Thedifferenceisthatthisformatwillonlyworkcorrectlywithdatetimevalues,whiletheDATE.formatonlyworkscorrectly

Page 497: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withdatevalues.Thefollowingtableshowstheresultwhenthedatetimevalueis1729519835.7,whichcorrespondstothetime2:10:36p.m.onOctober21,2014.

FormatName Result

Page 498: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTDATE5. 21OCT

DTDATE6. 21OCT

DTDATE7. 21OCT14

DTDATE8. 21OCT14

DTDATE9. 21OCT2014

Page 499: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTMONYYw.

DTMONYYw.displaysthedatefromadatetimevalueasthethree-lettermonthabbreviationfollowedimmediatelybytheyear.Thereareno

Page 500: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

separatingcharacters.Itisright-justified,andwcanbefrom5to7,withadefaultof5.Specifying5or6willgiveyouatwo-digityear,while7willgiveyouafour-digityear.AlthoughthisformatappearstoproducethesameMMMyy(yy)resultastheMONYY.format,theDTMONYY.

Page 501: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatonlyworkswithdatetimevalues,whiletheMONYY.formatonlyworkswithdatevalues.Thefollowingtableshowstheresultwhenthedatetimevalueis1727036339.1,whichcorrespondstothetime8:18:59p.m.onSeptember22,2014.

Page 502: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

DTMONYY5. SEP14

DTMONYY6. SEP14

DTMONYY7. SEP2014

Page 503: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTWKDATXw.

DTWKDATXw.writesdatetimevaluesasdayofweekname,day,monthname,andyear.ItdiffersfromtheWEEKDATX.formatinthatitworksondatetimevalues,notdatevalues.Itisright-justified,andwcanrangefrom3to37.The

Page 504: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultis29,whichisthemaximumwidthofadateinthisformat.Specifyinganythinglongerthan29willcauseleadingspacestobeadded.Ifthewidthspecifiedistoosmalltodisplaythecompletedayoftheyearandmonth,SASwillabbreviate.Itwillfirstabbreviatethemonth

Page 505: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andthenthedayoftheweekasnecessary.Thefollowingtableshowstheresultwhenthedatetimevalueis1645971726,whichcorrespondstothetime2:22:06p.m.onFebruary27,2012.

FormatName Result

DTWKDATX3. Mon

Page 506: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTWKDATX9. Monday

DTWKDATX15. Mon,27

Page 507: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Feb12

DTWKDATX23. Monday,27Feb2012

DTWKDATX29. Monday,27February

Page 508: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012

DTYEARw.

DTYEARw.displaystheyearforthegivendatetimevalue.TheDTYEAR.formatisidenticalinresulttotheYEAR.format,but

Page 509: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

itisusedwithdatetimevaluesinsteadofdatevalues.Itisright-justified,andwcanbefrom2to4,withadefaultwidthof4.Whenwisspecifiedas2or3,atwo-digityearisused.Thefollowingtableshowstheresultwhenthedatetimevalueis1716310050.2,whichcorrespondsto

Page 510: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thetime4:47:30p.m.onMay21,2014.

FormatName Result Comment

DTYEAR2. 14 Two-digityear.

DTYEAR3. 14 Two-digityearwith

Page 511: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aleadingspace.

DTYEAR4. 2014 Four-digityear.

DTYYQCw.

DTYYQCw.writesdatetimevaluesasa

Page 512: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

two-orfour-digityear,followedbyacolon,andasingle-digitrepresentingthequarteroftheyear.Itisright-justified,andwcanbefrom4to6.Thedefaultwidthis4.Useawidthof6togetafour-digityear.Use4or5togetatwo-digityear.Thisgivesyouthesameresultwith

Page 513: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevaluesthatusingtheYYQC.formatwouldyieldwithadatevalue.Thefollowingtableshowstheresultwhenthedatetimevalueis1662064659.5,whichcorrespondstothetime8:37:40p.m.onAugust31,2012.

Format

Page 514: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Name Result Comment

DTYYQC4. 12:3 Two-digityear.

DTYYQC5. 12:3 Leadingspace;two-digityear.

DTYYQC6. 2012:3 Four-digit

Page 515: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digit

year.

MDYAMPMw.MDYAMPMw.writesdatetimevaluesinthefollowingformat:mm/dd/yy(yy)hh:mmAM/PM.Therearenoleadingzeroesineitherthedateorthetimeportionsofthis

Page 516: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

format.Itisright-justified,andwcanbefrom8to40,withadefaultwidthof19.Thefollowingtableshowstheresultwhenthedatetimevalueis1716089122,whichcorrespondstothetime3:25:22a.m.onMay19,2014.Notethatinthefollowingexampletherearetwospaces

Page 517: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenthedateandthetimebecausethehourisonlyasingledigit.

FormatName

MDYAMPM8.

Page 518: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MDYAMPM15.

MDYAMPM16.

Page 519: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MDYAMPM17. 5/19/143:25AM

MDYAMPM18. 5/19/143:25AM

MDYAMPM19. 5/19/20143:25AM

MDYAMPM21. 5/19/20143:25AM

MDYAMPM24. 5/19/20143:25AM

2.5Creating

Page 520: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CustomDateFormatsUsingtheVALUEStatementofPROCFORMATInadditiontothedateandtimeformatssuppliedwithSAS,youcancreateyourowncustomformatswiththeFORMATprocedure.Withdates

Page 521: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtimes,youcanmodifythedefaultdisplayofanexistingSASformatorcreateyourownusingtheVALUEorthePICTUREstatement.BelowIshowhowtomodifythedefaultdisplayofanexistingSASformatusingtheVALUEstatement.

Page 522: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example2.6:CreatingYourOwnDateFormatwiththeVALUEStatementinPROCFORMATAnaccesscontrolcompanywantsareportofthepeople

Page 523: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whosesecuritycardshaveexpiredasofJanuary1,2014,andtheyhavetheexpirationdateforeachcard.Insteadofhavingtoreadthereportanddeterminewhichdatesarepriortothecutoff,theywanttodisplayanydatepriortoJanuary1,2014,as"EXPIRED,"anddates

Page 524: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

afterthatusingtheMMDDYY10.format.Setthevalueto"EXPIRED"fordatespriortoDecember31,2013,usingthatastheupperlimit,andthespecialvalue"LOW"asthebottomoftherange.Fortheremainingvalues,usethelowerlimitofJanuary1,2014,and

Page 525: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thespecialvalue"HIGH"astheupperlimit,andindicatetheSASformattobeappliedtothisrangeinbrackets❶.IfyoudonotenclosetheSASformatinbrackets,youwillgetanerrorintheFORMATprocedure.

PROCFORMAT;VALUEexp

Page 526: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LOW-'31DEC2013'd="Expired"'01JAN2014'd-HIGH=[MMDDYY10.];❶RUN;

PROCPRINTDATA=access;IDcard_num;VARexp_dateexp_date_raw;FORMATexp_dateexp.exp_date_rawDATE9.;RUN;

card_num exp_date

Page 527: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

84485598 11/14/2015

16205371 11/27/2014

63656754 01/14/2014

10270040 Expired

94822015 Expired

27800904 Expired

97189418 08/14/2014

70815194 03/14/2016

Page 528: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

50465401 Expired

43034970 09/28/2014

Example2.7:CreatingYourOwnTimeFormatwiththeVALUEStatementin

Page 529: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCFORMATInordertobeabletodrivethenextstageinaroadrace,driversmustfinishapreviousstageintenminutesorless,andtheresultsareposted.Thisexampleshowsthatyoucancustomizetimeanddatetimeformatsaswellasdateformats.

Page 530: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Tocustomizedatetimeformats,youwouldspecifyadatetimeformatinsteadofadateortimeformat.

PROCFORMAT;VALUEqualifyLOW-'00:10:00't=[MMSS5.];'00:10:00't<-HIGH='DidNotQualify';RUN;

PROCPRINTDATA=racers;IDname;

Page 531: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VARtime;FORMATtimequalify.;RUN;

name time

Bork DidNotQualify

Bova DidNotQualify

Brantley 08:31

Page 532: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Brickowski 08:59

Burkhart 07:10

Burroughs 08:05

Butler DidNotQualify

2.6CreatingCustomDate

Page 533: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatsUsingthePICTUREStatementofPROCFORMATTocreateyourowndateandtimeformatswiththePICTUREstatementintheFORMATprocedure,youneedtousetheDATATYPE=option.

Page 534: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATATYPEcantakethevaluesof"DATE,""TIME,"or"DATETIME"toindicatethetypeofvalueyouareformatting.Youthenneedtodefineyourdisplaybyusingoneormoreofthepictureformatdatedirectivesshowninthelistbelow.Thesedirectivesarecase-sensitive.

Page 535: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Table2.1:PictureFormatDateDirectives

DateDirective Description

%a Locale'sabbreviatedweekdayname.Localeisdefinedby

Page 536: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theLOCALE=systemoption.

%A Locale'sfullweekdayname.Localeisdefinedbythe

Page 537: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LOCALE=systemoption.

%b Locale'sabbreviatedmonthname.Localeisdefinedbythe

Page 538: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LOCALE=systemoption.

%B Locale'sfullmonthname.LocaleisdefinedbytheLOCALE=

Page 539: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

systemoption.

%d Dayofthemonthasadecimalnumber(1–31),withnoleadingzero.Putazero

Page 540: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenthepercentsignandthe"d"tohavealeadingzerointhedisplay.

%H Hour(24-

Page 541: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hourclock)asadecimalnumber(0–23),withnoleadingzero.Putazerobetweenthepercentsignandthe"H"

Page 542: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tohavealeadingzerointhedisplay.

%I Hour(12-hourclock)asadecimalnumber(1–12),withno

Page 543: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzero.Putazerobetweenthepercentsignandthe"I"tohavealeadingzerointhedisplay.

Page 544: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%j Dayoftheyearasadecimalnumber(1–366),withnoleadingzero.Putazerobetweenthe

Page 545: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

percentsignandthe"j"tohavealeadingzerointhedisplay.

%m Monthasadecimal

Page 546: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

number(1–12),withnoleadingzero.Putazerobetweenthepercentsignandthe"m"tohavealeadingzero

Page 547: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inthedisplay.

%M Minuteasadecimalnumber(0–59),withnoleadingzero.Putazerobetweenthe

Page 548: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

percentsignandthe"M"tohavealeadingzerointhedisplay.

%p Locale'sequivalentof

Page 549: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

eithera.m.orp.m.LocaleisdefinedbytheLOCALE=systemoption.

%S Secondasa

Page 550: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

decimalnumber(0–59),withnoleadingzero.Putazerobetweenthepercentsignandthe"S"tohavea

Page 551: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzerointhedisplay.

%U Weeknumberoftheyear(Sundayasthefirstdayoftheweek)

Page 552: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asadecimalnumber(0–53),withnoleadingzero.Putazerobetweenthepercentsignandthe"U"tohavea

Page 553: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzerointhedisplay.

%w Weekdayasadecimalnumber,where1isSundayandSaturdayis

Page 554: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7.

%y Yearwithoutcenturyasadecimalnumber(0–99),withnoleadingzero.Putazerobetweenthe

Page 555: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

percentsignandthe"y"tohavealeadingzerointhedisplay.

%Y Yearwithcenturyasadecimal

Page 556: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

number(four-digityear).

%% Thepercentcharacter(%).

Notethatifyouaregoingtousethesedirectivesinapicture

Page 557: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

format,youwillprobablywanttoaddthefollowinglineofcodeorsomevariantinthePICTUREstatementtohandlethedisplayofmissingvalues.

.-.z="NoDateGiven"

Example2.8createsaformatsimilartotheWORDDATE.format,

Page 558: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

exceptthatleadingzeroesareapartofthedatedisplay.Payspecialattentiontohowthepictureiscreatedinline4.Whenyouareusinganyofthedatedirectivesfromthetableabove,youmustencloseyourpicturestringinsinglequotes.Ifyouusedoublequotes,SASwill

Page 559: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

trytointerpretthedirectivesasmacrocallsbecauseofthepercentsign(%).ThePROCFORMATstepwillexecutewithouterror,andyouwillnotseeanywarningsintheSASloguntiltheformatisused.Theformatwillnotworkifyouhaveamacrothathasthesamenameas

Page 560: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theformatyoucreated.

Example2.8:CreatingaPictureFormatforDatesUsingDateDirectives

1PROCFORMAT(DEFAULT=21);2PICTUREZWDATE3.-.z="NoDateGiven"4LOW-HIGH='%B%0d,%Y'(DATATYPE=DATE);5RUN;

6PROCPRINTDATA=pictest

Page 561: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LABEL;7VARdatedate2;8FORMATdateworddate.date2zwdate21.;9LABELdate="DateUsingWORDDATE."10date2="DateusingZWDATE.";11RUN;

Line3definesthedisplayformissingvalues.Withoutit,youwillseetheSASmissingvaluesymbol:

Page 562: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

eitheraperiod(.)oraspecialmissingvalue.Line4givesthepictureofhowthedateistobedisplayed.Thezeroprecedingthedaydirective(%0d)causestheleadingzerotobeprintedasapartofthedate.Thelengthofthestringwiththedatedirectivesdeterminesthedefaultwidthofthe

Page 563: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

format.Inthisexample,thedefaultwidthis10,butinordertomakesurethatallthedatesprintcorrectly,theFORMATstatementinline8setstheformatwidthto21.Whatfollowsistheresultingoutput.

Date

Page 564: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DateusingWORDDATE.

usingcustomPICTUREformat

ZWDATE.

July8,2012 July08,2012

April17,2014

April17,2014

Page 565: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

October30,2013

October30,2013

. NoDateGiven

May14,2012

May14,2012

February2,2013

February02,2013

. NoDate

Page 566: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Given

November26,2014

November26,2014

Example2.9:UsingDateDirectiveswithTextThisexampledefinesa

Page 567: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatthatwilldisplaytextalongwiththedateandtime.Itprovidesastandardizeddateandtimestampthatcanbeaddedasatitleorfootnotetoreports(forexamplesofhowtodothis,seeSections6.1.2andsection6.1.3.Inline2ofthecodebelow,theDEFAULT=optionisusedto

Page 568: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specifyadefaultlengthfortheformat.Withoutthatoption,thisformatwouldhaveadefaultlengthof35,whichisthenumberofcharactersbetweenthequotes.However,thereisnowaythatthedefaultwidthcansuccessfullyprinttheentiredateandtimestampforallcases,

Page 569: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

becausefullmonthnamesarealwayslongerthantwocharacters,whichisallthattheformatdirective"%B"makesroomfor.Similarly,thefour-digityearwilltakeupmorethanthetwocharactersaccountedforbythe%Ydirective.Thecodebelowusestheformatwidthof35

Page 570: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toillustratewhatwouldhappenwithouttheDEFAULT=option.

Inline3,ifthedatetimestampismissing,thedatetimestampdisplaystheword"Missing."Thepictureforallothervaluesisdefinedinline4;"LOW-HIGH"specifiestherange.The

Page 571: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

picturedescriptionbeginswithtext,andthedateisrepresentedbythefullmonthname(%B),followedbythenumericday(%dwithoutaleadingzero),acomma,andthefour-digityear(%Y).Thetimefollowstheword"at"andisrepresentedasa12-hourclocktime

Page 572: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withoutseconds(%I:%0M),followedbyAMorPM(%p).TheDATATYPE=optiontellstheformatthatitwillbereceivingdatetimevaluestotranslate.

SASCode1PROCFORMAT;2PICTURErptdate(DEFAULT=43)

Page 573: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.-.Z='Missing'4LOW-HIGH='Generatedon%B%d,%Yat%I:%0M%p'(DATATYPE=DATETIME);5RUN;67DATA_NULL_;8rpt_date="15dec2012:22:25:00"dt;9PUT'SASdatetimevalue='rpt_date;10PUT'FormattedwithDATETIME.='rpt_datedatetime.;11PUT'Usingcustomformatatwidthof35='rpt_daterptdate35.;

Page 574: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12PUT'Usingcustomformatatdefaultwidthof43='rpt_daterptdate.;12RUN;

TheResultSASdatetimevalue=1671229500FormattedwithDATETIME.=15DEC12:22:25:00Usingcustomformatatwidthof35=GeneratedonDecember15,2012at1❶Usingcustomformatatdefaultwidthof43

Page 575: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

=GeneratedonDecember15,2012at10:25PM

Asyoucansee,withoutthecorrecteddefaultwidth,theresultingoutputistruncatedat35characters.

2.7CreatingCustomFormats

Page 576: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

UsingPROCFCMPforProcessingThereisanotherpossibilityforcustomformatsbeyondthefunctionalityoftheVALUEandPICTUREstatements.YoucanactuallyprocessdatausingPROCFCMPtowriteyourownfunctionsandthen

Page 577: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displaytheresultofthatfunctionasaformattedvalue.Thiscanbehelpfulforhandlingdirtydatedata(missingvaluesformonth,day,oryear)orimputingvaluesformissingdatecomponents.WhilethespecificsofPROCFCMParebeyondthescopeofthisbook,the

Page 578: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

followingsectionwilldemonstratesomeofthecapability.

Example2.10:UsingPROCFCMPtoImputeDateswithAnnotationforImputedDates

Page 579: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thisexampledefinesaformatthatwilltakeadate,andifanycomponentismissing,itwillimputeavaluefordisplayonthereportalongwithanindicatorofwhichcomponentismissing.Theadvantageofdoingthiswithaformatisthattheoriginalvalueinthedatasetis

Page 580: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

untouched,allowingforinvestigationandlatercorrection.TheOUTLIB=option(❶)onthePROCFCMPstatementdefinesthelocationwherethecompiledfunctionistobestored.TheFUNCTIONstatement(❷)namesthefunctionanddescribestheinput

Page 581: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

parameter(cdate;the$indicatesthatitisacharactervalue),andtheseconddollarsignindicatesthatthereturnedvaluewillalsobecharacter.

PROCFCMPCodetoCreatetheMAKEDATE()Functionfor

Page 582: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ImputationODSESCAPECHAR='~';

PROCFCMPOUTLIB=control.functions.dates;❶

FUNCTIONmakedate(cdate$)$;❷LENGTHdtstr$42supr$16;supr='';/*Initializesuperscriptvariable*//*assumethatdataisinMMDDYYYYform,but

Page 583: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

check*//*Parsedatestring,getelements*/cmo=SCAN(cdate,1,'/-.:','M');cdy=SCAN(cdate,2,'/-.:','M');cyr=SCAN(cdate,3,'/-.:','M');IFcyrEQ''ORINDEX(cyr,'_')GT0ORcyrEQ'.'THENy=.;ELSE

Page 584: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

y=INPUT(cyr,4.);

/*Testyearvalue-earliestdateis1990,cannotbeinfuture*/IFyne.and(1990LTyLTYEAR(TODAY()))THENDO;/*assumeyearisgood*/IFcmoEQ''ORINDEX(cmo,'_')GT0ORcmoEQ'.'THENm=.;ELSEm=INPUT(cmo,2.);IF1LEmLE12

Page 585: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

THENDO;/*monthhastobebetween1and12*//*Monthisgoodchkday*/IFcdyEQ''ORINDEX(cdy,'_')GT0ORcdyEQ'.'THENd=.;ELSEd=INPUT(cdy,2.);IFdLE1ORdGT(DAY(INTNX('MONTH',MDY(m,1,y),0,'e')))THENDO;/*Dayisbadforthismonth-imputemiddleofmonth*/

Page 586: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

d=CEIL(DAY(INTNX('MONTH',MDY(m,1,y),0,'e'))/2);supr='~{superDay}';END;END;ELSEDO;/*Monthisbad-resetmonthandday*/m=6;d=30;supr='~{superMonth}';END;/*Createthedatevalue*/

Page 587: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date=MDY(m,d,y);dtstr=CATT(PUT(date,mmddyy10.),supr);END;ELSEDO;/*yrisbad-flag*/dtstr='Unknown~{superY}';END;RETURN(dtstr);ENDSUB;RUN;QUIT;

Thissectionofthe

Page 588: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

examplewillusetheMAKEDATE()functioncreatedaboveinsideofacustomformat.Itisimportanttonotethatthedatesdisplayedarecharacters;therefore,theformatisacharacterformat(asindicatedbytheleadingdollarsign[$]).TheOTHER=keywordwithnoother

Page 589: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatrangesdefinesarangeofallpossiblevalues,andplacingthefunctionnameinsidethebrackets(❷)appliesthefunction.

UsingtheMAKEDATE()FunctionasaCustomFormat

PROCFORMAT;VALUE$mkdt

Page 590: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

❶OTHER=[makedate()]❷;RUN;

DATAerrorcnt;INPUTrecno@4rptdt$10.errs;DATALINES;113/15/2013374209/08/20133302/30/2013324__/15/201315504/27/2013195605/__/201317707/08/____20806/04/0135

Page 591: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

908/32/201341011//201361101/23/203111;;;;RUN;

TITLE1'ErrorCountsbyDate';PROCREPORTDATA=errorcntNOWDSPLIT='\';COLUMNrecnorptdtrptdt=dterrs;DEFINErecno/order'Record#';DEFINErptdt/'Original\Datefrom\ErrorLog';

Page 592: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DEFINEdt/'Imputed\Date'f=$mkdt36.;❶DEFINEerrs/"Errors\Reported";RUN;

ThePROCREPORTstepaboveusesthevaluethatisstoredinthedatasetandcreatesanalias(DT)forit.Weapplytheformatwecreatedtothataliased

Page 593: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variable(❶)toshowboththeoriginalandtheon-the-flyimputationaliasforit.

TheOutput

Record#

OriginalDatefromErrorLog

ImputedDate

1 13/15/2013

Page 594: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2 09/08/2013

3 02/30/2013

4 __/15/2013

5 04/27/2013

6 05/__/2013

7 07/08/____

8 06/04/013

Page 595: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9 08/32/2013

10 11//2013

11 01/23/2031

Itisimportanttorealizethatusingthisfunctionasaformatdoesnotcreateimputedvaluesinthedataset.Itisonlyawayto

Page 596: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displaythevaluesfromthedataset.

2.8ThePUT()FunctionandFormatsWhathappensifyouneedtousetheformattedvalueofadateinacharacter

Page 597: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringyou'reassembling?Ifyouusethevariablethatcontainsthedatevalue,you'llgettheactualSASdatevalue,regardlessofanypermanentformatsassignedtothevariable.ThePUT()functionisusedtostoretheformattedvalueofanumeric

Page 598: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueinacharactervariable.Thesyntaxis:

PUT(value,format);

valueisaconstantoravariable(eithernumericorcharacter),andformatisthenameofaSASformat.Ifyouareformattingacharactervariableorconstant,thenthe

Page 599: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatyouusemustbeacharacterformat.Similarly,ifyouareformattinganumericvalue,theformatmustbeanumericformat.Example2.11willdemonstratehowtousethePUT()function.ThePUT()function❶createsthecharactervariablechar_value.Thelengthofthevariable

Page 600: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inthiscaseisdefinedbythewidthoftheformat,10.Ifyou'vealreadydefinedthelengthofthecharactervariablewhereyoustoretheresult,ithastobeatleastaslongastheformatwidth,oryouroutputwillbetruncated.

Example2.11:Usingthe

Page 601: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUT()FunctiontoCreateaCharacterDateString

DATAsample281;numeric_value=18947;fmt_num_value=18947;char_value=PUT(numeric_value,MMDDYY10.);❶FORMATfmt_num_valuemmddyy10.;LABELnumeric_value="SASDate\Value"fmt_num_value="Formatted\SASDate\Value"char_value="CharacterDate\inString";

Page 602: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUN;

ODSRTFFILE="c:\book\2ndEd\examples\putfunction.rtf";

PROCPRINTDATA=sample281NOOBSSPLIT='\';RUN;ODSRTFCLOSE;

SASDateValue

FormattedSASDateValue

Page 603: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

18947 11/16/2011

Sowhat'sthedifference?Theformattedvaluelooksthesameasthecharacterdate,right?However,inthissimplePROCPRINT,onedifferenceisapparent.Thenumericvariableisright-justified(the

Page 604: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultfortheMMDDYY.formatisright-justified),whilethecharactervariableisleft-justified(defaultforcharactervariables).Ifyoulookatthelabels,youwillseethatthelabelsalsoreflectthedefaultjustification.Thelabelsforthenumericvariablesareright-

Page 605: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

justified,andtheoneforthecharactervariableisleft-justified.

WhywouldyouneedtouseacharacterrepresentationofaSASdate?Ifyouareprintingadateinsideofatextstring(e.g.,inatitleorfootnote,orinthetextofaformletter),youwillneedto

Page 606: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

createacharactervariablewiththedate.Example2.12putstheRPTDATE.formatcreatedinSection2.6.1oneachpageofareportasafootnote.

Example2.12:UsingaCharacterDateStringinaFootnote

PROCFORMAT;PICTURErptdate(DEFAULT=43)

Page 607: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

.-.Z='Missing'LOW-HIGH='Generatedon%B%d,%Yat%I:%0m%p'(DATATYPE=DATETIME);RUN;

Inordertodothis,wewillhavetocreateamacrovariabletostorethevaluesothatitcanbeusedinaFOOTNOTEstatement.ThemethodchosenhereisviaaDATAstep

Page 608: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtheCALLSYMPUTXstatement.CALLSYMPUTXtrimsanytrailingspacesasopposedtoCALLSYMPUT.Thereareotherwaystocreatethemacrovariable&RPTDTMaswell(seeSection6.1.3).

DATA_NULL_;CALL

Page 609: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SYMPUTX('RPTDTM',PUT(DATETIME(),RPTDATE.));RUN;FOOTNOTE"&RPTDTM";

Page 610: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter3:ConvertingDatesandTimesintoSASDate,Time,andDatetime

Page 611: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Values

3.1AvoidingtheTwo-DigitYearTrap3.2UsingInformats3.3TheINFORMATStatement

Page 612: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.4ListingandDiscussionofInformats

InChapter2,IdiscussedtranslatingSASdatestothewayweexpressthem.Howcanwedothereverse?Afterall,ifyouhaveadate,time,ordateandtimethatyouneedto

Page 613: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

storeormanipulate,itwon'tberepresentedasaSASdate,time,ordatetimevalue(unlessitcomesfromanotherSASdataset).Thetranslationfromcommondateand/ortimeterminologytoSASisalmostaseasyasgoingtheotherway,anditisdoneinoneoftwoways:Thefirst,

Page 614: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

discussedinSection1.4,usesdate,time,anddatetimeliterals.Whilethisworksforasmallnumberofthesevaluesthatareknownatthetimeyouwritetheprogram,howdoyoudealwithmultipledates,orthosethatareknownonlyatruntime?Byusinginformatstoprocess

Page 615: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

them.

3.1AvoidingtheTwo-DigitYearTrapFirst,itisalwaysgoodpracticetousefour-digityearsinyourdatevalues.Ifthisisnotpossible,SASwill

Page 616: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

handledateswithtwo-digityearswithoutanyproblems,butthissituationiswhereyou,theuser,shouldbeconcernedwiththeYEARCUTOFF=systemoption.Anytimethatyoutranslateadateordatetimefromaneverydayrepresentationusingtwo-digityears(for

Page 617: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

example,05/16/89)toitsSASvalue,theYEARCUTOFF=systemoptionwillaffecttheresultingSASvalue.TheYEARCUTOFF=systemoptionwilldefinethebeginningofthe100-yearperiodforthosetwo-digityears.InSAS9.4,theSASdefaultvalueforthisoptionis1926,

Page 618: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

definingarangeof1926through2025.Therefore,two-digityearsintherangefrom26through99willbeassignedtotheyears1926–1999,whileyearvaluesfrom00through25willbesettotheyears2000–2025.ThisruleappliestoeverythingthathastobetranslatedintoaSAS

Page 619: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateordatetimevalueandwherethereareonlytwodigitsrepresentingtheyear.ThismeansthattheYEARCUTOFF=optionappliestoexternaldatabeingprocessedwiththeINPUTstatement,anydateordatetimeliterals,aswellasanyfunctions(suchasINPUT())wherethe

Page 620: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inputstringonlyhastwodigitsrepresentingtheyear.TheexamplebelowshowshowdateanddatetimeliteralsareaffectedbytheYEARCUTOFF=systemoption.ItdisplaystheactualSASdateordatetimevaluerepresentedbytheliteralalongwithitsformattedvalue.

Page 621: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example3.1:EffectsoftheYEARCUTOFF=SystemOptiononDateandDatetimeLiteralsOPTIONSYEARCUTOFF=1920

Page 622: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASDateLiteral

a1 '23MAR2005'd

a2 '23MAR1905'd

a3 '23MAR05'd

a4 '19AUG1959:14:45:00'dt

a5 '19AUG2059:14:45:00'dt

Page 623: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

a6 '19AUG59:14:45:00'dt

Thevariablesa3anda6aredateanddatetimeconstants,respectively,withtwo-digityearsasapartoftheconstant.Whiletheliteralswithfour-digityearsarestoredastheirproperSASdates,

Page 624: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

a3isinthe21stcentury,buta6issetinthe20thcentury.The100-yearrangeis1920–2019.Therefore,'05'fallsintherange00–19andisassignedtotheyear2005,and'59'fallsintherange20–99andisassignedtotheyear1959.Forthefollowingexample,let'susethesamedate

Page 625: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddatetimeliterals,butchangethevalueoftheYEARCUTOFF=option.

OPTIONSYEARCUTOFF=1905

SASDateLiteral

a1 '23MAR2005'd

Page 626: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

a2 '23MAR1905'd

a3 '23MAR05'd

a4 '19AUG1959:14:45:00'dt

a5 '19AUG2059:14:45:00'dt

a6 '19AUG59:14:45:00'dt

Now,a3hasmovedtothe20thcentury.Theliteralswithfour-digit

Page 627: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearsremainstoredastheirproperSASdatesanda6remainsinthe20thcentury,butwehavemovedthe100-yearrangetostartin1905.Therefore,therangeisnow1905–2004,and'05'fallsinthe05–99range,soitisassignedto1905.

Thesamethingwill

Page 628: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

happenwhenyouuseaninformattotranslateadateordatetimestringwherethereareonlytwodigitsrepresentingtheyear.

3.2UsingInformatsFormatstakevalues

Page 629: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddisplaytheminaspecificfashion,whileinformatstakeaseriesofalphanumericcharactersandtranslatethemintoasinglevalue.Datesandtimesarethebestexampleofapplyinginformats,sinceSASdatevaluesarenotnormallyhowthemajorityoftheplanetexpressesdates.

Page 630: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Adatesuchas11/24/05,or24-05-2002containsnon-numericcharacters,sotheywouldhavetobereadascharactervalues,whichisquiteadistancefromnumericSASdatevalues.Aswithformats,youcancreate(andstoreforfutureuse)yourowninformatsifoneisnot

Page 631: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

availablewithinSAStofityourneeds.TherearefewerSAS-suppliedinformatsthanformats,soyoushouldbecarefultoonlyusetheinformatslistedinthischaptertotranslateyourdateanddatetimestrings,andnottheformatsfromChapter2.

Page 632: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JustasyoucanusetheFORMATstatementtoapplyaformattoavariable,youcanusetheINFORMATstatementtoapplyaninformattoavariableinaprocedureoraDATAstep.However,themostcommonuseofinformatsiswiththeINPUTstatementinaDATAstep,asdataare

Page 633: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beingreadin,orwiththeINPUT()functiontotranslatecharacterdatathatarealreadyindatasets.

Youspecifyaninformatbygivingtheinformatname,followedbyanoptionalwidthspecificationandaperiod(.).Informatsarelikeformatsinthat

Page 634: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

eachinformathasadefaultwidththatSASwilluseifnoneisspecified.

3.3TheINFORMATStatementTheINFORMATstatementisanalogoustotheFORMAT

Page 635: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statement.YouusetheINFORMATstatementtoassociateaninformatwithavariableinaSASdataset.Youcanalsoremoveaninformatthathasbeenpermanentlyassociatedwiththevariablebyleavingtheinformatnameblank.Youmightalsousethe

Page 636: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INFORMATstatementtoassociateaninformatwithavariableforthedurationoftheprocedure(whichmightbeusefulincertainproceduressuchastheFSEDITprocedure).

INFORMATdate1mmddyy10.;

Page 637: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thestatementabovesaysthatanytimeacharacterstringisreadintothevariabledate1,itwillbetranslatedintoaSASdatevalueusingtheinformatMMDDYY10(detailedbelow).IfthecharacterstringbeingprocessedisnotinanMMsDDsYY(YY)(sstandsforaseparator),

Page 638: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youwillgetamissingvaluestoredindate1andanerrormessageintheSASlog.

Justaswithformats,youcanremoveinformatsthathavebeenpermanentlyassociatedwithavariablebyusingthevariablenameintheINFORMATstatement

Page 639: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withoutaninformat,asisshownbelow.

INFORMATtime3;

Thisstatementwillremoveanyinformatthathasbeenpermanentlyassociatedwiththevariabletime3.

3.3.1Using

Page 640: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

InformatswiththeINPUTStatementThebasicsyntaxofanINPUTstatementwithaninformatisasfollows:

INPUT@1date1mmddyy10.;

First,youwillusually

Page 641: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specifyastartingcolumn.Thedefaultstartingcolumnis1,butyoucanspecifythestartingcolumnwiththe@sign,followedbythecolumnnumber.Ifyoudonot,thestartingcolumnwillbesettothecurrentlocationoftheinputpointer.Youshouldalsospecifyawidthfortheinformat

Page 642: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toindicatehowmanycharactersaretoberead.TheaboveINPUTstatementwillreadthefirsttencharactersinaline,startingatthefirstcharacterinadataline,andSASwillexpectittolooklikemmsdds(yy)yy,wheremmisthemonthfrom01to12,srepresentsaseparatorcharacter,dd

Page 643: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isadayfrom01to31,and(yy)yyisatwo-orfour-digityear.Thefollowingexampledemonstratesthattheseparatorcharacterdoesnothavetobethesameoneveryline,andthefielddoesnothavetobeexactlytencharacterslong.

Example3.2:INPUT

Page 644: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Statement1.DATAinformats_are_smart;2.INPUT@1date1:MMDDYY10.;3.unformatted_date=date1;4.DATALINES;5.10/17/20146.05-04-597.3-1-19408.RUN;9.PROCPRINT;10.FORMATdateworddatx.;

Page 645: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11.RUN;

TheResult

Obs date1 unformatted_date

1 17October2014

2 4May1959

Page 646: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3 1March1940

Asyoucansee,thecharactersineachlineoftheDATALINESstatementwereconvertedtoaSASdatevalue.Thelengthoftheinformatmustbelongenoughtoreadallof

Page 647: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thecharactersinthedatestring.Intheexample,thereareamaximumof10charactersinthedatestring.Therefore,thewidthoftheinformatisspecifiedas10.Theotherimportantthingtonoteistheuseofthecolon(:,line2,inbold)modifierprecedingtheinformat

Page 648: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

name.Itisbestpracticetousethecolonmodifierwithanyinformat,notjustthoserelatingtodatesandtimes,whenyouaredealingwithvaryinglengthfields.Thecolonmodifierensuresthatitdoesn'tmatterifthelengthsofthecharacterstringsrepresentingdateinthedataareless

Page 649: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thanthewidthspecifiedfortheinformat.

Ifthecharactersreaddonotmatchthatlayout(forexample,June26,2014,whentheinformatspecifiedisMMDDYY.)oriftheinformatwouldyieldanimpossiblevalue(forexample,February

Page 650: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

31),SASwillsetthevalueofthevariabledate1tomissingandsetthesystemvariable_ERROR_to1.Ingeneral,youshouldknowthelayoutofthecharactersbeforeselectinganinformat.Beginningwithversion9ofSAS,ifyoudonotknowthelayoutofthedatesaheadoftime,

Page 651: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theANYDTfamilyofinformats(seeSection3.4.4)aredesignedtosolvethisproblem.

3.3.2InformatswiththeINPUT()FunctionTheINPUT()functionistheparalleltothe

Page 652: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUT()function,anditstoresanumericorcharactervalueasanumericorcharactervariable.Thetypeoftheresultdependsonthetypeoftheinformatthatisused.Acharacterinformat(onethatbeginswitha$)willreturnacharactervalue.Allinformatsusedwithdates,times,

Page 653: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddatetimesarenumeric.Therefore,thevariablereturnedisnumeric.Thesyntaxisgivenbelow.

INPUT(character-value,informat-name);

IfyouwanttodefinetheinformatthatistobeappliedduringaSASjob(atruntime),

Page 654: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youwillneedtousetheINPUTN(character-value,informat-name)function(orINPUTC(),ifyouwanttoproduceacharactervariable)instead.informat-namerepresentsacharactervariableorcharacterconstantthatcontainsaninformatname,whiletheINPUT()functionneedsan

Page 655: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

actualinformatname.Makesurethatyouhavedefinedthewidthoftheinformatsothatitislongenoughtocaptureallthecharactersintheentirecharactervariable.ThefollowingexamplesillustratetheuseoftheINPUT()andINPUTN()functions:

Page 656: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example3.3:INPUT()Function

DATA_NULL_;datestr="15-NOV-2013";sasdate=INPUT(a,date11.);PUTsasdate=;RUN

TheINPUT()functiontranslatedthedateinthecharactervariabledatestrintoitsequivalentSASdate

Page 657: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

value,19677,andstoreditinthenumericvariablesasdate.Thedate11.informataccountsforthelengthofthecharactervariable.

Example3.4:INPUTN()Function

DATA_NULL_;datestr="15-NOV-2013";inf="DATE11.";sasdate=INPUTN(a,inf);

Page 658: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUTsasdate=;RUN;

TheINPUTN()functionusedthevalueofthecharactervalueinf(DATE11.)astheinformattouseintranslatingthedateinthecharactervariabledatestrintoitsequivalentSASdatevalue,19677.

Page 659: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.3.3WhentheInformatDoesNotMatchtheDataBeingReadInformats,likeformats,areseparatedintoclassesaccordingtothetypeofdatathatarebeingread.Inmostcases,ifyouusethewronginformatforthe

Page 660: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datatype,informatswillreturnanerror(settheSASautomaticvariable_ERROR_to1),andthevalueofthevariablebeingreadwillbesettomissing.

Thisbehaviordiffersfromdate,time,anddatetimeformatsinthatifyouusethewrongtypeofformat

Page 661: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

todisplayavalue(forexample,ifyouuseadateformattodisplayatimevalue),noerrorwilloccur,andatworst,youwillgetawarningintheSASlog.However,incorrectlyspecifyingaformatwillmostlikelycausethedisplaytobeincorrect.Example3.5showswhathappenswhen

Page 662: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youtrytouseaninformatthatdoesnotmatchthecharacterstringthatyouaretryingtoprocess.

Example3.5:UsingtheWrongInformat

DATAbad_informat;INPUT@1date:datetime18.;DATALINES;11-06-19888-25-2004

Page 663: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4-24-2005;;;;RUN;

TheLog1DATAbad_informat;2INPUT@1date:datetime18.;3DATALINES;

NOTE:Invaliddatafordateinline41-18.RULE:----+----1----+----2----+----3----+----4----+----5----+----6----+

Page 664: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

411-06-1988date=._ERROR_=1_N_=1NOTE:Invaliddatafordateinline51-18.58-25-2004date=._ERROR_=1_N_=2NOTE:Invaliddatafordateinline61-18.64-24-2005date=._ERROR_=1_N_=3NOTE:ThedatasetWORK.BAD_INFORMAThas3observationsand1variables.NOTE:DATAstatementused(Totalprocesstime):

Page 665: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

realtime0.53secondscputime0.03seconds7;;;;8RUN;

TheResultingSASDataset

Obs date

1 .

2 .

Page 666: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3 .

Asyoucanseeintheaboveexample,usingtheDATETIME.informattoprocessaseriesofcharacterstringsthatdonotrepresentdatetimesproducesanoteinthelog.Italsosetstheautomaticvariable

Page 667: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

_ERROR_to1foreachrecordwhereitencounteredamismatchbetweentheinformatspecifiedandthedatathatitattemptedtoread.TheendresultisthatthevalueofthedatevariableinyouroutputdatasetismissingbecauseSASwasnotabletoprocessthe

Page 668: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

charactersusingthespecifiedinformat.Remembertoalwayscheckyourlogandyourdatasetafterreadingatextfile.

3.4ListingandDiscussionofInformats

Page 669: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Eachdiscussionofaninformatinthissectionwillprovideanexplanationoftheinformat,itswidthspecification,andthetextitisdesignedtoprocess.Eachsubsectionisaccompaniedbyatablethatgivesexamplesofthetextthatistobeprocessed,alongwith

Page 670: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theinformat(anditswidthspecification)andtheresultingSASdate,time,ordatetimevalue.SAScontinuestodevelopformatsandinformats,soitisalwaysagoodideatochecktheonlinedocumentationatsupport.sas.com.

Page 671: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.4.1DateInformats

DATEw.DATEw.readsdatesintheformddmonyy(yy),whereddrepresentsthedayofthemonth,monisthethree-lettermonthabbreviation,andyy(yy)isthetwo-orfour-digityear.The

Page 672: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultvalueofwis7,butyoushouldspecify9ifyouarereadingfour-digityears.dd,mon,andyy(yy)canbeseparatedbyblanksorspecialcharacters.Ifyouseparatethem,youmustaccountfortheblanks(orspecialcharacters)inthewidthspecification.Ifyouhaveblanksafter

Page 673: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonthandtheday,thenyouneedtohaveawidthof9fortwo-digityearsor11forfour-digityears.Iftheleadingzeroforddismissing,ithasnoeffectonthevalue.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorresponds

Page 674: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tothetextshownineachline.

CharactersRead Informat

20sep15 DATE7.

Page 675: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4feb2014 DATE11.

07-may-1960

DATE11.

DDMMYYw.

Page 676: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYYw.readsdatesoftheformddmmyy(yy),whereddrepresentsthedayofthemonth,mmrepresentsthenumberofthemonth,andyy(yy)isthetwo-orfour-digityear.Thedefaultvalueofwis6,butyoushouldspecify8ifyouarereadingfour-digityears.dd,

Page 677: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mm,andyy(yy)canbeseparatedbyblanksorspecialcharacters.Ifyouseparatethem,youmustaccountfortheseparatingcharactersinthewidthspecification.Ifyouhaveblanksafterthemonthandtheday,thenyouneedtohaveawidthof8fortwo-digityearsor10forfour-digityears.SAS

Page 678: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willdoitsbesttodecipherthestringifnoseparatorsareused,butsomedatescannotbeprocessed—forexample,2112008.Withoutplace-holdingzerosorseparators,thereisnowaytoknowwhetherthedateis21January2008or2November2008.Thefollowingtablegives

Page 679: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

examplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

CharactersRead Informat

Page 680: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

140390 DDMMYY6.

06/09/05 DDMMYY8.

22-04-2003

DDMMYY10.

Page 681: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JULIANw.

JULIANw.translatesaJuliandateintheformyy(yy)ddd,withthetwo-orfour-digityearprecedingthezero-filleddayoftheyear.Itisright-justified.wcanbefrom5to32,andthedefaultis5.Ifyou

Page 682: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specify5,theyearportionoftheJuliandateistwodigitslong.Ifyouspecify7ormore,theyearportionisfourdigitslong.Zerosmustfillthespacebetweentheyearanddayvalues:Forexample,thefifthdayoftheyearmustbegivenas"005."Anydateprecedingtheyear

Page 683: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1582ontheGregoriancalendarcannotbereadasaJulianvalue.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

Page 684: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

09284 JULIAN5.

2014005 JULIAN7.

Page 685: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012168 JULIAN7.

MMDDYYw.MMDDYYw.readsdatesoftheformmmddyy(yy),wheremmrepresentsthenumberofthemonth,dd

Page 686: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representsthedayofthemonth,andyy(yy)isthetwo-orfour-digityear.Thedefaultvalueofwis6,butyoushouldspecify8ifyouarereadingfour-digityears.dd,mm,andyy(yy)canbeseparatedbyblanksorspecialcharacters.Ifyouseparatethem,youmustaccountforthe

Page 687: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

blanksinthewidthspecification.Ifyouhaveblanksafterthemonthandtheday,thenyouneedtohaveawidthof8fortwo-digityearsor10forfour-digityears.SASwilldoitsbesttodecipherthestringifnoseparatorsareused,butsomedatescannotbeprocessed—for

Page 688: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

example,1272003.Withoutplace-holdingzerosorseparators,thereisnowaytoknowwhetherthedateisJanuary27,2003,orDecember7,2003.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownin

Page 689: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

eachline.

CharactersRead Informat

041798 MMDDYY6.

Page 690: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1/15/2013 MMDDYY10.

08282015 MMDDYY10.

MONYYw.

MONYYw.readsdates

Page 691: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheformmonyy(yy),wheremonisthethree-lettermonthabbreviation,andyy(yy)isthetwo-orfour-digityear.UsingthisinformatwillsettheSASdatevaluethatcorrespondstothefirstdayofthemonth.Thedefaultvalueofwis5,butyoushouldspecify7ifyouarereading

Page 692: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

four-digityears.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

Characters

Page 693: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Read Informat

JAN15 MONYY5.

dec1920 MONYY7.

aug2020 MONYY7.

Page 694: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PDJULG4.

PDJULG4.readsapackedJuliandateinhexadecimalformatforIBMcomputers.Thewidthspecificationisalways4,becausethe

Page 695: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Juliandateisparsedasfollows:thefour-digitGregorianyeariswritteninthefirsttwobytes,andthethree-digitintegerthatrepresentsthedayoftheyearisinthenextone-and-a-halfbytes.Thelasthalf-bytecontainsallbinary1s,whichindicatesthevalueispositive.There

Page 696: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isnoexamplegivenforthisinformatbecausepacked-decimalJuliandatesyieldnonprintablecharacters.

PDJULIw.PDJULIw.alsoreadsapackedJuliandateinhexadecimalformatforIBMcomputers.It

Page 697: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

differsfromthePDJULG.informatinthatitexpectsthetwodigitsofthecenturyinthefirstbyte,followedbytwodigitsoftheyearinthesecondbyte.Thenextone-and-a-half-bytesstorethethree-digitintegerthatcorrespondstothedayoftheyear,whilethelasthalf-byteisfilled

Page 698: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withhexadecimal1s,representingapositivenumber.Thecenturyandyeararecalculatedbysubtracting1900fromthefour-digitGregorianyear.Onceagain,thereisnoexample,sincepacked-decimalJuliandatesyieldnonprintablecharacters.

Page 699: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDw.YYMMDDw.isusedtoreaddatesoftheformyy(yy)mmdd,whereyy(yy)isthetwo-orfour-digityear,mmrepresentsthenumberofthemonth,andddrepresentsthedayofthemonth.Thedefaultvalueofwis6,butyoushouldspecify8ifyou

Page 700: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

arereadingfour-digityears.yy(yy),mm,andddcanbeseparatedbyblanksorspecialcharacters.Ifyouseparatethem,youmustaccountfortheseparatingcharactersinthewidthspecification.Ifyouhaveblanksafterthemonthandtheday,thenyouneedtohaveawidthof8fortwo-

Page 701: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digityearsor10forfour-digityears.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

Page 702: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

441205 YYMMDD6.

20150517 YYMMDD8.

2014-07- YYMMDD10.

Page 703: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-07-

06

YYMMDD10.

YYMMNw.

YYMMNw.readsdatesoftheformyy(yy)mm,whereyy(yy)isthetwo-orfour-digityear,andmmrepresentsthenumberofthemonth.

Page 704: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thedayisautomaticallysetto1.Thedefaultvalueofwis4,butyoushouldspecify6ifyouarereadingfour-digityears.TheNintheinformatnameisnecessary.Yourdatamustnothaveanyseparatingcharactersbetweenthemonthandtheyear.Thisinformat

Page 705: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willproduceadatevaluethatisequaltothefirstdayofthemonthgiven.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.Notethatline2ofthistabledemonstrateswhat

Page 706: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

happensifyoutrytoreadadatewithatwo-digityearusingthewidthof6.Inthiscase,SASistranslatingthefourdigitsastheyear,sothemonthvalueisconsideredtobemissing.

Page 707: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

1 9905 YYMMN4.

2 9905 YYMMN6.

3 201403 YYMMN6.

Page 708: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4 201610 YYMMN6.

YYQw.YYQw.readsdatesoftheformyy(yy)Qq,whereyy(yy)isthetwo-orfour-digityearfollowedbytheletterQ

Page 709: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andqisanumberfrom1to4,indicatingthequarteroftheyear.Thedatevalueproducedbythisinformatwillcorrespondtothefirstdayofthegivenquarter.Use6forwifyouarereadingfour-digityears,or4ifyouarereadingtwo-digityears.Thedefaultwis6.Thefollowingtable

Page 710: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

givesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

CharactersRead Informat

ResultingSASDateValue

Page 711: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

14Q1 YYQ4.

2010Q3 YYQ6.

2015Q2 YYQ6.

Page 712: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2013Q4 YYQ6.

YYMMDDw.YYMMDDw.isusedtoreaddatesoftheformyy(yy)mmdd,whereyy(yy)isthetwo-orfour-digityear,mm

Page 713: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representsthenumberofthemonth,andddrepresentsthedayofthemonth.Thedefaultvalueofwis6,butyoushouldspecify8ifyouarereadingfour-digityears.yy(yy),mm,andddcanbeseparatedbyblanksorspecialcharacters.Ifyouseparatethem,youmustaccountforthe

Page 714: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

separatingcharactersinthewidthspecification.Ifyouhaveblanksafterthemonthandtheday,thenyouneedtohaveawidthof8fortwo-digityearsor10forfour-digityears.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorresponds

Page 715: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tothetextshownineachline.

CharactersRead Informat

441205 YYMMDD6.

Page 716: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20150517 YYMMDD8.

2014-07-06

YYMMDD10.

YYMMNw.YYMMNw.readsdates

Page 717: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheformyy(yy)mm,whereyy(yy)isthetwo-orfour-digityear,andmmrepresentsthenumberofthemonth.Thedayisautomaticallysetto1.Thedefaultvalueofwis4,butyoushouldspecify6ifyouarereadingfour-digityears.TheNintheinformatnameis

Page 718: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary.Yourdatamustnothaveanyseparatingcharactersbetweenthemonthandtheyear.Thisinformatwillproduceadatevaluethatisequaltothefirstdayofthemonthgiven.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdate

Page 719: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuethatcorrespondstothetextshownineachline.Notethatline2ofthistabledemonstrateswhathappensifyoutrytoreadadatewithatwo-digityearusingthewidthof6.Inthiscase,SASistranslatingthefourdigitsastheyear,sothemonthvalueisconsideredtobe

Page 720: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

missing.

CharactersRead Informat

1 9905 YYMMN4.

Page 721: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2 9905 YYMMN6.

3 201403 YYMMN6.

4 201610 YYMMN6.

YYQw.

Page 722: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQw.readsdatesoftheformyy(yy)Qq,whereyy(yy)isthetwo-orfour-digityearfollowedbytheletterQandqisanumberfrom1to4,indicatingthequarteroftheyear.Thedatevalueproducedbythisinformatwillcorrespondtothefirstdayofthegivenquarter.Use6forwif

Page 723: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youarereadingfour-digityears,or4ifyouarereadingtwo-digityears.Thedefaultwis6.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

Page 724: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

ResultingSASDateValue

14Q1 YYQ4.

2010Q3 YYQ6.

Page 725: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2015Q2 YYQ6.

2013Q4 YYQ6.

TheWEEKInformats

Page 726: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheWEEKinformatsreaddatesintheISO-standardWEEKformat.TherearethreealgorithmsusedtocalculatetheWEEKvaluefromagivendate.TheUalgorithmcalculatesweeksbasedonSundaybeingthefirstdayoftheweekwithoutanyotherrestriction.TheV

Page 727: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

algorithmdefinesthefirstweekoftheyearascontainingbothJanuary4andthefirstThursdayoftheyear.Therefore,ifthefirstMondayoftheyearfallsonJanuary2,3,or4,theprecedingdaysofthecalendaryearareconsideredtobeapartofweek53ofthepreviouscalendaryear.

Page 728: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Itisalsopossibleforcalendardaysattheendofayeartobeconsideredasbeinginthefirstweekofthenextcalendaryear.Finally,theWalgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweekwithoutanyotherrestriction.

Page 729: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheWEEKalgorithmscalculatedatesdifferently,soitiscriticalthatyouusethecorrectalgorithmfortheweekvaluethatyouareconverting.Thismeansthatyouneedtoknowwhichalgorithmwasusedtocreatetheweekvaluebeforeyoutrytoconvertit.Ifyouusea

Page 730: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

differentweekalgorithmfromtheonethatwasusedtocreatetheweekvalue,youwillgetthewrongactualdate.Example3.6demonstratesthis.

Example3.6:TheDifferencebetweentheVariousWEEKAlgorithms(U,V,W)

Date

Page 731: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Weekvalue

FormattedwithWEEKDATEUAlgorithm

1 W12 Sunday,March23,2014

2 13W45 Sunday,

Page 732: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

November10,2013

3 15W5303 Tuesday,January5,2016

4 2014W2104 Wednesday,May28,2014

Page 733: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5 2015-W01-02

Monday,January5,2015

Example3.6demonstratesthedifferencesthatexistnotonlyintheresultingdatebetweenthealgorithms,butalsointhedayoftheweek,

Page 734: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichdependsonwhetherthealgorithmusesSundayorMondayasthefirstdayoftheweek.Rows3and5highlightthedifferencesinthewaythefirstandlastweekofthecalendaryeararehandledbythedifferentalgorithms.Asyoucansee,ifyouusethewrongalgorithm,

Page 735: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notonlycanyouendupwithanunintendeddate,buttheresultingdatemightnotevenbeinthesameyear.

WEEKUw.WEEKUw.isnewasofSASversion9.3.ItisusedtotranslateweekvaluesintoSASdatesusingtheUalgorithm.

Page 736: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheUalgorithmcalculatesweeksbasedonSundaybeingthefirstdayoftheweek,andtheweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroifnecessary.Weekvaluesaredatesintheformyyyy-Wnn-dd,whereyyyyistheoptionalyear(canbe

Page 737: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

twoorfourdigits).Wistheletter"W"for"Week";nnistheweeknumberrangingfrom0to53;andddistheoptionaldayoftheweek.Thedashesareoptionalseparators,buttheyaretheonlyseparatorsthatcanbeusedinISO8601weekvaluesandareusedbetweentheyear,the

Page 738: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weeknumber,andtheday(ifpresent).Thewidthspecification,w,canrangefrom3to200andtellsSASwhattoexpectfromthecharacterstringbeingprocessedaccordingtothefollowingtable.

Width(w)

PatternExpected Example

Page 739: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3–4 Wnn W08

Page 740: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 741: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5–6 yyWnn 14W08

Page 742: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 743: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7–8 yyWnndd 14W0803

Page 744: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9–10 yyyyWnndd 2014W0803

Page 745: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11–200

yyyy-Wnn-dd

2014-W08-03

Page 746: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Specifyinganyvalue

Page 747: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

greaterthan11willhavenoeffectonthedatereturned,althoughtheimpliedcursorplacementcancauseanINPUTstatement(notfunction)toyieldunexpectedresults.

Characters

Page 748: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Read Informat

W12 WEEKU3.

14W40 WEEKU5.

15W2801 WEEKU7.

Page 749: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014W3304 WEEKU9.

2015-W06-02

WEEKU11.

Page 750: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKVw.WEEKVw.isavailableasofSASversion9.3.ItisusedtotranslateweekvaluesintoSASdatesusingtheValgorithm.TheValgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweek,andtheweeknumberis

Page 751: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayedasatwo-digitnumberfrom0to53,withaleadingzeroifnecessary.ThisalgorithmdefinesthefirstweekoftheyearascontainingbothJanuary4andthefirstThursdayoftheyear.Therefore,ifthefirstMondayoftheyearfallsonJanuary2,3,or4,theprecedingdays

Page 752: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthecalendaryearareconsideredtobeapartofweek53ofthepreviouscalendaryear.Weekvaluesaredatesintheformyyyy-Wnn-dd,whereyyyyistheoptionalyear(canbetwoorfourdigits).Wistheletter"W"for"Week";nnistheweeknumberrangingfrom0to53;andddisthe

Page 753: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

optionaldayoftheweek.Thedashesareoptionalseparators,buttheyaretheonlyseparatorsthatcanbeusedinISO8601weekvaluesandareusedbetweentheyear,theweeknumber,andtheday(ifpresent).Thewidthspecification,w,canrangefrom3to200andtellsSASwhat

Page 754: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toexpectfromthecharacterstringbeingprocessedaccordingtothefollowingtable:

Width(w)

PatternExpected Example

3–4 Wnn W08

Page 755: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 756: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 757: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5–6 yyWnn 14W08

Page 758: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7–8 yyWnndd 14W0803

Page 759: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9–10 yyyyWnndd 2014W0803

Page 760: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 761: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11–200

yyyy-Wnn-dd

2014-W08-03

Page 762: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Specifyinganyvaluegreaterthan11willhavenoeffectonthedatereturned,althoughtheimpliedcursorplacementcancausean

Page 763: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INPUTstatement(notfunction)toyieldunexpectedresults.

CharactersRead Informat

W12 WEEKV3.

Page 764: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

14W40 WEEKV5.

15W2801 WEEKV7.

2014W3304 WEEKV9.

Page 765: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2015-W06-02

WEEKV11.

WEEKWw.WEEKWw.isavailableasofSASversion9.3.Itisusedtotranslate

Page 766: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekvaluesintoSASdatesusingtheWalgorithm.TheWalgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweekwithoutanyotherrestriction.Theweeknumberisdisplayedasatwo-digitnumberfrom0to53,withaleadingzeroif

Page 767: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary.Weekvaluesaredatesintheformyyyy-Wnn-dd,whereyyyyistheoptionalyear(canbetwoorfourdigits).Wistheletter"W"for"Week";nnistheweeknumberrangingfrom0to53;andddistheoptionaldayoftheweek.Thedashesareoptionalseparators,buttheyare

Page 768: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theonlyseparatorsthatcanbeusedinISO8601weekvaluesandareusedbetweentheyear,theweeknumber,andtheday(ifpresent).Thewidthspecification,w,canrangefrom3to200andtellsSASwhattoexpectfromthecharacterstringbeingprocessedaccordingto

Page 769: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thefollowingtable:

Width(w)

PatternExpected Example

3–4 Wnn W08

Page 770: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 771: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5–6 yyWnn 14W08

Page 772: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 773: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7–8 yyWnndd 14W0803

Page 774: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9–10 yyyyWnndd 2014W0803

Page 775: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11–200

yyyy-Wnn-dd

2014-W08-03

Page 776: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 777: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Specifyinganyvaluegreaterthan11willhavenoeffectonthedatereturned,althoughtheimpliedcursorplacementcancauseanINPUTstatement(notfunction)toyieldunexpectedresults.

Page 778: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

W12 WEEKW3.

14W40 WEEKW5.

Page 779: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

15W2801 WEEKW7.

2014W3304 WEEKW9.

2015-W06- WEEKW11.

Page 780: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

02

3.4.2TimeInformats

HHMMSSw.HHMMSSw.isavailableasofSASversion9.3.Itwillreadtimevaluesintheformhh:mm:ssor

Page 781: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hhmmss,wherehhishours,mmisminutes,andssisseconds,withanoptionalseparator.Thisinformatwillignorefractionalseconds.Thedefaultwidthforthisformatis8,butwcanrangefrom1to20.Whentherearesixdigitsinthestringbeingread,thefirsttwodigitswill

Page 782: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betranslatedintohours,thesecondtwointominutes,andthelasttwointoseconds.

CharactersRead Informat

143500 HHMMSS6.

Page 783: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

971406 HHMMSS6.

000339 HHMMSS6.

081525 HHMMSS6.

Theparsingandinterpretationperformedbythisinformatchangesiftherearefewerthansixdigitsinthestring

Page 784: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beingreadandisbasedonthefollowingrules:Ifthereisanoddnumberofdigitsinthestringbeingread,SASwilladdazerotothebeginningofthestring,andthefirsttwodigitswillthenbetranslatedashours.SASwillthenaddzerostotheendofthestringuntilithassixdigitsandcanbe

Page 785: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

translatedashours,minutes,seconds.

CharactersRead Informat

1 HHMMSS6.

Page 786: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 787: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 788: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11 HHMMSS6.

Page 789: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 790: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

112 HHMMSS6.

Page 791: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 792: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1127 HHMMSS6.

Page 793: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 794: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

11274 HHMMSS6.

Page 795: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 796: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

112745 HHMMSS6.

Page 797: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 798: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whentherearemorethansixdigitsinthestringthatisbeingread,SASwillparsethestringfromrighttoleft.Theassumptionisthatsincesecondsandminutesnormallycycleat60,theywillonlyeverrequiretwodigits.Itwilltranslatethelast

Page 799: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fourdigitsofthestringasminutesandseconds.Alldigitstotheleftofthefinalfourdigitswillbetranslatedintohours.

CharactersRead Informat

Page 800: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

172154 HHMMSS10.

1721543 HHMMSS10.

Page 801: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 802: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

17215430 HHMMSS10.

Page 803: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

172154305 HHMMSS10.

1721543058 HHMMSS10.

Page 804: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MSEC8.

MSEC8.readsIBMmainframetimevaluesaccuratetothenearestmillisecond.Thewidthis8becausetheOSTIMEmacroandSTCKsysteminstructionsstoretheirtimevaluesin8bytes.

Page 805: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PDTIME4.PDTIME4.convertspacked-decimaltimevaluescontainedinSMFandRMFrecordsproducedbyIBMmainframesystemstoSAStimevalues.Thewidthisshownas4becauseSMFandRMFrecordsare4byteslong.Whilethe

Page 806: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informatRMFSTAMP8.alsoreadspackeddecimalRMFrecords,RMFSTAMP8.readsboththe4bytesoftimeand4bytesofdateinformationcontainedintheRMFrecordandcreatesaSASdatetimevaluefromit.PDTIME4.onlyreadsthe4bytesoftimeinformationfrom

Page 807: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anRMFrecordandcreatesaSAStimevalue.

RMFDUR4.RMFDUR4.convertsIBMmainframeRMFdurationrecordsintoSAStimevalues.Thewidthisshownas4becauseRMFrecordsare4-byte-longpacked

Page 808: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hexadecimalrecords.

STIMERw.STIMERw.readstimesproducedbytheSTIMERSystemoptionintheSASlog.Thisinformathasnodefaultwidth.Itreadstimesandinterpretsthembasedoncolonsanddecimalpoints.Ifthere

Page 809: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isonecolon,thefirsttwodigitsareminutesandthelasttwoareseconds.Iftherearetwocolons,thedigitsprecedingthefirstcolonarehours,thenextsetoftwodigitsisminutes,andthelasttwoareseconds.Ifthereisadecimalpoint,thevaluefollowingthedecimal

Page 810: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

pointistranslatedasadecimalfractionofseconds.Itcanreadtimevaluesinthefollowingformats,wherehhcorrespondstohours,mmcorrespondstominutes,sscorrespondstoseconds,andffcorrespondstodecimalfractionsofseconds.

Page 811: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ssss.ffmm:ssmm:ss.ffhh:mm:sshh:mm:ss.ff

CharactersRead Informat

33 STIMER2.

Page 812: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

51.60 STIMER5.

Page 813: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

14:05 STIMER5.

3:11.03 STIMER7.

Page 814: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1:19:21 STIMER7.

Page 815: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1:46:17.74 STIMER11.

Page 816: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEw.

TIMEw.willreadtimesintheformhh:mm:ss.ff,wherehhindicatesthehours,mmisminutes,andssisthenumberofseconds.Theymustbeseparatedbyaspecialcharacter,suchasacolon(:),period(.),or

Page 817: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hyphen(-).ffindicatesdecimalfractionsofsecondsandmustbeseparatedfromthesecondsbyadecimalpoint(.).Bothsecondsandtheirdecimalfractionsareassumedtobezeroiftheyarenotpresent.Thisinformatcanreada.m.andp.m.timevalues.Ifhhisgreaterthan24,

Page 818: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

and/ormmandssaregreaterthan60,thetimevaluereadwillgivethecorrectnumberofseconds,evenifitisgreaterthan86399.99(thenumberofsecondsinaday).Thisinformatwillparsethetimestringbeingreadashours,minutes,secondsfromlefttoright.Therefore,

Page 819: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ifyouareattemptingtoreadonlyminutesandseconds,youmusthaveleadingzerosforthehours,andvaluesforbothminutesandseconds.Otherwise,yourvaluewillbetranslatedashours:minutes,notminutes:seconds.wrangesfrom5to32withadefaultof8.

Page 820: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

1 124:46 TIME6.

2 14:11:03.3 TIME10.

3 08-15 TIME5.

4 00:10 TIME5.

5 00:10:42 TIME8.

Page 821: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6 10.42 TIME10.

7 00:10:42.5 TIME10.

TODSTAMP8.

TODSTAMP8.converts

Page 822: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aneight-bytetime-of-daystampproducedbytheOSTIMEmacroortheSTCKinstructiononIBMmainframesintoaSAStimevalue.Thewidthis8becausethesecallsreturneight-bytetime-of-dayvalues.UsethisinformatwhenyouarereadingIBMmainframetime-of-dayvalueson

Page 823: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

otheroperatingsystems.

TU4.TU4.convertsIBMmainframetimerunits(38,400timerunitspersecond)toSAStimevalues.ItisusedwhenreadingIBMmainframetimerunitvaluesunderotheroperating

Page 824: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

systems.Thewidthis4becausetheOSTIMEmacroreturnsafour-byteword.

3.4.3DatetimeInformats

B8601CIw.dB8601CIw.disavailableasofSASversion9.3.Itreads

Page 825: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IBMtimevalueswithacenturymarkeroftheformcyymmddhhmmss<fff>,wherecrepresentsthecenturydigit.Thecenturydigitiscalculatedbysubtracting1900fromthecurrentyear,dividingby100,anddroppingtheremainder.yyisthe

Page 826: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

two-digityearfrom00to99,themmrepresentsthenumberofthemonth,andddrepresentsthedayofthemonth.Thetimeisrepresentedbyhhmmss<fff>,wherehhindicatesthehours,mmisminutes,ssisthenumberofseconds,andfffindicatesthousandthsofseconds.

Page 827: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wrangesfrom10to26,withadefaultvalueof16,whiledrangesfrom0to6forthefractionalpartofseconds.

CharactersRead Informat

21504231905 B8601CI16.

Page 828: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1560928053505 B8601CI16.

2140630102416454 B8601CI19.3

2131216094500 B8601CI16.

B8601DJw.dB8601DJw.disavailableasofSASversion9.3.Itreadsdatetimesinstandard

Page 829: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Javadateandtimenotation.yyyymmddhhmmss<ffffff>whereyyyyisthefour-digityear,mmrepresentsthenumberofthemonth,ddrepresentsthedayofthemonth,andtimeisrepresentedbyhhmmss<ffffff>,wherehhindicatesthehours,mmisminutes,ssisthe

Page 830: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numberofseconds,andffffffindicatesmillionthsofseconds.wrangesfrom10to26,withadefaultvalueof16,whiledrangesfrom0to6forthefractionalpartofseconds.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorresponds

Page 831: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tothetextshownineachline.

CharactersRead

201607181108

20141123054509

201303070814433064

201406241630254

Page 832: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATETIMEw.d

DATETIMEw.dreadsSASdatetimevalues.Thedatetimevaluemustbeintheformddmonyy(yy),followedbyablankoraspecialcharacter,andthenthetimeintheformathh:mm:ss.ff.ddrepresentsthedayof

Page 833: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonth,monisthethree-lettermonthabbreviation,andyy(yy)isthetwo-orfour-digityear.hhindicatesthenumberofhours,mmisthenumberofminutes,andssisthenumberofseconds.ffindicatesfractionalpartsofseconds.Bothsecondsandfractionalseconds

Page 834: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

areassumedtobezeroiftheyarenotpresent.wcanbefrom13to40,withadefaultof18,whiledcanrangefrom0to6.

Ifyouuseatwo-digityear,SASwillapplytheYEARCUTOFF=systemoptionintranslatingtheyear.Thisinformatcanalso

Page 835: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

reada.m.andp.m.timevalues.

CharactersRead

22APR20145:23PM

22APR2014-17:23

22APR2014:05:23:15

Page 836: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PM

22APR2014/17:23:15.6

MDYAMPMw.dMDYAMPMw.dreadsdatetimevaluesintheformofmm-dd-yy(yy)hh:mm:ss.ffAM|PM,wheremmrepresentsthenumberofthemonth,ddrepresents

Page 837: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedayofthemonth,andyy(yy)isthetwo-orfour-digityear,followedbythetimewherehhrepresentshours,mmrepresentsminutes,ssrepresentsoptionalseconds,ffrepresentsoptionalfractionalseconds,andAM|PMindicatesa.m.orp.m.Theseparatorsarenotoptionaland

Page 838: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canbeahyphen(-),orperiod(.),orslash(/),orcolon(:).Thedefaultvalueofwis19andrangesfrom8to40,whiledrangesfrom0to39forthefractionalpartofseconds.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorresponds

Page 839: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tothetextshownineachline.Line4showsthatthisformatcantranslatetwenty-four-hourclockvalueswithouttheAM|PMindicator.

CharactersRead Informat

Page 840: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1 05-08-20159:33AM

MDYAMPM18.

2 10-26-20145:00PM

MDYAMPM18.

3 04-25-20131:00:57.4

MDYAMPM22.3

Page 841: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PM

4 08-09-201714:15

MDYAMPM17.

RMFSTAMP8.

RMFSTAMP8.convertsIBMmainframeRMF

Page 842: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateandtimerecordsintoSASdatetimevalues.Thewidthisshownas8becauseRMFrecordsarepacked-decimalrecordswith4bytesoftimeinformationfollowedby4bytesofdateinformation.WhilePDTIME4.alsoreadsRMFrecords,itonlyreadsthetimeportion

Page 843: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheRMFrecordandproducesaSAStimevalue.RMFSTAMP8.readsboththedateandtimeportionsoftheRMFrecordandproducesaSASdatetimevalue.

SHRSTAMP8.SHRSTAMP8.convertsIBMmainframeSHR

Page 844: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateandtimerecordsintoSASdatetimevalues.Thewidthisshownas8becauseSHRrecordsarepacked-decimalrecordswith4bytesofdateinformationfollowedby4bytesoftimeinformation.

SMFSTAMP8.

Page 845: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SMFSTAMP8.convertsIBMmainframeSMFrecordsintoSASdatetimevalues.Thewidthisshownas8becauseSMFrecordsarepacked-decimalrecordswith4bytesoftimeinformationfollowedby4bytesofdateinformation.ThisenablesyoutoreadSMFrecordswithout

Page 846: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

regardtooperatingsystem.

YMDDTTMw.dYMDDTTMw.dreadsdatetimevaluesintheform<yy>yy-mm-ddhh:mm:ss.ff,wherespecialcharacterssuchasahyphen(-),period(.),slash(/),orcolon(:)areusedtoseparate

Page 847: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theyear,month,day,hour,minute,andseconds.Separatorsarerequiredforeachcomponent.Theyearcanbeeithertwoorfourdigits.yy(yy)isthetwo-orfour-digityear,mmrepresentsthenumberofthemonth,andddrepresentsthedayofthemonth.Thetimefollows,wherehh

Page 848: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representshours,mmrepresentsminutes,ssrepresentsoptionalseconds,andffrepresentsoptionalfractionalseconds,whichmustbeseparatedfromthesecondsbyadecimalpoint(.).Thisinformatwilltranslatea.m.andp.m.timevalues.Thedefaultvalueofwis19

Page 849: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andcanrangefrom13to40,whiledrangesfrom0to39forthefractionalpartofseconds.

CharactersRead

2014-07-18-11-08-05

Page 850: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014/01/11:05:19:45

1983.02.18.05:59PM

2015-08-23-14:24:16.5

3.4.4The"ANYDATE"Seriesof

Page 851: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

InformatsSAS9addressedanissuewiththeprocessingofdatesandtimesthathasalwaysaffectedSASusers.AlthoughinformatshandlethetranslationofastringofcharactersintoSASdateandtimevalueseasily,inordertousethemyouhadto

Page 852: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

knowwhatthestringofcharacterslookedlikebeforeyouprocessedthem.Giventhemanywaysthatdatesandtimescanberepresented,itwasnotuncommonforseveralrecordstohaveincorrectvaluesafterprocessingbecauseofanerrorintheunderlyingtextstrings

Page 853: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beingtranslated.Forexample,ifyouexpectthedatestobeintheform"ddMOMyyyy,"buthalfwaythroughthefilethestringswereenteredas"mmddyyyy,"atleasthalfofyourresultingdatasetwillhavemissingdatevalues.TheDATE.informatcannotreadstrings

Page 854: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formattedtobereadwiththeMMDDYY.informat,andviceversa.Therearenowthreeinformatsthatwillintelligentlyand,forthemostpart,successfullyenableyoutoavoidthisproblem.

TheANYDTDTE.,ANYDTDTM.,andANYDTTME.informats

Page 855: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willtranslatedates,datetimevalues,andtimevalues,respectively,intotheircorrespondingSASvalues.Thistranslationwillbeperformedwithouthavingtoknowtherepresentationofthesedate,datetime,andtimevaluesinadvance.Somelimitsexistasto

Page 856: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thetypesofrepresentationstheseinformatswillbeabletotranslate,andinthiseraofbigdatayoushouldalsobeawarethatusingtheseinformatswillrequiremoreCPUtimethanifyouareabletouseoneoftheregularinformatstoprocessyourdata.

Page 857: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThepotentialforconfusionexistswithDDMMYY,MMDDYY,andYYMMDDvalues,especiallyinthepresenceoftwo-digityearvalues.TheSASoptionDATESTYLEindicateshowsuchconfusionswillberesolved.ThepossiblevaluesfortheDATESTYLE=system

Page 858: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

optionareshowninthefollowingtable.

Value Explanations

MDY Setsthedefaultorderasmonth,day,year."10-03-12"wouldbe

Page 859: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

translatedasOctober3,2012.

YMD Setsthedefaultorderasyear,month,day."10-03-12"wouldbe

Page 860: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

translatedasMarch12,2010.

DMY Setsthedefaultorderasday,month,year."10-03-12"wouldbe

Page 861: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

translatedasMarch10,2012.

LOCALE(default)

SetsthedefaultvalueaccordingtotheLOCALE=system

Page 862: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option.WhenthedefaultvaluefortheLOCALE=systemoptionis"English_US,"thissets

Page 863: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATESTYLEtoMDY.Therefore,bydefault,"10-03-12"wouldbetranslatedasOctober3,2012.

Page 864: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Note:YDM,MYD,andDYMhavebeenremovedfromSAS9.3andlaterversions.Ifyoucontinuetousetheminlegacycode,noerrorwillappearinthelog,butanyinputstringsthatcannotbetranslatedwithoutknowingtheDATESTYLE(forexample,"10-03-12")

Page 865: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willbetranslatedasmissing.

WhathappenswhentheinputstringcannotbetranslatedusingtheDATESTYLEoptionineffect?TheANYDTseriesofinformatswilltesttheinputstringtoseewhetheroneoftheotherDATESTYLEoptionswillwork.If

Page 866: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

onlyoneDATESTYLEproducesavalidSASdateordatetimevalue,thenSASwillprocesstheinputstringusingthatDATESTYLE.However,itisentirelypossiblethattheinputstringcanbetranslatedusingmorethanoneoftheremainingDATESTYLEoptions.Forexample,theinput

Page 867: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

string"270328"cannotbetranslatedusingMDY,butbothYMDandDMYwillproduceavalidSASdate.SAShasaninternalpriorityofwhichDATESTYLEtakesprecedencewhentheoptionineffectwillnotwork,andtwoormoreDATESTYLESwill.ItisbasedontheDATESTYLEoptionin

Page 868: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

effect.ThefollowingtabledetailshowtheDATESTYLEischoseniftheDATESTYLEoptionineffectdoesnotyieldavalidSASdatevalue.

DATESYTLEOptioninEffect

InputString

DATESTYLEsPossible

Page 869: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MDY 170514

YMD 110845

DMY Noambiguityispossible.DMYwillalwaystakeprecedence.

Page 870: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Inshort,whentheDATESTYLEisMDY,thenanyambiguitywillberesolvedusingYMD,andwhentheDATESTYLEisYMD,anyambiguityisresolvedusingDMY.

ANYDTDTEw.ANYDTDTEw.willtranslatedatathatcan

Page 871: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

bereadwiththefollowinginformats:DATE,DATETIME,DDMMYY,JULIAN,MDYAMPM,MMDDYY,MMxYY,MONYY,TIME,YMDDTTM,YYMMDD,YYMMn,orYYQintoSASdatevalues.Thisinformatcanalsoworkwiththefollowingstandarddateformats:"February25,

Page 872: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012,""08/2014,"or"2015-05."wcanrangefrom5to32,andthedefaultwidthis9.Thisinformatextractsdatevaluesfromadatetimestring.However,ifonlyatimevalueisgiven,thedateisassumedtobeJanuary1,1960.ThefollowingtableusesOPTIONSDATESTYLE=MDYas

Page 873: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedefaultinterpretationfortwo-digityearvalues.

CharactersRead

05172014

Page 874: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20140517

2014Q2

051714

17052014

Page 875: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

170514

Page 876: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

17MAY2014:15:12:06

15:12:06

Page 877: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014137

MAY2014

17MAY2014

Page 878: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

May17,2014

14-05

Page 879: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-05

05-2014

Page 880: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

05-17-20143:12:06PM

2014-05-17-15:12:06

ANYDTDTMw.

ANYDTDTMw.willtranslatedatathatcan

Page 881: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

bereadwiththefollowinginformats:DATE,DATETIME,DDMMYY,JULIAN,MDYAMPM,MMDDYY,MMxYY,MONYY,TIME,YMDDTTM,YYMMDD,YYMMn,orYYQintoSASdatetimevalues.Thisinformatcanalsoworkwiththefollowingstandarddateformats:"February25,

Page 882: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012,""08/2014,"or"2015-05."wcanrangefrom1to32,andthedefaultwidthis19.

Thisinformatwillparsetimevaluesfrominputstringsbasedoncolonsandperiods.Ifthereisonecolon(forexample,15:12),thefirsttwodigitsaretranslatedashoursand

Page 883: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thelasttwoasminutes.Iftherearetwocolons(forexample,15:12:06),thedigitsprecedingthefirstcolonaretranslatedashours,thenextsetoftwodigitsasminutes,andthelasttwoasseconds.Ifthereisasingledecimalpointandoneormorecolons(forexample,12:06.5),

Page 884: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thevaluefollowingthedecimalpointistranslatedasadecimalfractionofseconds,andthevalueprecedingthedecimalpointisconsideredtobeseconds.Asinglecolonisthereforeinterpretedasminutes:seconds.fractionalseconds.Inordertoaccountforhours,the

Page 885: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correctformatwouldbehours:minutes:seconds.fractionalseconds.

Iftherearemultipledecimalpoints(forexample,15.12.06),thentheyareconsideredtobedelimitersfordatevaluesandthusarenottranslatedastime

Page 886: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

values.

Thisinformatextractsdatetimevaluesfromastring.Ifonlyatimevalueisgiven,thedateisassumedtobeJanuary1,1960.Ifonlyadatevalueisgiven,thetimeisassumedtobemidnight(0:00).Thefollowingtableusesthesameinputdataasis

Page 887: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usedintheANYDTDTE.informatexampleabove.

CharactersRead

05172014

20140517

Page 888: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014Q2

051714

17052014

170514

17MAY2014:15:12:06

15:12:06

2014137

Page 889: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MAY2014

17MAY2014

May17,2014

2014-05

05-2014

14-05

05-17-20143:12:06

Page 890: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PM

2014-05-17-15:12:06

ANYDTTMEw.

ANYDTTMEw.willtranslatedatathatcanbereadwiththefollowinginformats:DATE,DATETIME,DDMMYY,JULIAN,

Page 891: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MDYAMPM,MMDDYY,MMxYY,MONYY,TIME,YMDDTTM,YYMMDD,YYMMn,orYYQintoSAStimevalues.Thisinformatcanalsoworkwiththefollowingstandarddateformats:"February25,2012,""08/2014,"or"2015-05."wcanrangefrom1to32,andthedefaultwidthis8.

Page 892: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ANYDTTMEw.canextracttimevaluesfromadatetimevalue.However,ifonlyadatevalueisgiven,thetimeisassumedtobe12:00a.m.

Characters

Page 893: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Read

17MAY2014:15:12:06

15:12:06

05-17-20143:12:06PM

Page 894: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-05-17-15:12:06

08.05

Page 895: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

08.05

08:05

08:05:05

17:15

2:45PM

Page 896: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2:45PM

6AM

6:00AM

27:36:58

Page 897: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3.4.5SoWhyNotJustUsethe"ANYDATE"SeriesofInformats?ItistemptingtoautomaticallyuseANYDTDTE.,ANYDTTM.,orANYDTTME.toprocess

Page 898: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringsrepresentingdates,datetimes,andtimes.Youdonothavetoworryabouttheformattingoftheinputstring,andSASwillmakesenseoutofit.The"ANYDATE"informatsdohavelimitations,andtherulesthatSASfollowsmightnotbetherulesthatyouneedtoapply.

Page 899: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

First,thereisthematteroftheadditionalprocessingneeded.Theseinformatsgothroughadecisiontreetodeterminehowtotranslateeverysinglevalueencountered;therefore,theamountofadditionalprocessingwillincreasewiththenumberoftimeseach"ANYDATE"informat

Page 900: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hastobeused.Thiswouldhaveanegligibleimpactonasmallamountofdata,butifyouneedtousethemonbigdata,youmightwanttoconsiderstandardizingtherepresentationofyourdate,time,anddatetimetextvaluesbeforehandandusingthecorresponding

Page 901: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informat.Second,itisentirelypossiblethatyouwouldwanttoconsideranonstandardvalueerroneousanddon'twantSAStodecidewhattodowithitwithoutyoubeingabletoinspectitfirst.Third,exceptionscanoccur,evenwhentheuseofanANYDATEinformatiswarranted.

Page 902: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WhileitisalwaysagoodideatocheckalldatathatyouareconvertingtoSASfromanothersourceandespeciallywhenyouareconvertingdates,times,anddatetimevalues,itiscriticalifyouareusingtheANYDT.informats.

Finally,itisimportant

Page 903: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tonotethattheANYDATEseriesofformatsaredesignedtohandletheissueofvaryingwaysofrepresentingdates,butthey,likeallotherdate,time,anddatetimeinformats,arenotdesignedtohandledirtydata.Missingseparators,misspelledwords,andmissing

Page 904: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datecomponentsareafewofthemajordataproblemsthatareencounteredwhenprocessingdates,times,anddatetimesfromsourcesotherthanSAS.Unlessyouspecificallyprovidecodetofixthoseproblemsontheflyorcanensurethatyourdatesarecleanbeforeprocessingthem,

Page 905: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youwillwindupwithmissingdate,time,ordatetimevaluesinyourdataset.SASprogrammershavewrittenagreatdealofcodetohandlethisspecificissue;KingandFlemingshowonesuchapproachinthe2011SASGlobalForumproceedings(seehttp://support.sas.com/resources/papers/proceedings11/117-

Page 906: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2011.pdf).Example3.7isaskeletonofanapproachusingauser-writtenfunctionandtheANYDATE.informat.

Example3.7:ASimpleFunctiontoHandleDateswithMissingSeparators

LIBNAMEcontrol"c:\book\2ndEd\examples";ODSESCAPECHAR='~';OPTIONSCMPLIB=

Page 907: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(control.functions);

PROCFCMPOUTLIB=control.functions.dates;

FUNCTIONmakedate(cdate$)$;LENGTHdtstr$42supr$16;supr='';*assumethatdateisinMMDDYYform,butcheck;*missingdatesoftenhaveaformof__/__/____;IFSUBSTR(cdate,7,4)

Page 908: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NOTIN('','____')THENDO;*assumeyearisgood;yy=INPUT(SUBSTR(cdate,7,4),4.);m=SUBSTR(cdate,1,2);IF'01'<=m<='12'THENDO;mm=INPUT(m,2.);*Monthisgoodchkday;*UseINTNXtofindthemaximumnumberofdaysincurrentmonth;d=SUBSTR(cdate,4,2);IF'01'<=d<=PUT(DAY(INTNX('MONTH',MDY(mm,1,yy),0,'e')

Page 909: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

),z2.)THENdd=INPUT(d,2.);ELSEDO;*Dayisbadforthismonth;*Fixbydividingnumberofdaysinmonthby2;dd=CEIL(DAY(INTNX('MONTH',mdy(mm,1,yy),0,'e'))/2);supr='~{superDay}';END;END;ELSEDO;*Monthisbad-

Page 910: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

resetmonthandday;mm=6;dd=31;supr='~{superMonth}';END;*Createthedatevalue;date=MDY(mm,dd,yy);dtstr=CATT(PUT(date,MMDDDYY10.),supr);END;ELSEDO;*yrisbad;dtstr='Unknown~{superY}';

Page 911: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

END;RETURN(dtstr);ENDSUB;RUN;QUIT;PROCFORMAT;VALUE$mkdtOTHER=[makedate()];RUN;

ThecodeinExample3.7createdafunctiontobeusedasaformatforourdates,whichwillimputea

Page 912: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

replacementdatestringwhenprovidedwithadatestringthatmayormaynothavemissingdatecomponents.Notethatthisisacharacterformatbecauseithastotreattheincomingdatedataascharacterstrings.IfyoutrytoreaddatestringswithmissingdatecomponentsasaSAS

Page 913: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date,youwillgetamissingvalue.

Nowlet'sreadinsomesampledata.ThedatasetRPTDATEScontainsproductionerrortrackingdatafromalegacysystem.Unfortunately,someofthedatedatawascorrupted,sotherearemissingandincorrect

Page 914: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

days,months,andyears.

DATArptdates;INPUTrecno@4rptdt$10.errs;DATALINES;113/15/2013374209/08/20133302/30/2013324__/15/201315504/27/2013195605/__/201317707/08/____20806/04/0135

Page 915: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

908/32/201341011//201361101/23/203111;;;;RUN;

Butnowthatwe'vecreatedafunctionthatistobeusedasaformat,let'suseit.Belowisadatasetwithsomedatesandacountofmachinefaultsoccurringonthatdate.

Page 916: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Asyoucansee,thedateshavenotbeenenteredverywell,withmissingcomponents(rows3,5,and6)andtypographicalerrors(rows7and9).We'regoingtousetheformat$MKDTtoimputethedateswecan,andannotateeachdatethatwasimputedintheoutputstring.

Page 917: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Figure3.1:RPTDATESDataSetwithInvalidDates

Page 918: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NowwewilluseaPROCREPORTtodisplaythetableandformatourbaddata.In

Page 919: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

line2,wealiastheoriginaldatestringasasecondcolumninordertoshowitwithandwithouttheformatthatwecreated.Weleavetheoriginaldatestringinthevariablerptdtandinline4weapplytheformatthatwecreatedtothevaluesinthealiasedcolumndt.

Page 920: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.PROCREPORTDATA=rptdatesNOWDSPLIT='\';2.COLUMNrptdtrptdt=dterrs;3.DEFINErptdt/DISPLAY"Original\Datefrom\ErrorLog";4.DEFINEdt/'Imputed\Date'f=$mkdt36.;5.DEFINEerrs/"Errors\Reported";6.RUN;

TheresultofthePROC

Page 921: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

REPORTisbelow.The"ImputedDate"columnisnothingmorethantheresultofdisplayingtheoriginaldatestringusingthe$MKDT.format.

Output

OriginalDatefromErrorLog

ImputedDate

Page 922: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

13/15/2013 06/30/2013

09/08/2013 09/08/2013

02/30/2013 02/14/2013

__/15/2013 06/30/2013

04/27/2013 04/27/2013

05/__/2013 05/16/2013

07/08/____ UnknownY

06/04/013 UnknownY

Page 923: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

08/32/2013 08/16/2013

11//2013 11/15/2013

01/23/2031 UnknownY

ThiscapacitytousePROCFCMPtocreatefunctionsthatprocesscharacterstringsandthenusethefunctionyoucreateinacustominformat(orformat,for

Page 924: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatmatter)givesyoutheabilitytocreatecustomdateinformats.Withalittlecreativityonyourpart,youcanusethismethodtosolvesomeofthemoredifficultdateprocessingchallengesthatyoumayencounter.

Page 925: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter4:ISO8601Dates,Times,Datetimes,Durations,andFunctions

Page 926: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.1WhatIsISO8601?4.2ISO8601Formats4.3ISO8601Informats4.4TimeZoneFunctions4.5ISO8601

Page 927: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DurationsandIntervals4.6Conclusion

4.1WhatIsISO8601?ISO8601isthenameofaninternationallyacceptedwayof

Page 928: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

describingdatesandtimesusingnumbersandisoneofnumerousinternationalstandardsmaintainedbytheInternationalStandardsorganization(ISO).Whileusingnumberseliminatestheneedfortranslatingmonthnames,thestandarddefinestheorderofdateandtime

Page 929: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

componentsinadatestring,removingtheconfusionofwhether01-11-05meansJanuary11,2005,November12005,orNovember5,2001.Thisfacilitatestheexchangeofdata,especiallybetweeninternationalparties.Thecompletestandard(availablefrom

Page 930: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

http://www.iso.org/iso/home.htmlcoversthefollowing:

•Date•Timeofday•Coordinateduniversaltime(UTC)•LocaltimewithoffsettoUTC•Dateandtime•Timeintervals

Page 931: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

•Recurringtimeintervals

ThisstandardhasbeenadoptedbytheClinicalDataInterchangeStandardsConsortium(CDISC)fortherepresentationofitsdateandtimedata,sothoseinthepharmaceuticalindustryare

Page 932: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

particularlyfamiliarwithit.

SAShasbuilt-informatsspecificallydesignedtodisplayitsdate,time,anddatetimevaluesinthewaydescribedbythestandard.Thereisalsoacompletesetofinformatsdesignedtointerpretstringsthat

Page 933: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

conformtothestandardsothattheycaneasilybeconvertedtoSASdate,time,anddatetimevaluesandthereforeusedincalculationsandstoredinSASdatasets.OnemajordifferencebetweentheISOformatsandinformatsandtheirstandardcounterpartsisthatthe

Page 934: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISOstandardallowsformissingcomponents.Forexample,ifyouhaveadatevaluewhereyouonlyknowthemonthandyear,itcanbeexplicitlyrepresentedinanISO-standarddatestring.ThestandardSASdateformatscanonlyrepresentacompleteSASdatevalue.Even

Page 935: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thestandardformatsthatonlydisplaymonthandyearusecompleteSASdatevalues,butthedaysarenotpartofthedisplay.TheISOinformatsarealittlemoreintricatebecauseofthisfeatureofthestandard;someoftheinformatsperformadefaultsubstitutionforany

Page 936: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

missingcomponentsinordertocreateavalidSASdate,time,ordatetimevalue.Bycomparison,thestandardSASdate,time,anddatetimeinformatswillreturnamissingvalueifyoupresentthemwithastringthathasamissingcomponent.Therefore,whileit

Page 937: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mightseemthatsomeoftheformatsandinformatsthatwedescribedinChapters2and3willproduceresultsaccordingtothestandard,itismuchmorereliabletousetheformatsandinformatsdescribedinthischapterwhenyouareworkingwithISOstandarddatesand

Page 938: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

times.

AswithstandardSASformats,itiscriticalthatyouonlyusedateformatsandinformatsfordatevalues,timeformatsandinformatswithtimevalues,anddatetimeformatsandinformatswithdatetimevalues.

Page 939: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.2ISO8601FormatsAlltheISO8601formatsareleft-justified,asopposedtothemajorityofstandardSASdate,time,anddatetimeformats,whichareright-justifiedbecausetheyrepresentnumericvalues.Theformatsin

Page 940: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thefollowingdescriptionsaregroupedbydate,time,anddatetime.Eachformathasabasic(formatnamestartswith"B")andanextended(formatnamestartswith"E")version.TheextendedversionoftheISOformatsusesdelimitersbetweencomponents,whilethe

Page 941: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

basicversionsdonot.Allthefollowingexampleswillpresentthebasicversionoftheformat,followedbytheextendedversionofthesameformatwiththesameSASvalue,soyoucanseethedifference.ItisimportanttonotethatSAScontinuestodevelopformatsandinformats,soitis

Page 942: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

alwaysagoodideatocheckthedocumentationthatcamewithyourreleaseofSAS,ortheonlinedocumentationatsupport.sas.comforanyadditionalformatsand/orinformats.

4.2.1ISODateFormats

Page 943: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DAw.B8601DAw.writesdatevaluesintheISO8601basicdatenotationyyyyMMdd,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,andddisthezero-paddednumericaldayofthemonth.Itisleft-justifiedwithinthefield.wcanbefrom8

Page 944: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

to10,andthedefaultwidthis10.Thefollowingtableshowstheresultwhenthedatevalueis19920,whichcorrespondstoJuly16,2014.

FormatName Result

B8601DA8. 20140716

Page 945: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DA9. 20140716

B8601DA10. 20140716

E8601DAw.

E8601DAw.writesdatevaluesintheISO8601extendeddatenotationyyyy-MM-dd,whereyyyyisthefour-digityear,MMisthezero-

Page 946: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

paddedmonth,andddisthezero-paddednumericaldayofthemonth.Itisleft-justified.wisalways10.Thefollowingtableshowstheresultwhenthedatevalueis19920,whichcorrespondstoJuly16,2014.

Page 947: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result Comment

E8601DA10. 2014-07-16

4.2.2ISOTimeFormats

Page 948: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YoucandeterminewhichoftheISO8601timeformatsyouwanttousebyansweringtwoquestions.First,doestheSAStimevaluethatyouaregoingtodisplayrepresentUTCorlocaltime?IfitrepresentsUTC,thenyouwilluseeitherthe*8601TX.orthe*8601TZ.format,

Page 949: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andyouhavetodeterminewhetheryouwanttodisplayUTCasitslocaltimeequivalent(basedontheTIMEZONE=optionsetting).Inthatcase,youwouldusethe*8601TX.format.IfyouwanttodisplaytheUTCtimevalueasitisstored,usethe*8601TZ.format.Ifthe

Page 950: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAStimevaluerepresentslocaltime,thenusethe*8601LZ.format.TheTIMEZONE=optionhasnoeffectbecausetheformatunderstandsthatthetimeisthelocaltime.

B8601TMw.dB8601TMw.dwrites

Page 951: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timevaluesintheISO8601basictimenotationhhmmssffffff,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds.NotethattherearenodelimitersintheISObasictimestring,whichmight

Page 952: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

maketheresultdifficulttoread.wcanbefrom6to15,withadefaultwidthof8.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis61479.468,whichcorrespondstothetime5:04:39p.m.

Page 953: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(17:04:39.468):

FormatName Result

B8601TM6. 170439

B8601TM8. 170439

B8601TM10.2. 17043947

B8601TM12. 170439

B8601TM15.3. 170439468

Page 954: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TMw.d

E8601TMw.dwritestimevaluesintheISO8601extendedtimenotationhh:mm:ss.ffffff,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds.Colons(:)

Page 955: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimithours,minutes,andseconds,whileadecimalpointdelimitsthedecimalportionofseconds.wcanbefrom8to15,withadefaultwidthof8.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis

Page 956: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

61479.468,whichcorrespondstothetime5:04:39p.m.(17:04:39.468):

FormatName Result

E8601TM6.

E8601TM8. 17:04:39

E8601TM10.2. 17:04:39.5

Page 957: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TM12. 17:04:39

E8601TM15.3. 17:04:39.468

B8601TXw.d

Thisformatisavailable

Page 958: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asofSASversion9.4.B8601TXw.dadjustsaCoordinatedUniversalTime(UTC)valuetotheuser'slocaltime.Sinceitisintendedforclocktimesonly,valuesgreaterthan86400orlessthan0willshowasterisks.TheformatwritestheadjustedlocaltimeintheISO8601basictime

Page 959: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notationhhmmssffffff+|–hhmm,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthe

Page 960: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashhmm,allwithoutdelimiters,asthisisabasicISOformat.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,usingtheTIMEZONE=

Page 961: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

systemoption.IfTIMEZONEisnotset,thentheuserlocaltimeisbasedonthesystemclock.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Youwillneedtoensurethatyouhavealargeenoughwidthspecification,orelsethetimezone

Page 962: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

offsetcouldbetruncatedordroppedcompletely.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis37050,whichcorrespondstothetime10:17:30a.m.GreenwichMeanTime.Asyoucansee,thevaluedisplayedchangeswiththetime

Page 963: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zone.

FormatName Result

B8601TX9. 144730+04

B8601TX12. 171730+0700

B8601TX14. 052500-0500

B8601TX16. 221730+1200

Page 964: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TXw.d

ThisformatisavailableasofSASversion9.4.E8601TXw.dadjustsaCoordinatedUniversalTime(UTC)valuetotheuser'slocaltime.Sinceitisintendedforclocktimesonly,valuesgreaterthan86400orlessthan0willshowasterisks.Theformat

Page 965: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

writesthelocaltimeintheISO8601extendedtimenotationhh:mm:ss.ffffff+/-hh:mm,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffset.The

Page 966: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashh:mm.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,usingtheTIMEZONE=system

Page 967: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option.IfTIMEZONEisnotset,thentheuserlocaltimeisbasedonthesystemclock.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis37050,which

Page 968: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correspondstothetime10:17:30a.m.GreenwichMeanTime.Asyoucansee,thevaluedisplayedchangeswiththetimezone.

FormatName Result

E8601TX9. 12:17:30

Page 969: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 970: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TX. 12:17:30+02:00

E8601TX12. 15:47:30+05

E8601TX14. 18:17:30+08:00

Page 971: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TX16. 12:17:30+02:00

B8601LZw.d

ThisformatisavailableasofSASversion9.4.B8601LZw.ddisplaysSAStimevalueswithoutanyadjustmentsusingtheISO8601basictimenotation

Page 972: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hhmmssffffff+|-hhmm,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffsetfromCoordinatedUniversalTime(UTC).Thetimeoffsetisaplus(fortime

Page 973: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashhmm,allwithoutdelimiters,asthisisabasicISOformat.TheoffsetiscalculatedbasedonthetimezoneatthelocalSASsessionItisnotaffectedbythe

Page 974: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEZONE=systemoption.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.ThemaindifferencebetweenthisformatandtheB8601TX.formatisthattheSAStimevalueisconsideredtobelocal

Page 975: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time,notGreenwichMeanTime.Foreasiercomparison,wewillusethesameSAStimevalueaswiththe*8601TXexamples.Thefollowingtableshowstheresultwhenthetimevalueis37050,whichcorrespondstothetime10:17:30a.m.CentralDaylighttime.

Page 976: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

B8601LZ9. 101730Z

Page 977: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601LZ12. 101730-0500

B8601LZ14. 101730-0500

Page 978: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601LZ16. 101730-0500

E8601LZw.d

ThisformatisavailableasofSASversion9.4.E8601LZw.ddisplaysSAStimevalueswithoutanyadjustmentsusingthe

Page 979: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601extendedtimenotationhh:mm:ss.ffffff+/-hh:mm,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffsetfromCoordinatedUniversal

Page 980: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Time(UTC).Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashh:mm.TheoffsetiscalculatedbasedonthetimezoneatthelocalSASsession.Itisnotaffectedbythe

Page 981: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEZONE=systemoption.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.ThemaindifferencebetweenthisformatandtheE8601TX.formatisthattheSAStimevalueisconsideredtobelocal

Page 982: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time,notGreenwichMeanTime.Foreasiercomparison,wewillusethesameSAStimevalueaswiththe*8601TXexamples.Thefollowingtableshowstheresultwhenthetimevalueis37050,whichcorrespondstothetime10:17:30a.m.CentralDaylighttime.

Page 983: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

E8601LZ9. 10:17:30Z

Page 984: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601LZ12. 10:17:30Z

Page 985: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 986: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601LZ14. 10:17:30-05:00

E8601LZ16. 10:17:30-05:00

B8601TZw.d

ThisformatisavailableasofSASversion9.4.B8601TZw.ddisplaysa

Page 987: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAStimevalueasaCoordinatedUniversalTime(UTC)value.Sinceitisintendedforclocktimesonly,valuesgreaterthan86400orlessthan0willshowasterisks.TheformatwritestheUTCtimeintheISO8601basictimenotationhhmmssffffff+|-hhmmwherehhrepresents

Page 988: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisalwaysgoingtobedisplayedas+0000,becausetheresultingtimedisplayisthetimeatthezero

Page 989: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

meridianinGreenwich,England.ThisformatisnotaffectedbytheTIMEZONE=systemoption.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis37050,

Page 990: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichcorrespondstothetime10:17:30a.m.GreenwichMeanTime.

FormatName Result

B8601TX9. 101730Z

Page 991: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601TX12. 101730+0000

Page 992: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601TX14. 101730+0000

B8601TX16. 101730+0000

E8601TZw.d

ThisformatisavailableasofSASversion9.4.E8601TZw.ddisplaysaSAStimevalueasaCoordinatedUniversal

Page 993: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Time(UTC)value.Sinceitisintendedforclocktimesonly,valuesgreaterthan86400orlessthan0willshowasterisks.TheformatwritestheUTCtimeintheISO8601extendedtimenotationhh:mm:ss.ffffff+/-hh:mm,wherehhrepresentszero-paddedhours,mmrepresents

Page 994: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zero-paddedminutes,ssrepresentsseconds,andffffffrepresentsdecimalfractionofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisalwaysgoingtobedisplayedas+00:00,becausetheresultingtimedisplayisthetimeatthezeromeridianinGreenwich,England.Thisformatis

Page 995: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notaffectedbytheTIMEZONE=systemoption.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableshowstheresultwhenthetimevalueis37050,whichcorrespondstothetime10:17:30a.m.

Page 996: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

GreenwichMeanTime.

FormatName Result

E8601TX9. 10:17:30Z

Page 997: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TX. 10:17:30+00:00

Page 998: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TX12. 10:17:30Z

E8601TX14. 10:17:30+00:00

E8601TX16. 10:17:30+00:00

4.2.3ISO

Page 999: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatetimeFormats

B8601DNw.B8601DNw.writesdatesfromdatetimevaluesintheISO8601basicdatenotationyyyyMMdd,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,andddisthe

Page 1000: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zero-paddednumericaldayofthemonth.Itisleft-justifiedwithinthefield.wcanbefrom8to10,andthedefaultwidthis10.Notethatthebasicdatenotationhasnodelimiters.Thedatetimevalueusedinthisexampleis1664263800,whichcorrespondsto7:30:00a.m.onWednesday,

Page 1001: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

September26,2012.

FormatName Result

B8601DN8. 20120926

B8601DN9. 20120926

B8601DN10. 20120926

E8601DNw.

Page 1002: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DNw.writesdatesfromdatetimevaluesintheISO8601extendeddatenotationyyyy-MM-dd,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,andddisthezero-paddednumericaldayofthemonth.Itisleft-justified.wisalwaysequalto10,andthe

Page 1003: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultwidthis10.Thedatetimevalueusedinthisexampleis1664263800,whichcorrespondsto7:30:00a.m.onWednesday,September26,2012.

FormatName Result Comment

E8601DN10. 2012-

Page 1004: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

09-26

B8601DTw.d

B8601DTw.dwritesdatetimevaluesintheISO8601basicdatetimenotationyyyyMMddThhmmssffffffwhereyyyyisthefour-digityear,MMisthezero-paddedmonth,dd

Page 1005: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds.TistheISO8601delimiterfortime.NotethattherearenootherdelimitersintheISObasic

Page 1006: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimestring,includingthedecimalfractions,whichmightmaketheresultdifficulttoread.wcanbefrom15to26,withadefaultwidthof19.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thedatetimevalueusedinthisexampleis1686408430.44,which

Page 1007: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correspondsto2:47:10.44p.m.onSunday,June9,2013.

FormatName Result

B8601DT15.2. 20130609T144710

Page 1008: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DT19. 20130609T144710

B8601DT19.2. 20130609T14471044

B8601DT24.1. 20130609T1447104

B8601DT26. 20130609T144710

E8601DTw.d

Page 1009: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DTw.dwritesdatetimevaluesintheISO8601extendeddatetimenotationyyyy-MM-ddThh:mm:ss.ffffff,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-padded

Page 1010: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

minutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds.TistheISO8601delimiterfortime.Colons(:)delimithours,minutes,andseconds,whileadecimalpointdelimitsthedecimalportionofseconds.wcanbefrom19to26,withadefaultwidthof19.dcanbe

Page 1011: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

from0to6,withadefaultof0.Thevalueisleft-justified.Thedatetimevalueusedinthisexampleis1686408430.44,whichcorrespondsto2:47:10.44p.m.onSunday,June9,2013.

FormatName Result

E8601DT19. 2013-06-

Page 1012: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

09T14:47:10

E8601DT19.2. 2013-06-09T14:47:10

E8601DT22.1. 2013-06-09T14:47:10.4

Page 1013: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DT24. 2013-06-09T14:47:10

E8601DT24.2. 2013-06-09T14:47:10.44

B8601DXw.d

ThisformatisavailableasofSASversion9.4.B8601DXw.dadjustsa

Page 1014: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CoordinatedUniversalTime(UTC)datetimevaluetotheuser'slocaldateandtime.TheformatwritestheadjustedlocaltimeintheISO8601basicdatetimeandtimezonenotationyyyyMMddThhmmssffffff+|–hhmm,whereyyyyisthefour-digityear,MMisthezero-padded

Page 1015: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.NotethattherearenodelimitersintheISO

Page 1016: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

basicdatetimestringincludingthe+/-ofthetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashhmm,allwithoutdelimiters.Theoffsetiscalculatedfromthe

Page 1017: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeatthezeromeridianinGreenwich,England,usingtheTIMEZONE=systemoption.IfTIMEZONEisnotset,thentheuserlocaltimeisbasedonthesystemclock.wcanbefrom20to35,withadefaultwidthof26.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.

Page 1018: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thefollowingtableusesthevalue1763371185,whichcorrespondsto9:19a.m.onNovember17,2015,GreenwichMeanTime.Asyoucansee,thedisplaydoesnotchangedependingonthewidthspecification,becausetheminimumlengthof20canaccommodatethe

Page 1019: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

entirewidthoftheoutput.

FormatName Result

B8601DX20. 20151117T111945+0200

B8601DX22. 20151117T031945-0600

B8601DX24. 20151117T041945-0500

B8601DX26. 20151117T171945+0800

Page 1020: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DX28. 20151117T194945+1030

E8601DXw.d

ThisformatisavailableasofSASversion9.4.E8601DXw.dadjustsaCoordinatedUniversalTime(UTC)datetimevaluetotheuser'slocaldateandtime.Theformatwritesthe

Page 1021: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

adjustedlocaldatetimeintheISO8601extendeddatetimeandtimezonenotationyyyy-MM-ddThhmmss.ffffff+/-hh:mm,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-padded

Page 1022: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffset

Page 1023: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timerepresentedashh:mm.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,usingtheTIMEZONE=systemoption.IfTIMEZONEisnotset,thentheuserlocaltimeisbasedonthesystemclock.wcanbefrom20to35,withadefaultwidthof26.d

Page 1024: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thefollowingtableusesthevalue1763371185,whichcorrespondsto9:19a.m.onNovember17,2015,GreenwichMeanTime.

Format

Page 1025: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Name Result

E8601DX20. 2015-11-17T02:19:45

E8601DX22. 2015-11-17T05:19:45-04

Page 1026: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1027: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DX26. 2015-11-17T03:19:45-06:00

E8601DX28. 2015-11-17T19:49:45+10:30

Page 1028: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DZw.d

B8601DZw.ddisplaysaSASdatetimevaluebasedonthezeromeridianCoordinatedUniversalTime(UTC)intheISO8601basicdatetimeandtimezonenotationyyyyMMddThhmmssffffff+|–hhmm,whereyyyyisthefour-digityear,MM

Page 1029: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisalways

Page 1030: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

goingtobedisplayedas+0000,becausetheresultingtimedisplayisthetimeatthezeromeridianinGreenwich,England.Therefore,thisformatisnotaffectedbytheTIMEZONE=systemoption.WhiletheSASdocumentationsaysthatwcanbefrom16to35,withadefault

Page 1031: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthof26,youwillgetawarningandamissingvalueifyouuseaformatwidthoflessthan20.dcanbefrom0to6,withadefaultof0.Thevalueisleft-justified.Thedatetimevalueusedinthisexampleis1730398875,whichcorrespondsto6:21:15p.m.onOctober31,

Page 1032: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014,GreenwichMeanTime.

FormatName Result

B8601DZ20. 20141031T182115+0000

Page 1033: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601DZ26. 20141031T182115+0000

E8601DZw.d

E8601DZw.ddisplaysaSASdatetimevaluebasedonthezeromeridianCoordinatedUniversalTime(UTC)intheISO8601extendeddatetimeandtimezonenotation

Page 1034: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yyyy-MM-ddThhmmss.ffffff+/-hh:mm,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimal

Page 1035: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisalwaysgoingtobedisplayedas+00:00,becausetheresultingtimedisplayisthetimeatthezeromeridianinGreenwich,England.Therefore,thisformatisnotaffectedbytheTIMEZONE=system

Page 1036: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option.wcanbefrom20to35,withadefaultwidthof26,andthevalueisleft-justified.dcanbefrom0to6,withadefaultof0.Thedatetimevalueusedinthisexampleis1730398875,whichcorrespondsto6:21:15p.m.onOctober31,2014,GreenwichMeanTime.

Page 1037: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName Result

E8601DZ20. 2014-10-31T18:21:15Z

Page 1038: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DZ22. 2014-10-31T18:21:15Z

Page 1039: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DZ26. 2014-10-31T18:21:15+00:00

E8601DZ28. 2014-10-31T18:21:15+00:00

Page 1040: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.3ISO8601InformatsISO8601informatsaredesignedtoprocessstringsofcharactersthatrepresentISO8601dates,times,anddatetimesand,whenusedwiththeINPUTstatementorINPUT()orINPUTN()functions,willproduceaSAS

Page 1041: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date,time,ordatetimevaluewhenthestringisreadwiththeselectedinformat.

4.3.1ISODateInformats

B8601DAw.B8601DAw.willreaddatevaluesinboththeISO8601basicdate

Page 1042: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notationyyyyMMddandtheISO8601extendeddatenotationyyyy-MM-dd,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,andddisthezero-paddednumericaldayofthemonth.wisalways10,andthedefaultis10.Itisimportanttonotethatifeithermonthor

Page 1043: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dayismissing,SASwilluseavalueof1forthemonthand/ordaytoprovideaSASdatevalue.However,thismightnotbewhatyouwant,asyoursituationmightcallforanalgorithmtoimputedateswithmissingmonthsand/ordaysinstead.

Page 1044: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

20140504 B8601DA.

201405 B8601DA.

Page 1045: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014 B8601DA.

Page 1046: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DAw.

E8601DAw.readsdatevaluesintheISO8601extendeddatenotationyyyy-MM-dd,whereyyyyisthefour-digityear,MMisthezero-

Page 1047: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

paddedmonth,andddisthezero-paddednumericaldayofthemonth.Thisinformatcanonlyhavealengthof10,asthatistheonlylengthforadateinISO8601extendeddatenotation.Unliketheparallelbasicdatenotationinformat,SASwillnotsubstituteavalueof1foreither

Page 1048: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

missingmonthorday,andyouwillgetamissingvalueforyourSASdate.

CharactersRead Informat

Page 1049: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-05- E8601DA10.

2014-05 E8601DA10.

2014 E8601DA10.

Page 1050: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.3.2ISOTimeInformats

B8601TMw.dB8601TMw.dreadstimevaluesintheISO8601basictimenotationhhmmssffffff,

Page 1051: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisdecimalfractionsofseconds.wcanbefrom6to15,with8asthedefault.Usingawof6willreadtimevalueswithnodecimalportion.dcanbefrom0to6,withadefaultof

Page 1052: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

0.

CharactersRead Informat

144535 B8601TM8.

0630 B8601TM8.

Page 1053: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1208455 B8601TM10.1

E8601TMw.d

E8601TMw.dreadstimevaluesintheISO8601extendedtimenotationhh:mm:ss.ffffff,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresents

Page 1054: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

seconds,andffffffisdecimalfractionsofseconds.wcanbefrom8to15,with8asthedefault.Usingawof8willreadtimevalueswithnodecimalportion.dcanbefrom0to6,withadefaultof0.

Page 1055: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

10:17:45 E8601TM8.

18:05 E8601TM8.

Page 1056: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

07:15:12.25 E8601TM12.2

Page 1057: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601TZw.d

B8601TZw.dreadsCoordinatedUniversalTime(UTC)timevaluesusingtheISO8601basictimenotationhhmmssffffff,+|-hhmm,wherehhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentszero-paddedseconds,

Page 1058: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashhmm.Thetimezoneoffsetmightalsobe

Page 1059: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

indicatedbytheletterZ,representingthezeromeridian.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,andtheresultingSAStimevaluewillbethetimeatthezeromeridian.wcanbefrom9to20,withadefaultwidthof14,whiledcanbefrom

Page 1060: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

0to6,withadefaultof0.Sincethisinformatisintendedtoonlyreadclocktimes,valuesresultingintimesgreaterthan24:00:00orlessthan00:00:00willbeadjustedappropriatelysothattheresultwillbewithinthe24-hourclock.

Page 1061: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

175200+0000 B8601TZ14.

175200Z B8601TZ9.

Page 1062: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

091520+0600 B8601TZ14.

210800-0500 B8601TZ14.

Page 1063: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601TZw.d

E8601TZw.dreadsCoordinatedUniversalTime(UTC)timevaluesusingtheISO8601extendedtimenotationhh:mm:ss.ffffff+|-hh:mm,wherehh

Page 1064: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

representszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentszero-paddedseconds,andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestof

Page 1065: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themeridian),followedbytheoffsettimerepresentedashh:mm.ThetimezoneoffsetmightalsobeindicatedbytheletterZ,representingthezeromeridian.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,andtheresultingSAStime

Page 1066: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuewillbethetimeatthezeromeridian.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Sincethisinformatisintendedtoonlyreadclocktimes,valuesresultingintimesgreaterthan24:00:00orlessthan00:00:00willbeadjusted

Page 1067: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

appropriatelysothattheresultwillbewithinthe24-hourclock.

CharactersRead Informat

17:52:00+00:00

17:52:00Z

Page 1068: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

06:00:30.57+08:00

Page 1069: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

04:17:00-05:00

YoumightwonderwhyaUTCvalueof06:00:30.57+08:00

Page 1070: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yieldsatimevalueof22:00:30.57atthezeromedian,andnot14:00:30.57.The+08:00houroffsetmeansthatGMTplus8hourswillgiveyouthelocaltime.Therefore,theconversionfromlocaltimetotimeatthezeromedianislocaltimeminustheoffset.6-8=-2,whichisthen

Page 1071: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

adjustedto22byadding24hours.

E8601LZw.d

E8601LZw.dreadsCoordinatedUniversalTime(UTC)timevaluesusingtheISO8601extendedtimenotationhh:mm:ss.ffffff+|–hh:mm,wherehhrepresentszero-padded

Page 1072: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hours,mmrepresentszero-paddedminutes,ssrepresentszero-paddedseconds,andffffffisdecimalfractionsofseconds,whichisthenfollowedbythetimeoffset.Thetimeoffsetisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followed

Page 1073: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

bytheoffsettimerepresentedashh:mm.ThetimezoneoffsetmightalsobeindicatedbytheletterZ,representingthezeromeridian.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,andtheresultingSAStimevaluewillbethetime

Page 1074: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthelocalSASsession,basedonthesystemclock.ItisnotaffectedbytheTIMEZONE=systemoption.wcanbefrom9to20,withadefaultwidthof14.dcanbefrom0to6,withadefaultof0.Sincethisinformatisintendedtoonlyreadclocktimes,valuesresultingin

Page 1075: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timesgreaterthan24:00:00orlessthan00:00:00willbeadjustedappropriatelysothattheresultwillbewithinthe24-hourclock.

Page 1076: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

17:52:00+00:00 E8601LZ14.

17:52:00Z E8601LZ9.

Page 1077: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

06:00:30.57+08:00 E8601LZ18.2

04:17:00-05:00 E8601LZ14.

Page 1078: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.3.3ISODatetimeInformats

B8601CIw.dB8601CIw.dreadsIBMtimevalueswithacenturymarkeroftheformcyyMMddhhmmss<fff>,wherecrepresentsthe

Page 1079: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

centurydigit.Thecenturydigitiscalculatedbysubtracting1900fromthecurrentyear,dividingby100,anddroppingtheremainder.yyisthetwo-digityearfrom00to99,MMrepresentsthenumberofthemonth,andddrepresentsthedayof

Page 1080: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonth.Thetimeisrepresentedbyhhmmss<fff>,wherehhindicatesthehours,mmisminutes,ssisthenumberofseconds,andfffindicatesthousandthsofseconds.wrangesfrom10to26,withadefaultvalueof16,whiledrangesfrom0to6forthefractionalpartofseconds.

Page 1081: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

However,itisimportanttonotethatthereareonly3placesofdecimalprecision.

CharactersRead Informat

11504231905 B8601CI16.

0560928053505 B8601CI16.

1140630102416454 B8601CI19.3

Page 1082: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2131216094500 B8601CI16.

B8601DJw.dB8601DJw.dreadsdatetimesinstandardJavadateandtimenotationyyyyMMddhhmmssffffff,whereyyyyisthefour-digityear,MMrepresentsthenumber

Page 1083: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthemonth,ddrepresentsthedayofthemonth,andtimeisrepresentedbyhhmmss<ffffff>,wherehhindicatesthehours,mmisminutes,ssisthenumberofseconds,andffffffindicatesmillionthsofseconds.wrangesfrom10to26,withadefaultvalueof16,whiledrangesfrom

Page 1084: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

0to6forthefractionalpartofseconds.ThefollowingtablegivesexamplesofhowtoapplythisinformattoyieldtheSASdatevaluethatcorrespondstothetextshownineachline.

CharactersRead

Page 1085: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

201607181108

20141123054509

201303070814433064

201406241630254

B8601DTw.d

B8601DTw.dreadsdatetimevaluesinthe

Page 1086: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601basicdatetimenotationyyyyMMddThhmmssffffffwhereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffis

Page 1087: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

decimalfractionsofseconds.TistheISO8601delimiterfortime.NotethattherearenootherdelimitersintheISObasicdatetimestring,includingthedecimalfractions.wcanbefrom19to26,withadefaultwidthof19.dcanbefrom0to6,withadefaultof0.Itis

Page 1088: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

importanttonotethatifeithermonthordayismissing,SASwilluseavalueof1forthemonthand/ordaytoprovidethedatefortheSASdatetimevalue,whilesettingthehours,minutes,andsecondstozero.However,thismightnotbewhatyouwant,asyoursituationmightcallforan

Page 1089: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

algorithmtoimputedatetimeswithmissingmonthsand/ordays,and/ortimesinstead.

CharactersRead

20141007T133008745

20150716T0859003315

20140331T1404

Page 1090: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20150903T06

20140804

201312

2016

E8601DTw.d

E8601DTw.dreadsdatetimevaluesinthe

Page 1091: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601extendeddatetimenotationyyyy-MM-ddThh:mm:ss.ffffff,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffis

Page 1092: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

decimalfractionsofseconds.TistheISO8601delimiterfortime.wcanbefrom19to26,withadefaultwidthof19.dcanbefrom0to6,withadefaultof0.Unliketheparallelbasicdatetimeinformat,thereisnodefaultsubstitutionformissingdateand/ortimecomponents,so

Page 1093: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theresultisamissingdatetimevalue.

CharactersRead Informat

2014-10-07T13:30:08

E8601DT19.

Page 1094: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2015-07-16T08:59:00

E8601DT19.

2014-03-31T14:04

E8601DT19.

2015-09-03T06

E8601DT19.

Page 1095: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-08-04 E8601DT19.

2013-12 E8601DT19.

Page 1096: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2016 E8601DT19.

B8601DZw.d

B8601DZw.dreadsa

Page 1097: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASdatetimevaluebasedonthezeromeridianCoordinatedUniversalTime(UTC)intheISO8601basicdatetimeandtimezonenotationyyyyMMddThhmmssffffff+|-hhmm,whereyyyyisthefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericalday

Page 1098: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisthedecimalfractionofseconds.Thisisfollowedbythetimeoffset,whichisaplus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestof

Page 1099: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themeridian),followedbytheoffsettimerepresentedashhmm.ThetimezoneoffsetmightalsobeindicatedbytheletterZ,representingthezeromeridian.TheoffsetiscalculatedfromthetimeatthezeromeridianinGreenwich,England,andtheresultingSASdatetime

Page 1100: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuewillbethedatetimeatthezeromeridian.wcanbefrom20to35,withadefaultwidthof26.dcanbefrom0to6,withadefaultof0.

CharactersRead

Page 1101: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20150208T112705+0500

20150920T05045914-0400

20140511T211700Z

20140511T211700+0000

Page 1102: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601DZw.d

E8601DZw.dreadsaSASdatetimevaluebasedonthezeromeridianCoordinatedUniversalTime(UTC)intheISO8601extendeddatetimeandtimezonenotationyyyy-MM-ddThh:mm:ss.ffffff+|–hh:mm,whereyyyyis

Page 1103: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thefour-digityear,MMisthezero-paddedmonth,ddisthezero-paddednumericaldayofthemonth,hhrepresentszero-paddedhours,mmrepresentszero-paddedminutes,ssrepresentsseconds,andffffffisthedecimalfractionofseconds.Thisisfollowedbythetimeoffset,whichisa

Page 1104: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

plus(fortimezoneseastofthezeromeridian)oraminus(fortimezoneswestofthemeridian),followedbytheoffsettimerepresentedashh:mm.ThetimezoneoffsetmightalsobeindicatedbytheletterZ,representingthezeromeridian.Theoffsetiscalculatedfromthe

Page 1105: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeatthezeromeridianinGreenwich,England,andtheresultingSASdatetimevaluewillbethedatetimeatthezeromeridian.wcanbefrom20to35,withadefaultwidthof26.dcanbefrom0to6,withadefaultof0.

Page 1106: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CharactersRead Informat

2015-02-08T11:27:05+05:00

E8601DZ26.

2015-09-20T05:04:59.14-04:00

E8601DZ29.2

2014-05- E8601DZ26.

Page 1107: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-05-

11T21:17:00Z

E8601DZ26.

2014-05-11T21:17:00+00:00

E8601DZ26.

4.4TimeZoneFunctions

Page 1108: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.4.1IntroductionThereareseveraltimezonefunctionsavailableinSAS.TheyareprovidedaspartofNationalLanguageSupport(NLS),butthesefunctionsarecoveredinthissectionbecausethisiswheretheformatsand

Page 1109: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informatsthatprovidefortimezoneoffsetsandsupportforUTCaredetailed.

Forallofthefollowingfunctions,time-zone-idrepresentsaSAStime-zoneIDvalue.Itisalsoanoptionalargumentinthesefunctions.Ifitisnotprovided,thefunctionwillusethe

Page 1110: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

currentsettingoftheTIMEZONE=systemoptionasthedefault.Beforeusingthesefunctions,youneedtocheckthesettingofyourTIMEZONE=systemoptiontomakesurethatitdoeshaveavalue,ormanyofthesefunctionswillproduceamissingresult.Thisoptioncanbelockedby

Page 1111: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yourSASadministrator.

4.4.2TheTIMEZONE=OptionManyoftheISOtimeanddatetimeformatsusetheTIMEZONE=systemoption,whichisavailableasofSAS

Page 1112: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

version9.4.TheTIMEZONE=systemoptionallowsyoutosetatimezonebasedonageographicallocation.Itisnotsetbydefault.Thisoptionaffectsthefollowing:

•Timesthatarerecordedinlogsandevents.•Creationand

Page 1113: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

modificationtimestampsonSASdatasets•TheDATE(),DATETIME(),TIME(),andTODAY()functions•ThetimezoneformatsB8601DXw.d,E8601DXw.d,B8601LXw.d,

Page 1114: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601LXw.d,B8601TXw.d,E8601TXw.d,NLDATMZw.,NLDATMTZw.,andNLDATMWZw.•ThetimezonefunctionsdescribedinSection4.4.3.

Thevalueoftheoptionmayberepresentedinoneoftwoways:It

Page 1115: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

maybeathree-orfour-letteracronymthatdescribesthetimezone(forexample,EST,forEasternStandardTime),oratime-zoneIDthatspecifiesaregionandanarea,separatedbyaforwardslash(/),suchas"America/New_York."ThetimezoneIDvaluesareuniqueand

Page 1116: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

compatiblewithJavatimezonenames,whilethethree-orfour-letterabbreviationsarenot.Considertheabbreviation“CST”,whichstandsfor"ChinaStandardTime,"or"CubaStandardTime,"or"CentralStandardTime."HowdoesSASchoosewhat"CST"representsinyour

Page 1117: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

program?ItusesthevalueoftheLOCALE=systemoptiontodecidewhatthecorrectregionandareashouldbe.Ofcourse,ifyouarerunningaSASprogramintheUnitedStates,butareusingtheLOCALE=systemoptiontoproduceoutputforanothergeographicalregion,

Page 1118: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thismaycauseincorrecttiming.IwouldrecommendusingthetimezoneIDinsteadoftheacronymsasabestpractice.Thereareover500timezoneIDvalues;consulttheSASdocumentationtofindthetimezoneIDvaluesyouneed.

Page 1119: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.4.3ListofTimeZoneFunctionsNotethatallofthesefunctionsareonlyavailablebeginningwithSASversion9.4.

TZONEID(time-zone-id)TZONEIDwillreturn

Page 1120: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

eitherthecurrenttimezoneIDiftime-zone-idisavalidvalue,orablankwhentime-zone-idismissingorinvalid.Note:PriortoSAS9.4TS1M2,supplyingatime-zone-idforthisfunctionwillcauseanerror.

Sample

Page 1121: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FunctionCall

OPTIONSTIMEZONE=

TZONEID()

TZONEID() Africa/Addis_Ababa

TZONEID() Africa/Brazzaville

TZONEID() America/Montreal

TZONENAME(time-

Page 1122: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zone-id,datetime-value)

TZONENAMEreturnsthecurrenttimezonenamebasedonthetimezoneIDandanydaylightsavingtimerules.Thisfunctionwillreturnablankiftime-zone-idismissing,ortheTIMEZONE=

Page 1123: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

systemoptionhasnovalue.Inthefollowingexample,theTIMEZONEoptionissetto'America/Chicago.'

SampleFunctionCall

TZONENAME()

Page 1124: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONENAME('06JUN2014:08:00'dt)

Page 1125: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1126: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONENAME('06JAN2015:08:00'dt)

Page 1127: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1128: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEOFF(time-zone-id,datetime-value)

Page 1129: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEOFFreturnsthetimezoneoffsetfromCoordinatedUniversalTime(UTC)basedontimezonename,andstandardordaylightsavingtimerules.Iftime-zone-idisblankormissing,theoffsetwillbedeterminedfromthesystemclock.Inthefollowingexample,theTIMEZONEoptionis

Page 1130: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

setto'Europe/Stockholm.'

SampleFunctionCall

TZONEOFF()

Page 1131: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEOFF('06JUN2014:08:00'dt)

Page 1132: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEOFF('06JAN2015:08:00'dt)

Page 1133: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONES2U(datetime-value,time-zone-id)

Page 1134: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONES2UconvertsaSASdatetimevaluetoaCoordinatedUniversalTime(UTC)datetimevaluebasedonthetimezoneanddaylightsavingrules.Iftime-zone-idisblankormissing,theoffsetwillbedeterminedfromthesystemclock.Inthefollowingexample,theTIMEZONEoptionis

Page 1135: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

setto'America/Chicago.'

SampleFunctionCall

TZONES2U('11NOV2014:12:00'dt)

TZONES2U('11NOV2014:12:00'dt,'Pacific/Guam')

TZONES2U('11NOV2014:12:00'dt,'Europe/Istanbul')

TZONEDSTNAME(time-

Page 1136: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zone-id)

TZONEDSTNAMEreturnsthedaylightsavingtimeabbreviationforthetimezoneID.Iftime-zone-idismissing,andtheTIMEZONE=optionisnotset,youwillreceivea"NOTE:Invalidargumenttofunction

Page 1137: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEDSTNAME()"messageinthelog.Ifthetimezoneprovideddoesnotfollowdaylightsavingtime,thentheresultwillbemissing.Inthefollowingexample,theTIMEZONEoptionissetto'America/Chicago.'

Page 1138: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

TZONEDSTNAME()

TZONEDSTNAME('Asia/Calcutta')

Page 1139: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEDSTNAME('America/Sao_Paulo')

TZONEDSTOFF(time-zone-id)

TZONEDSTOFFreturnsthetimezoneoffsetvalueforthespecifieddaylightsavingtime.If

Page 1140: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time-zone-idismissing,andtheTIMEZONE=optionisnotset,youwillreceivea"NOTE:InvalidargumenttofunctionTZONEDSTOFF()"messageinthelog.Inthefollowingexample,theTIMEZONEoptionissetto'America/Chicago.'

Page 1141: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

TZONEDSTOFF()

TZONEDSTOFF('Asia/Calcutta')

Page 1142: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEDSTOFF('America/Sao_Paulo')

TZONESTTNAME(time-zone-id)

TZONESTTNAMEreturnsthestandardtimenameforthetime

Page 1143: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

zoneID.ThisfunctiondiffersfromtheTZONEDSTNAME()functioninthatitprovidesthestandardtimeabbreviationasopposedtothedaylightsavingtimeabbreviation.Iftime-zone-idismissing,andtheTIMEZONE=optionisnotset,youwillreceivea"NOTE:

Page 1144: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

InvalidargumenttofunctionTZONESTTNAME()"messageinthelog.Inthefollowingexample,theTIMEZONEoptionissetto'Pacific/Honolulu.'

SampleFunctionCall

TZONESTTNAME()

Page 1145: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONESTTNAME('Australia/Sydney')

TZONESTTNAME('Asia/Dubai')

TZONESTTOFF(time-zone-id)

TZONESTTOFFreturnsthetimezoneoffsetvalueforthespecifieddaylightsavingtime.Iftime-zone-idismissing,

Page 1146: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtheTIMEZONE=optionisnotset,youwillreceivea"NOTE:InvalidargumenttofunctionTZONESTTOFF()"messageinthelog.Inthefollowingexample,theTIMEZONEoptionissetto'Pacific/Honolulu.'

Page 1147: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

TZONESTTOFF()

TZONESTTOFF('Australia/Sydney')

TZONESTTOFF('Asia/Dubai')

TZONEU2S(UTC-datetime-value,time-zone-id)

Page 1148: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEU2SconvertsaCoordinatedUniversalTime(UTC)datetimevaluetoaSASdatetimevalue.Iftime-zone-idisblankormissing,theoffsetwillbedeterminedfromthesystemclockandaccordingtodaylightsavingtimerulesineffect.Inthefollowingexample,the

Page 1149: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEZONEoptionissetto'America/Chicago.'

SampleFunctionCall

TZONEU2S('11NOV2014:12:00'dt)

TZONEU2S('11NOV2014:12:00'dt,'Pacific/Guam')

TZONEU2S('11NOV2014:12:00'dt,'Europe/Istanbul')

Page 1150: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.5ISO8601DurationsandIntervalsTheISO8601standardalsoprovidesawaytodescribetheintervalbetweentwodatetimes,oraperiodoftimeoverwhichaneventhasoccurred.Anintervalcanbeexpressedby

Page 1151: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usingthestartingandendingdatetimes,oradurationandastartingtime.Durationscanbeexpressedbyprovidingthelengthoftimeinacommonform.However,ISO8601durationsandtimeintervalsdonotdirectlyequatetoasinglepointintime,whileeverythingelse

Page 1152: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intheSASdateandtimefacilityrepresentsonespecificpointintime.Nonetheless,SASgivesyoutheabilitytohandledurationsandintervalssothatyouhaveaccesstoallthefunctionalityoftheSASdateandtimefacility.

UnlikeeveryotherpartoftheSASdateand

Page 1153: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timefacility,ISO8601durationsandintervalsarestoredincharactervariables,buttheyarenotstoredassimpletext.YoucannotconvertadatetimetoacharacterstringusingthePUT()functionandthenconcatenateitwithadurationstringoranotherdatetimethathasbeenconverted

Page 1154: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toacharacterstring.InordertousetheSASdateandtimecapacitywithISOdurationsandintervals,youmustfirstconvertISOdurationandintervalvaluestoaninternalrepresentation.Thisisverymuchthesameprocessthatyouhavetoundertakewhenyouareworkingwithdates

Page 1155: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtimesasweunderstandthem;youmustturnthemintovaluesthatSASunderstandsortakethevaluesthatSASunderstandsandtranslatethemintotherepresentationthatweunderstand.

Therefore,SAShasformatsandinformats

Page 1156: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dedicatedtotranslatingthisinternalrepresentationofISOdurationsandintervals.Theinternalrepresentationcannotbeusedwithoutthistranslation.ThereisalsooneextremelyimportantCALLroutinethatisusedtoeffectthetransformationbetweenISOdurations

Page 1157: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andintervalstoorfromSASdate,time,anddatetimevalues.First,weshouldtalkabouttheformsofISOdurationsandintervals.

4.5.1ISODurationandIntervalRepresentations

Page 1158: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISODurationRepresentationsISOdurationsarerepresentedinthreeways.Thereisnopreferenceorpriorityattachedtoanyoftheforms:Usetheonethatbestfitsyourdata.OneformisPnYnMnDTnHnMnS,wherePistheindicator

Page 1159: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

forperiod,andisprecededbyaminussigniftheperiodrepresentedisnegative(the"starting"dateordatetimecomesafterthe"ending"dateordatetime),Yistheindicatorforyears,Mformonths,andDfordays.TheTistheindicatorfortimeandmustbepresentifthere

Page 1160: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isatimecomponenttothedurationstring.Histheindicatorforhours,theMtotherightoftheTindicatorisforminutes,andtheSistheindicatorforseconds.nrepresentsanumber.Thisformofdurationdescriptionisthesameforboththebasicandextendednotation,asthereare

Page 1161: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

nodelimitersnecessary.

AnotherdurationformisPnW,whichisonlyusedtodescribeadurationmeasuredinweeks.nrepresentsanumber,andtheWistheindicatorforweeks.Aswiththepreviousform,whenthestartingdateordatetimeis

Page 1162: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

afterthe"ending"dateordatetime,thePisprecededbyaminussign.Again,sincetherearenodelimitersinthisform,theoutputusingbasicandextendednotationisidentical.

ThelastofthedurationformsisPyyyyMMddThhmmssfffinbasicnotation,or

Page 1163: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Pyyyy-MM-ddThh:mm:ss.fff,intheextendednotation,whereyyyyisthenumberofyears,MMisthenumberofmonths,ddisnumberofdays,hhrepresentsnumberofhours,mmrepresentsminutes,andssrepresentssecondsintheperiod,withfffasdecimalfractionsof

Page 1164: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

secondsuptothemillisecond.TistheISO8601delimiterfortime.ThisdurationformcanalsoberepresentedasPyyyyMMddorPyyyy-MM-ddifthereisnotimecomponent.Aswiththeotherdurationforms,ifthe"start"islaterthanthe"end,"thePwillbeprecededbya

Page 1165: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

minussign.

ISOIntervalRepresentationsISOintervalsarerepresentedbytwodatetimesthatrepresentthestartandendoftheinterval,oradurationandadatetimethatrepresentseitherthe

Page 1166: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startorendoftheinterval.Thedatetime/datetimeformisyyyyMMddThhmmss/yyyyMMddinthebasicnotation,oryyyy-MM-ddThh:mm:ss/yyyy-MM-ddThh:mm:ssintheextendednotation.Theslashorsolidus(/)betweenthetwodatetimesisarequired

Page 1167: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimiterwhenusingeithernotation.

Inthebasicnotation,theduration/datetimeformis:PnYnMnDTnHnMnS/yyyyMMddwhenthedatetimerepresentstheendoftheinterval,oryyyyMMddThhmmssfff/whenthedatetimevaluerepresentsthe

Page 1168: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginningoftheinterval.Inextendednotation,theformsareyyyy-MM-ddThh:mm:ss.fff/PnYnMorPnYnMnDTnHnMnS/yyyy-MM-ddThh:mm:ss.fff.ThedurationsegmentisspecifiedbyP,theindicatorforperiod,Yistheindicatorforyears,Mformonths,D

Page 1169: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fordays.TheTistheindicatorfortime,andmustbepresentifthereisatimecomponenttothedurationstring.Histheindicatorforhours,theMtotherightoftheTindicatorisforminutes,andtheSistheindicatorforseconds.nrepresentsanumber.

Page 1170: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.5.2ISO8601DurationandIntervalFormatsThefirstthingyoumightnoticeabouttheISOdurationandintervalformatsisthat,unlikeanyoftheotherdate-andtime-relatedformatsinSAS,theyarecharacterformats,

Page 1171: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asindicatedbytheleadingdollarsign($).Thisisreasonable,sincedurationsandintervalsarestoredascharactervalues.Whydoyouneedformatsforcharactervariables?SASstoresISOdurationsandintervalsinaninternalform,sojustasyouneedformatstoconvert

Page 1172: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenSASdatevaluesandhowweunderstanddates,youneedtheseformatstoperformtheconversionfromthestoredformtothecorrespondingISO8601representation.Here'sanexamplethatshowswhatthestoredformofdurationandintervalvalueslookslike,alongwithits

Page 1173: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatteddisplay.TheexampleusestheCALLIS8601_CONVERTroutinetocreatetheinternalrepresentations,andwewilldiscussthisroutine,itssyntax,anduseindetailinsection4.5.4.

Example4.1:WhyFormatsareNecessarywithISO

Page 1174: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Durations,Intervals,andDatetimes

1.DATAisostore;2.SETisotest;3.LENGTHresult1-result2$32;4.CALLIS8601_CONVERT('dt/dt','du',dt1,dt2,result1);5.CALLIS8601_CONVERT("dt/dt",'intvl',dt1,dt2,result2);6.fmt_result1=PUT(result1,$N8601E.);7.fmt_result2=PUT(result2,$N8601E.);8.RUN;

Page 1175: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Theabovecodewilltakethedatetimevaluesdt1anddt2fromadataset.TheCALLIS8601_CONVERTinline4createsanISOdurationvalueinthevariableresult1,whiletheoneinline5createsanISOintervalvalueinthevariableresult2.Thetwo

Page 1176: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

resultsarethenformattedinnewvariablesusingthePUTfunction.Thevaluesinthisdatasetaredisplayedbelow.

Startofperiod(dt1)

04MAR2014:10:23:23

Page 1177: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IntervalValueStoredinDataset(result2)

20143041023230012014C28230403001

Clearly,thestoreddurationandintervalvalues(italicizedintheabovetable)arenotintheISO8601format,soSASprovidesthefollowingformatsto

Page 1178: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

translatethemintothedesiredrepresentation.

$N8601Bw.d$N8601Bw.writesduration,datetime,andintervalvaluesintheISO8601basicnotation.Thisformatdiffersfromthe$N8601BA.formatinthatitdisplays

Page 1179: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

durationsintheformPnYnMnDTnHnMnSinsteadofPyyyymmddThhmmss.Itisleft-justified,andwcanbefrom1to200,withadefaultwidthof50.Inordertodisplaydatetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplayintervalvalues

Page 1180: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correctly,theminimumwidthis32.Inversion9.4,SASwillautomaticallyprovideupto3decimalplacesforthesecondscomponentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbe

Page 1181: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayed).Thenumberofdecimalplacesshownisdependentupontheformatwidth.TheformatwilldisplaythedurationvalueasanISOdurationandanintervalvalueasanISOintervalwithoutanyadditionalprogramming.SASchoosesoneofthefollowingformsbased

Page 1182: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ontheinformationstoredintheISOduration,datetime,orintervalvariable:

•PnYnMnDTnHnMnS•PnYnMnDTnHnMnS/•yyyymmddThhmmssfff•

Page 1183: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yyyymmddThhmmssfff•yyyymmddThhmmssfff

$N8601BAw.d

$N8601BAw.writesduration,datetime,andintervalvaluesintheISO8601basicnotation.Thisformatdiffersfromthe$N8601B.formatin

Page 1184: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatitdisplaysdurationsintheformPyyyymmddThhmmssinsteadofPnYnMnDTnHnMnS.)Itisleft-justified,andwcanbefrom1to200,withadefaultwidthof50.Inordertodisplaydatetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplay

Page 1185: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalvaluescorrectly,theminimumwidthis32.Ifyouattempttodisplayanintervalwithawidthspecificationoflessthan32,youwillgetaseriesofasterisks(*)asyouroutput.Inversion9.4,SASwillautomaticallyprovideupto3decimalplacesfortheseconds

Page 1186: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

componentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbedisplayed).Whetherone,two,orthreedecimalplacesareshownisdependentupontheformatwidth.Theformatwilldisplay

Page 1187: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedurationvalueasanISOdurationandanintervalvalueasanISOintervalwithoutanyadditionalprogramming.SASchoosesoneofthefollowingformsbasedontheinformationstoredintheISOduration,datetime,orintervalvariable:

Page 1188: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

•PyyyymmddThhmmss•yyyymmddThhmmss•PyyyymmddThhmmss/yyyymmdd•yyyymmddThhmmss/P•yyyymmddThhmmss/

$N8601Ew.

Page 1189: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

$N8601Ew.writesduration,datetime,andintervalvaluesintheISO8601extendednotation.Itisleft-justified,andwcanbefrom1to200,withadefaultwidthof50.Inordertodisplaydatetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplay

Page 1190: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalvaluescorrectly,theminimumwidthis32.Ifyouattempttodisplayanintervalwithawidthspecificationoflessthan32,youwillgetaseriesofasterisks(*)asyouroutput.Inversion9.4,SASwillautomaticallyprovideupto3decimalplacesfortheseconds

Page 1191: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

componentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbedisplayed).Whetherone,two,orthreedecimalplacesareshownisdependentupontheformatwidth.Theformatwilldisplay

Page 1192: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thedurationvalueasanISOdurationandintervalvaluesasanISOintervalwithoutanyadditionalprogramming.SASchoosesoneofthefollowingformsbasedontheinformationstoredintheISOduration,datetime,orintervalvariable:

Page 1193: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

•PnYnMnDTnHnMnS•yyyy-mm-ddThh:mm:ss.fff•PnYnMnDTnHnMnS/mm-ddThh:mm:ss.fff•yyyy-mm-ddThh:mm:ss.fff/PnY•yyyy-mm-ddThh:mm:ss.fff/yyyy-

Page 1194: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mm-ddThh:mm:ss.fff

$N8601EAw.

$N8601EAw.writesduration,datetime,andintervalvaluesintheISO8601extendednotation.Itisleft-justified,andwcanbefrom1to200,withadefaultwidthof50.In

Page 1195: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ordertodisplaydatetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplayintervalvaluescorrectly,theminimumwidthis32.Ifyouattempttodisplayanintervalwithawidthspecificationoflessthan32,youwillgetaseriesofasterisks(*)as

Page 1196: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youroutput.Inversion9.4,SASwillautomaticallyprovideupto3decimalplacesforthesecondscomponentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbedisplayed).Whether

Page 1197: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

one,two,orthreedecimalplacesareshownisdependentupontheformatwidth.TheformatwilldisplaydurationvaluesasanISOdurationandintervalvaluesasanISOintervalwithoutanyadditionalprogramming.SASchoosesoneofthefollowingformsbased

Page 1198: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ontheinformationstoredintheISOduration,datetime,orintervalvariable:

•Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff•Pyyyy-mm-ddThh:mm.ss.fff/yyyy-mm-ddThh:mm.ss.fff

Page 1199: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

•yyyy-mm-ddThh:mm.ss.fff/Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff/yyyy-mm-ddThh:mm.ss.fff

$N8601EHw.

$N8601EHw.writesduration,datetime,and

Page 1200: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalvaluesintheISO8601extendednotation,substitutinghyphensforanymissingcomponents.Withthisformat,omitteddatetimecomponentsarealwaysdisplayed.Itisleft-justified,andwcanbefrom1to200,withadefaultwidthof50.Inordertodisplay

Page 1201: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplayintervalvaluescorrectly,theminimumwidthis32.Ifyouattempttodisplayanintervalwithawidthspecificationoflessthan32,youwillgetaseriesofasterisks(*)asyouroutput.Inversion

Page 1202: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9.4,SASwillautomaticallyprovideupto3decimalplacesforthesecondscomponentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbedisplayed).Whetherone,two,orthree

Page 1203: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

decimalplacesareshownisdependentupontheformatwidth.TheformatwilldisplaydurationvaluesasanISOdurationandintervalvaluesasanISOintervalwithoutanyadditionalprogramming,anditwilldisplaytheminoneofthefollowingforms.SASchoosesone

Page 1204: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthefollowingformsbasedontheinformationstoredintheISOduration,datetime,orintervalvariable:

•Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff•Pyyyy-mm-ddThh:mm.ss.fff/yyyy-

Page 1205: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff/Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff/yyyy-mm-ddThh:mm.ss.fff

$N8601EXw.

Page 1206: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

$N8601EXw.writesduration,datetime,andintervalvaluesintheISO8601extendednotation,substitutingtheletter"x"foranymissingcomponents.Withthisformat,omitteddatetimecomponentsarealwaysdisplayed.Itisleft-justified,andwcanbefrom1to200,witha

Page 1207: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultwidthof50.Inordertodisplaydatetimeanddurationvaluescorrectly,thewidthmustbeatleast16,whiletodisplayintervalvaluescorrectly,theminimumwidthis32.Ifyouattempttodisplayanintervalwithawidthspecificationoflessthan32,youwillgeta

Page 1208: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

seriesofasterisks(*)asyouroutput.Inversion9.4,SASwillautomaticallyprovideupto3decimalplacesforthesecondscomponentofthesevalues(indicatedbelowbyfff),regardlessofsignificance(thatis,ifthereisnodecimalfractionofseconds,thenzeroswillbe

Page 1209: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

displayed).Whetherone,two,orthreedecimalplacesareshownisdependentupontheformatwidth.TheformatwilldisplaydurationvaluesasanISOdurationandintervalvaluesasanISOintervalwithoutanyadditionalprogramming.SASchoosesoneofthe

Page 1210: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

followingformsbasedontheinformationstoredintheISOduration,datetime,orintervalvariable:

•Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff•Pyyyy-mm-ddThh:mm.ss.fff/yyyy-mm-

Page 1211: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff/Pyyyy-mm-ddThh:mm.ss.fff•yyyy-mm-ddThh:mm.ss.fff/yyyy-mm-ddThh:mm.ss.fff

4.5.3ISO8601Durationand

Page 1212: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IntervalInformatsInordertostoreduration,interval,anddatetimevaluesthatarealreadyrepresentedintheirISO8601formascharacterstrings,youwillneedtousethefollowinginformatswiththeINPUTstatementorINPUT()

Page 1213: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

function.TheCALLIS8601_CONVERTroutineisdesignedtoworkwithSASdate,time,anddatetimevalues,notcharacterstrings.

$N8601Bw.$N8601Bw.willtranslateduration,datetime,andinterval

Page 1214: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringswrittenintheISO8601basicorextendednotationintotheSASinternalrepresentationforthesevalues.Missingcomponentsarecorrectlyprocessedaslongasasinglehyphen(-)isusedinplaceofeachmissingcomponent.wcanbefrom1to200,witha

Page 1215: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultwidthof50.Inordertoprocessdatetimeanddurationvaluescorrectly,wmustbeatleast16,whiletoprocessintervalvaluescorrectly,wmustbeatleast32.Theinformatwillprocessdatainanyoneofthefollowingforms:

Page 1216: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISOFormRead

Pyyyy-mm-ddThh:mm:ss

Page 1217: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1218: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PyyyymmddThhmmss

Page 1219: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PnYnMnDTnHnMn.fffS

Page 1220: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PnYnMnDTnHnMn.fffS

PnW

yyyy-mm-ddThh:mm:ss.

yyyymmddThhmmssfff/yyyymmdd

PnYnMnDTnHnMn.fffS/

yyyy-mm-ddThh:mm:ss.

yyyy-mm-ddThh:mm:ss.

Page 1221: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yyyymmddThhmmss.fff

$N8601Ew.

$N8601Ew.willtranslateduration,datetime,andintervalstringswrittenintheISO8601extendednotationintotheSASinternalrepresentationforthesevalues.This

Page 1222: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informatdiffersfromthe$N8601B.informatinthatitwillonlyprocessvaluesthatareintheISO8601extendedformat.Ifyouattempttoreadavalueinthebasicnotationwiththisinformat,youwillgetanerror,andthestoredresultwillbeamissingvalue.Thisinformatisusefulwhen

Page 1223: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youneedtoenforceadherencetotheextendednotation.Missingcomponentsarecorrectlyprocessedaslongasasinglehyphen(-)isusedinplaceofeachmissingcomponent.wcanbefrom1to200,withadefaultwidthof50.Inordertoprocessdatetimeandduration

Page 1224: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuescorrectly,wmustbeatleast16,whiletoprocessintervalvaluescorrectly,wmustbeatleast32.

Todemonstratethedifferencebetweenthe$N8601B.and$N8601E.informats,theexamplebelowwillusethesameduration

Page 1225: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringinboththebasicandextendednotationsandtrytoprocessitwitheachinformat.

Example4.2:The$N8601B.Informatversusthe$N8601E.Informat

stored=INPUTC(duration_string,inf);

Example4.2usestheabovecodetostorethe

Page 1226: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

durationstring.infrepresentstheinformatbeingused.

ReadingtheISO8601BasicNotation

InformatUsed DurationString

$N8601B. P00020806T0100

$N8601E. P00020806T0100

Page 1227: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Asyoucansee,the$N8601E.informatfailedtoprocessthebasicnotationstring,resultinginamissingvalue.WhenyoutrytoprocessastringintheISObasicnotationusingtheextendednotation-specificinformat,youwillgetthefollowingnoteinyourlog.

Page 1228: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NOTE:InvalidargumenttofunctionINPUTatlinexxxcolumnyy.

Nowlet'sseewhathappenswhenwetrytoprocesstheextendednotationstring.

ReadingtheISO8601ExtendedNotation

Informat Duration Stored

Page 1229: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Used String Representation

$N8601B. P0002-08-06T01:00

$N8601E. P0002-08-06T01:00

Inthiscase,both

Page 1230: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informatscausedSAStostoreidenticalvalues.The$N8601B.informatcanprocessbothbasicandextendednotations,butthe$N8601E.informatwillonlyworkwiththeextendednotation.Usethe$N8601E.informatifyouwanttomakesurethattheinputdataconformtothe

Page 1231: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

extendednotation.

4.5.4CALLIS8601_CONVERTItisimportanttounderstandthatalthoughISOdurationsandintervalsarestoredincharactervariables,theyarenotsimpletext.Youcannotconvertadatetimetoa

Page 1232: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

characterstringusingthePUT()functionandthenconcatenateitwithadurationstringoranotherdatetimethathasbeenconvertedtoacharacterstring.InordertousetheSASdateandtimecapacitywithISOdurationsandintervals,youmustfirstconvertISOdurationandintervalvaluesto

Page 1233: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aninternalrepresentationthatwillallowthis.

Theformatsandinformatswejustdiscussedwillworkoncharactervalues,butwhatifyouhaveSASdateordatetimevalues,whichofcoursearestoredinnumericvariables,andyou

Page 1234: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttousethosetobuildyourISOdurationsandintervals?TheCALLIS8601_CONVERTroutineperformsthisconversioninbothdirections,soitwillcreateyourISOdurationsandintervalsfromthoseSASdateanddatetimevalues.Itwillalsodothereverse,

Page 1235: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtransformyourISOdurationsandintervalsintotheirindividualdateanddatetimecomponents.Inordertocreateanintervaloraduration,youwillneedtwovalues:astartandanend(definedasdatesordatetimes),oryoucanprovideaduration,andeitherstartoranend.

Page 1236: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Similarly,inordertoconvertaninterval,youwillbesplittingthecomponentsintotwovariables,adurationandadatetime,ortwodatetimes.Inaddition,theroutinecanderivethestartingorendingpointofanISOdurationorinterval.

Thesyntaxforthe

Page 1237: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CALLIS8601_CONVERTroutineis:

CALLIS8601_CONVERT(convert-from,convert-to,from--variables,to-variables,replacements);

Theconvert-fromargumentdescribesthetypeof

Page 1238: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date/datetime/interval/durationdatathatyouareconverting.convert-fromcanbeoneofthefollowingkeywordvalues,enclosedinquotationmarks,ormightalsoberepresentedbyacharactervariablethatresolvestooneofthesevalues.

Page 1239: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

KeywordValue

Description

'dn' Usethiswhenthevaluethatyouwanttoconvertconsistsofindividualcomponentsofadatevalue.n

Page 1240: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

from1to6,andindicateshowmanycomponentsareinthelistoffrom-variablesYoucancreateeitheradateoradatetimevaluewiththis

Page 1241: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

argumenttype.Theindividualcomponentsfromlefttorightare:month,day,year,hour,minute,andsecond.

'dtn' Usethiswhenthevaluethatyou

Page 1242: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttoconvertareindividualcomponentsofadatetimevalue.isfrom1to6,andindicateshowmanycomponentsareinthelistoffrom-variables

Page 1243: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Youcancreateeitheradateoradatetimevaluewiththisargumenttype.Theindividualcomponentsfromlefttorightare:month,day,year,

Page 1244: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hour,minute,andsecond.

'dun' Usethiswhenyouwanttoconvertavalueconsistingofindividualcomponentsofadurationvalue.isfrom1to6,

Page 1245: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andindicateshowmanycomponentsareinthedurationvalue.Theindividualcomponentsfromlefttorightarethenumberofmonths,days,

Page 1246: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

years,hours,minutes,andsecondsintheduration.

'dt/dt' Usethiswhenyouareconvertingtwodatetimevalues.

Page 1247: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'dt/du' Usethiswhenyouareconvertingadatetime/durationinterval.Thissignifiesthatthedatetimeisthestartoftheintervalthatyouareconverting.

Page 1248: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'du/dt' Usethistoconvertaduration/datetimeinterval.Thissignifiesthatthedatetimeistheendoftheintervalthatyouareconverting.

Page 1249: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'intvl' Usethistoconvertintervalvalues.

Theconvert-toargumentdescribestheformoftheresultfromtheCALLIS8601_CONVERTroutine.

Page 1250: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

KeywordValue Description

'intvl' Usethistocreateanintervalvalue.

'dt/dt' Usethistocreateadatetime/datetimeinterval.

'dt/du' Usethistocreate

Page 1251: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'dt/du' Usethistocreate

adatetime/durationinterval.Thissignifiesthatthedatetimeisthebeginningoftheinterval.

'du/dt' Usethistocreatea

Page 1252: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

duration/datetimeinterval.Thissignifiesthatthedatetimeistheendoftheinterval.

'du' Usethistocreateaduration.

'start' Usethistoderive

Page 1253: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'start' Usethistoderive

astartingdateordurationfromanintervalvalue.

'end' Usethistoderivetheendingdateordurationfromanintervalvalue.

'dn' Usethiswhenyou

Page 1254: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttocreateindividualcomponentsofadatevalue.n1to6,andindicateshowmanycomponentsareinthelistoffrom-variables.The

Page 1255: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

individualcomponentswillbestoredfromlefttorightinthefollowingorder:month,day,year,hour,minute,andsecond.

'dtn' Usethiswhenyou

Page 1256: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttocreateindividualcomponentsofadatetimevalue.from1to6,andindicateshowmanycomponentsareinthelistoffrom-variables.The

Page 1257: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

individualcomponentswillbestoredfromlefttorightinthefollowingorder:month,day,year,hour,minute,andsecond.

'dun' Usethistocreate

Page 1258: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'dun' Usethistocreate

individualcomponentsofadurationvalue.from1to6,andindicateshowmanycomponentsareinthelistoffrom-variables.Theindividual

Page 1259: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

componentswillbestoredfromlefttorightinthisorder:thenumberofmonths,days,years,hours,minutes,andseconds.

from-variablesarethe

Page 1260: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variablescontainingthevalue(s)tobeconverted.Specifyonevariableifyouareconvertinganintervalandtwofordatetimeand/ordurationvalues.Ifyouareconvertingindividualcomponentsofadateoradatetime(using'dn,''dtn,'or'dun'inyourconvert-fromargument),then

Page 1261: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youwillneedonevariableforeachcomponentspecified.Ifyouareconvertinganinterval,thevariablemustbeatleast32charactersinlength.

to-variablesarethevariablescontainingtheresultthattheroutinecalculates.Specifyonevariableif

Page 1262: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youarecreatinganintervalandtwofordatetimeand/ordurationvalues.Ifyouwantindividualcomponentsofadateoradatetimefromthefunction(using'dn,''dtn,'or'dun'inyourconvert-toargument),thenyouwillneedonevariableforeachcomponentspecified.

Page 1263: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

replacementsenablesyoutoprovideyourownvalueforthemonth,day,hour,minute,andsecondcomponentstobeusedifanyofthosecomponentsaremissingintheconvert-fromargument.Thedefaultis1formonthandday,and0forhour,minute,and

Page 1264: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

second.Whenyouusethisparameter,eventhoughtheyearcomponentisrequiredforavalidISOdurationorintervalvalue,youwillneedaleadingcommaasaplaceholderfortheyearvalue,followedbyall5replacementvalues,separatedbycommas.Whileitis

Page 1265: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

possibletoleavesomeofthereplacementvaluesblank(andtherebyusethedefault),itiseasiertoreadwhenavalueisprovidedforeachofthereplacements,evenifitisthesameasthedefault.

Thefollowingexamplesdemonstratemanyof

Page 1266: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thecapabilitiesoftheCALLIS8601_CONVERTroutine,includingtheabilitytoperformdateandtimecalculations.

Example4.3:HowLongIs…inSAStime?MostofthecalculationsinvolvingSASdates

Page 1267: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtimeshaveaspecificreferencepointinmind;ingeneral,thereisadateand/oratimeinvolved,suchasJune8,2014,at3:50p.m.However,whatcanyoudoifyouwanttomeasureanISOdurationinSAStime?OnepossibilityistouseasamplestartingpointandSASintervalsto

Page 1268: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

obtaintheendpoint.Fromthere,itisjustamatterofsubtractingyourdummystartpointfromyourendpoint.OryoucoulduseCALLIS8601_CONVERT.Thefirstperiodissimple:Howlongisfourweeksexpressedintime?Thesecondperiodasksthesamequestionfor3days,4hours,27

Page 1269: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

minutes,and16.8seconds,whichisnotsoeasytodowithSASintervals.

1.DATAhowlong;2.LENGTHperiod$16;3.period="P4W";4.CALLIS8601_CONVERT('du','du',period,howlong);5.howlong_disp=STRIP(PUT(howlong,time10.1));6.OUTPUT;7.period="P3DT4H27M16.8S";

Page 1270: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8.CALLIS8601_CONVERT('du','du',period,howlong);9.howlong_disp=STRIP(PUT(howlong,time10.1));10.OUTPUT;11.RUN;

Inlines4and8,weuse'du'astheconvert-toargumentbecausethereisnoargumentvaluethatwillexplicitlygiveatimevalue.However,whenyouusethe'du'

Page 1271: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

argumentanddonotspecifytheresultasacharactervariable,CALLIS8601_CONVERTwillcreateyourdurationinseconds(thatis,aSAStimevalue).HereisthePROCPRINTofourHOWLONGdataset:

Page 1272: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601Duration

TimeinSeconds

P4W 2419200.0

P3DT4H27M16.8S 275236.8

Example4.4:ConvertingTwoDatetimestoanISO

Page 1273: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DurationThissituationisonefrequentlyencounteredinclinicaltrials.Thestartingandendingdatetimesforaneventwouldbepresentedastext,andyouneedtocalculatethedurationanddisplayitinISO8601formataccordingtotheCDISC

Page 1274: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specification.Inthisexample,wewilltakesimulatedeventdata(someofwhichhasmissingcomponentsandentiremissingvalues),calculatethedurations,andpresenttheresultaccordingtotheISOstandard.Herearethedatashownintheiroriginaltextrepresentation.

Page 1275: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ObsStartingDate

EndingDate

1

2 2013-01-18T09:30

2013-01-18T21:00

3 2012-12-30T11

2013-01-01T14:00

4 2012-11 2013-01-

Page 1276: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

04

5 2012-11-19

2013-01-04

6 2012-11-20T08:30

2013-01-03

7 2012 2013-02

8 2012-12-27

Page 1277: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

9 2012-11 2012-11-20

10 2012-11-19T14:15

2012-11-20

11 2012-12-17T08:20

2013-01-02T09

ThefirststepistocreatetheseasSAS

Page 1278: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalues.Let'srunaquickSASprogramtocreateourexampledataset,AEDTM:

DATAbook.aedtm;INFILE"eventdata.txt"PADMISSOVERDLM='09'xFIRSTOBS=2DSD;INPUTaestdtm:E8601DT.aeendtm:E8601DT.;FORMATaestdtmaeendtmdatetime20.;RUN;

Page 1279: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Executingthiscoderesultsinthisdataset:

Whathappenedhere?Mostofthedatetimevaluesaremissing!

Page 1280: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thisisadirectconsequenceofincompletedatafordatesandtimesinthesourcedata.RememberthatSASdatetimevaluesareacompletedateandacompletetime,soyes,thevaluesaremissingbecausethereisnocompletedateandtimeinmostofthesecases.TheISO

Page 1281: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

standardcanaccommodatepartialdatesandtimes,butthenormalSASdateandtimefacilitycannot.Let'strythisadifferentwayusingtheISO8601informats.

DATAbook.aedtm2;INFILE"eventdata.txt"PADMISSOVERDLM='09'xFIRSTOBS=2DSD;INPUTaestdtm:$N8601B.

Page 1282: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

aeendtm:$N8601B.;FORMATaestdtmaeendtmdatetime20.;RUN;

Thiscodedidn'tevenrun,givinganerrorinthelog.

1DATAbook.aedtm2;2INFILE"eventdata.txt"PADMISSOVERDLM='09'xFIRSTOBS=2DSD;3INPUTaestdtm:$N8601B.aeendtm

Page 1283: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

:$N8601B.;4FORMATaestdtmaeendtmdatetime20.;-----------48ERROR48-59:Theformat$DATETIMEwasnotfoundorcouldnotbeloaded.

5RUN;

NOTE:TheSASSystemstoppedprocessingthisstepbecauseoferrors.WARNING:ThedatasetBOOK.AEDTM2maybe

Page 1284: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

incomplete.Whenthisstepwasstoppedtherewere0observationsand2variables.

TheISOinformatsonlycreatecharactervariables.Therefore,theerrorariseswhenyoutrytouseanumericformatwithacharactervariable.Atthispoint,weareno

Page 1285: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

closertogettingourdurationsthanbefore.WhathappenswhenwetaketheFORMATstatementout?

Nowwe'vegottheinternalrepresentation

Page 1286: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofISOdatetimesinaSASdataset,whichwehavenamedAEDTM2.Let'sformatthetwovariablesusingthe$N8601EH.formatsothatwecanseetheresultwithaproperISOrepresentation.The$N8601EH.formatprovideshyphensformissingcomponents.

Page 1287: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Fromhere,wecancalculatedurationswithCALLIS8601_CONVERTusingthefollowingcodeandthedatasetAEDTM2thatwecreatedintheprevious

Page 1288: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

step.Inline3,wedefinetheresultvariablesascharactersothatwedonotgetaSAStimevalue.Wearecreatingtwoforthepurposesofthisexercise,DURATIONwiththeroutineinline4,andAEDURinline6,whichistheISO-formattedversionofDURATIONsothatit

Page 1289: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canbeshownalongsidetheinternalrepresentationoftheduration.

1.DATAbook.iso_durations;2.SETbook.aedtm2;3.LENGTHdurationaedur$16;4.CALLIS8601_CONVERT('dt/dt','du',aestdtm,aeendtm,duration);5.aedur=duration;6.FORMATaedur$N8601E.;

Page 1290: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7.RUN;

Oneofthethingsthatyoumightnoticeisthatdurationshavebeencalculatedforallthevaluesthathaveanendingdate,regardlessofmissingcomponents

Page 1291: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inthestartingandendingdatetimes.TheCALLIS8601_CONVERTroutineimputescompletedatetimevaluesbyusingdefaultvaluesformissingcomponents,soithassupplied1formissingmonthsanddays,andzerosforhours,minutes,andseconds.

Page 1292: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thiscausesanydatetimewithamissingtimetobesetat12:00a.m.ofthefollowingday.Thissimpleimputationmightnotbewhatyouwanttouse,andmoreelaboratecodingmightbenecessary.Youmighthavetostartwithparsingtheinputdatetimestringto

Page 1293: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

determinewhichcomponentsaremissingandapplyyourimputationalgorithmfromthere.Nonetheless,onceyouhaveyourimputeddatetimevalues,CALLIS8601_CONVERTwillcreateandstoreanISOdurationfromthosedatetimes.

Page 1294: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example4.5:ConvertingTwoDatetimestoanISOIntervalLet’susethedatafromexample4.4toquicklydemonstratethecreationofanISOintervalusingtheCALLIS8601_CONVERTroutine.WewillstartwiththeISOdatetime

Page 1295: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesstoredinthedatasetAEDTM2.

ThefollowingcodewillcreateourISOinterval.Notethatinline3,thelengthoftheresultvariableintervalisset

Page 1296: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

to32,whichistheminimumlengthforavariablethatholdsanISOinterval.Theresultisshowninthescreencapturefollowingthecode.Onceagain,wearecreatingacopyoftheresulttoshowboththeintervalvalueasastoredvalueandtheISO-formattedintervalvalue.

Page 1297: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.DATAbook.iso_intervals;2.SETbook.aedtm2;3.LENGTHintervalformatted_interval$32;4.CALLIS8601_CONVERT('dt/dt','intvl',aestdtm,aeendtm,interval);5.formatted_interval=interval;6.FORMATformatted_interval$N8601E.;7.RUN;

Page 1298: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example4.6:ConvertingaDatetimeandaDurationintoanISOIntervalInthisexample,wewillcreateanISOintervalfroma

Page 1299: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimeandadurationusingthefollowingdataset,SAMPLE,whichcontainsastartdatetimeandanISOduration.

Whydon'tweapplythe

Page 1300: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

$N8601E.formattoAESTDTMandDURATIONsothattheyarereadable?

Ifthisweretrueclinicaldata,therewouldbeanerrorinthedataat

Page 1301: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

record3,becausethedurationisnegative,indicatingthatthestartdatetimeislaterthantheendingdatetime.WhiletheISOstandardaccountsfornegativedurations,thisdoesnotmeanthatnegativedurationsareappropriateinyourdata.

Page 1302: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thiscodelookssimilartothecodeintheprevioustwoexamples.ThedifferenceisintheargumentsusedfortheCALLIS8601_CONVERTroutineinline4.The'dt/du'signifiesthatthedatetimerepresentsthestartoftheinterval.Wewillshowtheunformattedand

Page 1303: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formattedresultasareminderthatthecharacterresultsarestoredintheirSASinternalrepresentation:

1.DATAinterval;2.SETsample;3.LENGTHintervalformatted_interval$32;4.CALLIS8601_CONVERT('dt/du','intvl',aestdtm,duration,interval);5.formatted_interval=interval;6.FORMAT

Page 1304: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatted_interval$N8601E.;7.RUN;

Interval

2012B221030FFFFDFFFFF072330FFFFC

2012C02FFFFFFFFDFFFF105FFFFFFFFC

2012C121200FFFFDFEFFFFF12FFFFFFC

Page 1305: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012C191000FFFFDFFFFF072130FFFFC

2013101FFFFFFFFDFFFFF29FFFFFFFFC

Example4.7:CalculatingtheEndofanIntervalfromaDatetimeandaDuration

Page 1306: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AnadditionalcapabilityoftheCALLISO_8601routineistocalculatestartingandendingdatetimesfromintervals,oracombinationofdurationanddatetime.Inthisexample,wewillcalculatetheenddatetimesfortheISOintervalsthatwecreatedinexample4.6.

Page 1307: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.DATAinterval_end;2.SETinterval;3.CALLIS8601_CONVERT('intvl','end',interval,end);4.FORMATinterval$N8601E.endDATETIME20.;5.RUN;

Youcanseethatthiscodeisagainslightlydifferentfromexample4.6.OnelargedifferenceisthatthereisnoLENGTH

Page 1308: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statementusedtocreatetheresultasacharactervariable.ThiscausestheroutinetocreatetheresultasaSASdatetimevalue,notanISO8601datetimecharacterstring.Thisisanimportantdistinction.Inline3,wetelltheroutinethatwearegoingtoconvertaninterval,andwe

Page 1309: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttheendingdatebasedonthatinterval.SincethevariableINTERVALisstillstoredinitsinternalSASrepresentation,wehaveappliedthe$N8601E.formattothatvariable,aswellasformattingtheENDvariablewithDATETIME20.sowecanreadthemeasilyin

Page 1310: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theoutput.Hereistheresult.

Interval

2012-11-22T10:30/P7DT23H30M

2012-12-02/P1M5D

2012-12-12T12:00/-PT12H

Page 1311: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012-12-19T10:00/P7DT21H30M

2013-01-01/P29D

Anyoftheprecedingexamplescanbereversed;youcanconvertanISOdurationorintervalintotwodatetimesoryoucanturnan

Page 1312: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalintoadurationandadatetime.Toshowyouhowitallconnectstotogether,inthislastexample,wewilltakestartingandendingISOdatetimevaluesandcreateanISOdurationandanISOinterval,andthenwewillrecalculatethestartingandendingdatetimesfromthat

Page 1313: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISOinterval,bothastheirISOdatetimevaluesandtheirSASdatetimevalues.

Example4.8:CALLIS8601_CONVERT:ISODatetimes,Durations,andIntervalsfromStarttoEndandBackAgainwithOneRoutine

Page 1314: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThiscodeusesthedatasetEX4_8,whichcontainstwoISOdatetimevaluesinthevariablesAESTDTMandAEENDTM.Theyaredisplayedusingthe$N8601E.formatsoyoucanreadthevalueseasily.

Page 1315: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

BecausewewanttheresultsinthisexampleastheirISOrepresentations,wedefinetheresultvariablesthatwearecreatingascharactervariables,includingtheendingdatetimes.Note

Page 1316: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatthevariableINTERVALis32bytesinlengthbecausethatistheminimumlengthforanISOintervalvalue.Line4createstheISOdurationandline5theISOintervalvalue,andweusethatintervalinlines6and7toproducethestartingandendingISOdatetimevalues.Lines

Page 1317: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8and9linesrecalculatethestartingandendingdatetimesasSASdatetimevalues.Wewillpresenttheresultsseparately.

1.DATAcircle;2.SETex4_8;3.LENGTHduration$16interval$32start_dt$16end_dt$16;4.CALLIS8601_CONVERT('dt/dt','du',aestdtm,aeendtm,duration);5.CALL

Page 1318: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IS8601_CONVERT('dt/dt','intvl',aestdtm,aeendtm,interval);6.CALLIS8601_CONVERT('intvl','start',interval,start_dt);7.CALLIS8601_CONVERT('intvl','end',interval,end_dt);8.CALLIS8601_CONVERT('intvl','start',interval,datetime_start);9.CALLIS8601_CONVERT('intvl','end',interval,datetime_end);10.FORMATdurationintervalstart_dtend_dt$N8601E.;11.RUN;

Original Original

Page 1319: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

StartingISODatetime

EndingISODatetime

ISODurationValue

2012-11-21T09:12

2012-11-30

2012-11-21T09:25

2012-11

Page 1320: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012-12-03T09:00

2012-12-03T10:00

2012-12-14T09:12

2013-01-17

2012-12-14

2012-12-29

Page 1321: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

14 29

Theonethingthatyoushouldtakenoteofhereisthenegativedurationvalueinthesecondrow.Sincethedaycomponentismissingfromtheendingdatetime,CALLIS8601_CONVERThasuseditsdefaultvalueforthedaycomponent,

Page 1322: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichis1.Obviously,November21(thestartingdate)isafterNovember1,soyoumightwanttouseadifferentimputationforyourendingdates.Nextaretherecalculatedstartingandendingdates,presentedinseparatetablestomakeiteasytoseetheresultoftheconversions.

Page 1323: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OriginalStartingISODatetime

RecalculatedISOStartingDatetime

2012-11-21T09:12

2012-11-21T09:12

2012-11-21T09:25

2012-11-21T09:25

Page 1324: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2012-12-03T09:00

2012-12-03T09:00

2012-12-14T09:12

2012-12-14T09:12

2012-12-14

2012-12-14

Page 1325: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OriginalEndingISODatetime

RecalculatedISOEndingDatetime

2012-11-30

2012-11-30

2012-11 2012-11

2012-12-03T10:00

2012-12-03T10:00

Page 1326: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

03T10:00 03T10:00

2013-01-17

2013-01-17

2012-12-29

2012-12-29

Takenoteofthesecondrowintheendingdatetimetableabove.Notonlydoesthis

Page 1327: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

pointouttheproblemwithrelyingontheautomaticsubstitutionoftheroutine,butthedifferencebetweentheISOdatetimevaluesandSASdatetimevalues.TheISOstandardaccommodatesmissingcomponents,soitislegaltohavethevalue"2012-11,"which

Page 1328: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

clearlyindicates,"DuringthemonthofNovember,buttheexactdayisunknown,"and,"Atsometimeonthedayof…"TheSASdatetimevaluehastoprovidebothexactdateandexacttime,so"2012-11"issetto12:00AM,November1,2012,usingthedefaultreplacements

Page 1329: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formissingcomponents.

4.6ConclusionTheISO8601standardhasbeendevelopedfortheclearcommunicationofdateandtimeinformationacrosscountries,applications,and

Page 1330: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

platforms.SAShasthecapabilitytohandlethisstandardtoitsfullextent,eventhoughonthesurfacethereseemtobemanyincompatibilitiesbetweentheSASdateandtimefacilityandtheISOstandard.Thelargestdifferencesarewithrespecttoincompletedata,and

Page 1331: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatalphabeticcharactersareusedasmorethansimpledelimitersmakestheISO8601handlingfacilityinSASuniquewhenitcomestodatesandtimes.ItistheonlypieceoftheSASdateandtimecapabilitiesthatrequirescharactervariables.

Page 1332: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter5:DateandTimeFunctions

5.1CurrentDateandTimeFunctions

Page 1333: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.2ExtractingPiecesfromSASDate,Time,andDatetimeValues5.3CreatingDates,Times,andDatetimesfromNumbersorOtherInformation

Page 1334: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.4CalculatingElapsedTime,andtheHOLIDAY()Function5.5TheBasicsofSASIntervals5.6ModifyingSASIntervals5.7CreatingYour

Page 1335: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OwnSASIntervals5.8IntervalFunctionsaboutIntervals5.9RetailCalendarIntervalsandSeasonality

SAShasmanyfunctionstomanipulate

Page 1336: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dates,times,anddatetimevalues.Thefunctionscanbecategorizedaccordingtowhattheydo.Youcanobtainthecurrentdate,time,ordatetime(asspecifiedbythecomputer'sclock).Youcanalsoeasilyextractpiecesofdates,times,ordatetimesasnumericalvaluesfrom

Page 1337: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theircorrespondingSASvalues,oryoucanassembleSASdate,time,anddatetimevaluesfromSASvariablesorconstants.Anothersetoffunctionsoperateswithintervalssuchasweeksormonths.

Page 1338: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.1CurrentDateandTimeFunctionsCurrentdateandtimefunctionshavenoargumentsandreturnSASvaluesasnotedinthefollowingtable.Thevaluesareobtainedfromtheoperatingsystem'sclock.

Page 1339: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Table5.1:CurrentDateandTimeFunctions

DateandTimeFunction Description

DATE(),TODAY()

Thesefunctionsareidentical,

Page 1340: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andbothreturnthecurrentdateasaSASdatevalue.

TIME() Thisreturnsthecurrent

Page 1341: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeasaSAStimevalue.

DATETIME() ThisreturnsthecurrentdateandtimeasaSAS

Page 1342: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalue.

5.2ExtractingPiecesfromSASDate,Time,andDatetimeValuesTheextraction

Page 1343: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functionsalluseasingleargument(representedbyarginthefollowingtable),whichrepresentsaSASdate,time,ordatetimevalue.ThiscanbeeitheraSASvariablenameortheappropriateconstant.Iftwo-digityearvaluesareused,theresultwillbesubjecttothe

Page 1344: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YEARCUTOFF=optionvalueineffect.Theyallreturnanumericvalueastheresult.Thefollowingtablesareseparatedintofunctionsthatusedatesasanargument,thosethatusedatetimesastheargument,andthosethatusetimesasanargument.Eachtablegivesexamplesof

Page 1345: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

howtoapplyeachfunction,alongwithrelevantcommentsforeachexample.

Table5.2:FunctionsReturningaDateComponentandRequiringaSASDateValueasanArgument

FunctionName Explanation

DAY(arg) Extractsthenumber

Page 1346: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthedayofthemonthfromaSASdatevalue.

JULDATE(arg) ExtractstheJuliandatefromaSASdatevalue.Itwillreturnafour-orfive-digitvaluewithaone-ortwo-digityear,ifthe

Page 1347: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearportionofthedatefallswithinthe100-yearspandefinedbytheYEARCUTOFF=option.Ifyouwanttoensurefour-digityearvalues,youshouldusethe

Page 1348: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JULDATE7()function.

JULDATE7(arg) ExtractstheJuliandatewithafour-digityearfromaSASdatevalue.Thisalwaysreturnsaseven-digitnumber,regardless

Page 1349: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheyear.

MONTH(arg) ExtractsthenumericalmonthfromaSASdatevalue.

QTR(arg) ExtractsthequarteroftheyearfromaSASdatevalue.

Page 1350: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEK(arg) ExtractstheweeknumberfromaSASdatevalue,whereSundayisthefirstdayoftheweek,whichisthe"U"algorithm.ThisfunctionhasbeenaugmentedinSAS

Page 1351: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

versions9.1.3andabovebytheWEEK(functiondescribedimmediatelybelow.

(Version9.1.3andup)WEEK(arg,descriptor)

ExtractstheweeknumberfromaSASdatevalue.descriptorreferstothealgorithmusedtocalculatethefirstweekof

Page 1352: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theyear.

TheUalgorithmcalculatesweeksbasedonSundaybeingthefirstdayoftheweek.

TheValgorithmcalculatesweeksto

Page 1353: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theISOstandard.Mondayisthefirstdayoftheweek,andthefirstweekoftheyearisdefinedastheonethatcontainsbothJanuary4andthefirstThursdayoftheyear.

Page 1354: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheWalgorithmcalculatesweeksbasedonMondaybeingthefirstdayoftheweekwithoutrestriction.

WEEKDAY(arg) Extractsthenumberofthedayofthe

Page 1355: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

week,whereSunday=1,Monday=2,andsoonfromaSASdatevalue.

YEAR(arg) ExtractstheyearfromaSASdatevalue.Ifyouuseadateconstant(asin

Page 1356: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theexample)andnotaSASdatevalue,itisimportanttorememberthattheYEARCUTOFF=optionaffectstwo-digityears.

Table5.3:FunctionsReturningaDatetime

Page 1357: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ComponentandRequiringaSASDatetimeValueasanArgument

FunctionName Explanation

DATEPART(arg) ExtractsthedatefromaSASdatetimevalueasaSASdate

Page 1358: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

value.

TIMEPART(arg) ExtractsthetimeportionfromaSASdatetimevalueasaSAStimevalue.

Page 1359: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Table5.4:FunctionsReturningaTimeComponentandRequiringaSASTimeValueasanArgument

FunctionName Explanation

HOUR(arg) Extractsthehourfroma

Page 1360: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAStimevalue.

MINUTE(arg) ExtractstheminutesfromaSAStimevalue.

SECOND(arg) ExtractsthesecondsfromaSAS

Page 1361: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timevalue.

5.3CreatingDates,Times,andDatetimesfromNumbersorOtherInformation

Page 1362: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.3.1IntroductionThisseriesoffunctionswillcreateSASdate,time,anddatetimevaluesfromnumericalvariablesorconstants.Whileinformatstakecompletedate,time,anddatetimereferencesandtranslatethemtotheir

Page 1363: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

correspondingSASvalue,thesefunctionswillcreateaSASvaluefromdiscretepiecessuchasmonth,day,andyear.

5.3.2ListofFunctionsandTheirDescriptions

Page 1364: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATEJUL(Julian-date);DATEJUL(Julian-date);createsaSASdatevaluefromanumericvaluerepresentingaJuliandate.Julian-datemustbeofthetypeyy(yy)ddd,whereyy(yy)istwoorfourdigitsrepresentingtheyear,anddddmustbea

Page 1365: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numberfrom1to365(366ifaleapyear).Ifyouusetwodigitsfortheyear,theYEARCUTOFF=optionwillbeusedtodeterminethecentury.Thefollowingtablegivesexamplesofhowtoapplythisfunction:

Page 1366: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

OPTIONSYEARCUTOFF=1920;DATEJUL(21286)

Page 1367: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OPTIONSYEARCUTOFF=2000;DATEJUL(21286)

DATEJUL(2014174)

DATEJUL(1989005)

Page 1368: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATEJUL(00368)

Page 1369: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DHMS(date,hour,minute,second);DHMS(date,hour,minute,second);createsaSASdatetimevalue.Allfourargumentsarerequired.dateisaSASdatevalue,whichcanbeeitheranumericvalueoradateconstant.Ifyouuseatwo-digityearinadateconstant,thedatewill

Page 1370: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betranslatedaccordingtotheYEARCUTOFF=option.hour,minute,andsecondareallnumericvariablesand/orconstants.Hour,minute,andsecondarenotrestrictedtotheirclocktimes.Therefore,hourcanbegreaterthan24,whileminuteandsecondcanbegreaterthan60.The

Page 1371: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

followingtablegivesexamplesofhowtoapplythisfunction:

SampleFunctionCall

DHMS("08JUN2015"d,15,24,0)

DHMS("02FEB2014"d,11,54,15)

Page 1372: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DHMS("30JUN2012"d,8,7,93)

Page 1373: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1374: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1375: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.1:UsingDHMS()WhenYou

Page 1376: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AlreadyHaveaSASDateandTimeIfyouneedtocreateadatetimefromaSASdateandaSAStimevalue,youdonothavetousetheHOUR(),MINUTE(),andSECOND()functionstoextractthosecomponentsfromthetimevalue.Remember

Page 1377: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatSASkeepstrackoftimeinsecondssincemidnightandthatthesecondsargumentintheDHMSfunctioncanbegreaterthan59.Therefore,ifyousupplytheSAStimevalueasthesecondsargumentandsethourandminutetozeros,thatwillworkjustfine.Thefollowingexample

Page 1378: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

demonstrates:

1DATAex5_1;2INPUTrow$sasdate:date9.sastime:time8.;3result=DHMS(sasdate,0,0,sastime);4fmt_result=result;5formatsasdatedate9.sastimetimeampm.;6DATALINES;7A16oct201517:308B08jun201611:009C14apr201500:0010;;;11RUN;

Page 1379: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12PROCPRINTDATA=ex5_1NOOBSLABELSPLIT='\';13IDrow;14VARsasdatesastimeresultfmt_result;15FORMATfmt_resultdatetime19.sasdatesastime;16LABELrow='Row'17result='~{text_align=c}SASDatetimeValue\CalculatedUsing\DHMS(sasdate,0,0,sastime)'18fmt_result="ResultFormattedwith\DATETIME19.Format";19RUN;

Page 1380: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheResult

Row sasdate sastime

A 20377 63000

B 20613 39600

C 20192 0

Page 1381: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thecalculationofthedatetimevalueisdoneinline3oftheaboveprogram.Theformatshavebeenremovedfromthesasdateandsastimevariablesinthetableforthepurposeofillustratingtheactualvaluesthatarebeingsenttothefunction.RowCabovehasbeenincludedto

Page 1382: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

demonstratethatanyvalidSAStimevaluewillwork,even0seconds.Whatgoodisthat?IfyouneedtocreateaSASdatetimevalueandyouonlyhaveadatewithoutatimevalue,thenyoucanuseDHMS(sasdate,0,0,0)toconvertyourSASdateintoaSASdatetime.

Page 1383: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HMS(hour,minute,second);HMS(hour,minute,second);createsaSAStimevalue.hour,minute,andsecondareallnumericvariablesand/orconstants.Noneoftheparametersarerestrictedtotheirclocktimes.Therefore,hourcanbegreaterthan24,whileminuteandsecond

Page 1384: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canbegreaterthan60.Allthreeargumentsmustbepresentoryouwillgetamissingvalueastheresult.Thefollowingtableprovidesexamples:

SampleFunction Time

Page 1385: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Call Value

HMS(18,0,9) 64809

HMS(7,45,80) 27980

Page 1386: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1387: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HMS(15,03,35.56) 54215.56

HMS(8,17,33) 29853

Page 1388: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HMS(21,14,28) 76468

MDY(month,day,year);

MDY(month,day,year);createsaSASdatevaluefromthearguments.Allthreeargumentsarerequired.month,day,andyearareall

Page 1389: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numericvariablesorconstants.Ifyearistwodigits,thecenturywillbedeterminedbytheYEARCUTOFF=option.Ifavaluegivenforanyoftheargumentsisnotvalidormissing,suchasMDY(2,31,2014)(February31,2014),thefunctionwillreturnamissingvalueand

Page 1390: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

giveyouan"invalidargumenttofunction"messageinthelog.Thefollowingtableshowsexamples:

SampleFunctionCall

SASDateValue

MDY(9,3,1876) –

Page 1391: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

30434

MDY(12,14,15) 20436

MDY(3,26,1915) –16352

Page 1393: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

givenweekdayinagivenweeknumberfromagivenmonthandyear.Alloftheargumentsarenumericandcanberepresentedbyconstantsornumericvariables.Ifanyoftheargumentsaremissing,ornotvalid,thenthefunctionwillreturnamissingvalue.nrangesfrom1

Page 1394: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(first)to5(last).Sometimesusingthevalueof5willgivethesameresultas4,ifthefourthweekofthemonthisalsothelastweekofthemonth.weekdayrangesfrom1to7,where1representsSunday,and7representsSaturday.monthcanrangefrom1(January)to12

Page 1395: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(December).yearrepresentstheyearandissubjecttotheYEARCUTOFF=optionifyouusetwodigitsfortheyear.

Youcanusethisfunctiontofinddatesthatareexpressedas"ThefirstSaturdayinMay,"orthe"lastTuesdayinAugust,"

Page 1396: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

easily.Thefollowingtableprovidesexamplesofhowthisfunctionworks.

DateDescription SampleFunctionCall

First NWKDOM(1,3,11,2014)

Page 1397: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TuesdayinNovember.

SecondTuesdayinDecember.

NWKDOM(2,3,12,2014)

ThirdWednesdayinApril.

NWKDOM(3,4,4,2014)

Page 1398: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FourthSundayinMay.

NWKDOM(4,1,5,2014)

LastSundayinMay.

NWKDOM(5,1,5,2014)

FirstSundayin

NWKDOM(1,1,6,2018)

Page 1399: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

June.

SecondSaturdayinDecember.

NWKDOM(2,7,12,2018)

ThirdSaturdayinMay.

NWKDOM(3,7,5,2018)

Fourth NWKDOM(4,2,10,2018)

Page 1400: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MondayinOctober.

LastMondayinOctober.

NWKDOM(5,2,10,2018)

Theboldedanditalicizedtextaboveshowsthatthelast

Page 1401: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekofthemonthandthefourthweekofthemonthmayormaynotproducethesamedate.

YYQ(year,qtr);YYQ(year,qtr);createsaSASdatevaluefromthearguments.Bothargumentsarerequired.yearisanumericvariableor

Page 1402: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

constantrepresentingtheyear,andqtrisanumericvariableorconstantbetween1and4,representingthequarteroftheyear.Ifyearistwodigits,thecenturywillbedeterminedbytheYEARCUTOFF=systemoption.Thisfunctionreturnsthedateofthefirstdayofthequarter

Page 1403: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inthegivenyear.Thefollowingtableprovidesexamplesofhowthisfunctionworks.

SampleFunctionCall

SASDateValue

Formattedwithmmddyy10.Format

YYQ(2015,1) 20089

Page 1404: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQ(99,3) 14426

YYQ(25,2) –12693

YYQ(25,2) 23832

Page 1405: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQ(2015,2) 20179

Page 1406: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.4CalculatingElapsedTime,andtheHOLIDAY()FunctionBecauseSASusessimplemathasthebasisfordatesandtimes,youmightthinkthatcalculatingelapsedtimeorprojectingintothefuturewouldbe

Page 1407: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

easy.Itshouldbeamatterofsimpleadditionorsubtraction.However,SASprovidesseveralfunctionsthatdealwithcalculatingelapsedtime,andforthemostpartthefunctionisgoingtobemoreaccuratethansimplemath.Forexample,oneofthemathematicalequations

Page 1408: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

forcalculatingageis(currentdate–dateofbirth)/365.25.Thisapproximationusesthe.25toaccountforleapyears,butitfailstotakeintoaccounttheexceptionforyearsthataredivisibleby100butnotby400.Whileyoumightrarelyneedsuchaccuracywhencalculatingelapsed

Page 1409: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

years,theefficiencyofaSASfunctionmightspeedthingsperceptiblywhenworkingwithbigdata.

5.4.1CalculatingElapsedTimewithDATDIF()andYRDIF()

Page 1410: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thesetwofunctionswereoriginallydevelopedforusewithsecuritiescalculationsforspecificfinancialinstruments.

DATDIF(start,end,basis);DATDIF(start,end,basis);calculatesthenumberofdaysbetweentwodates.startisthe

Page 1411: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startingdate,whichcanbeadateconstant,anumericvariable,oraSASexpression.endistheendingdate,alsoadateconstant,anumericvariable,oraSASexpression.basisisacharacterconstantorvariablethattellsSAShowtocalculatethedifference.Thestartandendargumentsare

Page 1412: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

required,whilebasisisoptional.basishastwopossiblevalues.Notethatifyouuseacharacterconstantforbasis,rememberthatitwillneedtobeenclosedinquotationmarks,oryouwillgetanerror.

1.'30/360',which

Page 1413: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

setseachmonthto30days,andtheyearto360days,regardlessofhowmanydaysareineachmonthoryearinthespanbetweenthetwodates.Ifadayisattheendofamonth(forexample,

Page 1414: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

February28/29orMarch31),itwillbeconsideredasthe30thofthemonth.

2.'ACT/ACT',whichusestheactualnumberofdaysineachmonthandyearinthespan

Page 1415: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenthetwodates.Thisisthedefault,anditisidenticaltosubtractingstartfromend.

SampleFunctionCall

DATDIF('19JUL2015'd,'19JUL2016'd,'30/360')

Page 1416: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATDIF('19JUL2015'd,'19JUL2016'd,'ACT/ACT')

Page 1417: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YRDIF(start,end,basis);

YRDIF(start,end,basis);calculatesthenumber

Page 1418: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofyearsbetweentwodates.Itisalmostalwaysmoreaccuratethanusingmathematicalapproximation,dependingonthebasisusedandthedesiredresult.startisthestartingdate,whichcanbeadateconstant,anumericvariable,oraSASexpression.endis

Page 1419: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theendingdate,alsoadateconstant,anumericvariable,oraSASexpression.basisisacharacterconstantorvariablethattellsSAShowtocalculatethedifference.basishasfivepossiblevalues,ascomparedwiththetwopossibilitiesintheDATDIF()function:

Page 1420: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.'30/360',whichsetseachmonthto30days,andtheyearto360days,regardlessofhowmanydaysareineachmonthoryearinthespanbetweenthetwodates.Ifadayisattheendofa

Page 1421: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month(forexample,February28/29orMarch31),itwillbeconsideredasthe30thofthemonth.

2.'ACT/ACT',whichusestheactualnumberofdaysineach

Page 1422: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

monthandyearinthespanbetweenthetwodates.ThiswasthedefaultbasisthroughSASversion9.2.Youcanusethealias"Actual,"not'ACT.'

3.'ACT/360',whichusesthe

Page 1423: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

actualnumberofdaysbetweenthetwodatestocalculatethenumberofyears,butitusesa360-dayyear,regardlessofhowmanydaysareineachyear,sotheresultisnumberofdaysdividedby360.

Page 1424: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4.'ACT/365',whichusestheactualnumberofdaysbetweenthetwodatestocalculatethenumberofyears,butusesa365-dayyear,regardlessofhowmanydaysareineachyear,

Page 1425: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

sotheresultisnumberofdaysdividedby365.

5.'AGE',whichisusedtocalculateaperson'sage.ThisisavailablestartingwithSASversion9.3,andasofthatrelease,'AGE'isnowthedefault.

Page 1426: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

YRDIF('07AUG1967'd,'24MAY2014'd,'30/360')

Page 1427: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YRDIF('07AUG1967'd,'24MAY2014'd,'ACT/ACT')

Page 1428: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YRDIF('07AUG1967'd,'24MAY2014'd,'ACT/360')

Page 1429: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YRDIF('07AUG1967'd,'24MAY2014'd,'ACT/365')

Page 1430: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Asyoucansee,allfourresultsaredifferent,andthisisduetothewaytheywerecalculated.Priortoversion9.3,thisfunctionwasoftenusedtocalculateages,buteventhe'ACT/ACT'basisdoesn'tcalculateagesprecisely.The'ACT/ACT'basisaveragesleapyeardays

Page 1431: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

acrossthefouryears.Nowlet'sexaminetheYRDIFfunctionwhenthe'ACT/ACT'basisandthe'AGE'basisareused.

SampleFunctionCall

YRDIF('07AUG1967'd,'24MAY2014'd,'ACT/ACT')

YRDIF('07AUG1967'd,'24MAY2014'd,'AGE')

Page 1432: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThislooksasiftheYRDIF()functionwillyieldthesameresultforboththe'AGE'basisandthe'ACT/ACT'basis.Whereisthedifference?Let'slookatanotherseriesofdates.

SampleFunctionCall

Page 1433: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

A YRDIF('07AUG1968'd,'24MAY2014'd,'ACT/ACT')

YRDIF('07AUG1968'd,'24MAY2014'd,'AGE')

B YRDIF('07AUG1969'd,'24MAY2014'd,'ACT/ACT')

YRDIF('07AUG1969'd,'24MAY2014'd,'AGE')

C YRDIF('07AUG1971'd,'24MAY2014'd,'ACT/ACT')

YRDIF('07AUG1971'd,'24MAY2014'd,'AGE')

D YRDIF('07AUG1972'd,'24MAY2014'd,'ACT/ACT')

Page 1434: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YRDIF('07AUG1972'd,'24MAY2014'd,'AGE')

IngroupingsAandD,youseethatthebasismakesadifference,whileingroupingsBandC,the'ACT/ACT'and'AGE'returnidenticalresults.WhatmakesgroupingsAandDsodifferent?Theseareleapyears,andthe

Page 1435: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leapdayisaccountedforasawholedayforthatgivenyear,asopposedtotheaveragingofaquarterdayperyearperformedbythe'ACT/ACT'algorithm.

(U.S.andCanadaOnly)HOLIDAY(holiday,year

Page 1436: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY(holiday,year);providesthedateofselectedholidaysinanygivenyearasaSASdatevalue.ThisfunctionisvalidforU.S.andCanadaholidaysonly.holidaycanbeacharacterstringenclosedinquotationmarksoracharactervariablecontainingoneofthe

Page 1437: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

argumentslistedbelow.Thevalidvaluesofholidayarelistedinthetablebelow.Note:Ifyouuseacharactervariableinsteadofastring,yourvariableshouldbeatleast18characterslongtoaccommodatethelongestargument.

Page 1438: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ArgumentUsedinFunction

BOXING

CANADA

CANADAOBSERVED

Page 1439: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CHRISTMAS

COLUMBUS

EASTER

FATHERS

Page 1440: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HALLOWEEN

LABOR

MLK

Page 1441: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MEMORIAL

MOTHERS

NEWYEAR

THANKSGIVING

Page 1442: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

THANKSGIVING

THANKSGIVINGCANADA

USINDEPENDENCE

Page 1443: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

USPRESIDENTS

VALENTINES

Page 1444: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VETERANS

VETERANSUSG

Page 1445: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VETERANSUSPS

Page 1446: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VICTORIA

Page 1447: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

HOLIDAY("EASTER",2014)

HOLIDAY("EASTER",2019)

HOLIDAY("EASTER",2026)

Page 1448: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY("EASTER",2039)

HOLIDAY("THANKSGIVINGCANADA",2014)

Page 1449: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY("THANKSGIVINGCANADA",2019)

HOLIDAY("THANKSGIVINGCANADA",2026)

HOLIDAY("THANKSGIVINGCANADA",2039)

Page 1450: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY("USINDEPENDENCE",2014)

HOLIDAY("USINDEPENDENCE",2019)

HOLIDAY("USINDEPENDENCE",2026)

Page 1451: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY("USINDEPENDENCE",2039)

5.5TheBasicsofSASIntervals

Page 1452: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SomeoftheSASfunctionsdescribedinsection5.4,suchasDATDIF,areverygoodatcalculatingtheexactamountofelapsedtimebetweentwoSASdates,andasdemonstratedinsomeoftheaboveexamples,youcanseethedifferencebetweenthefunctionandsimplemath.Therearen't

Page 1453: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functionstoprojectfuturedates,becauseitwouldseemsimpleenough:youjustaddanumberofdays,hours,orminutes,andyoucomeupwithananswer.

However,wefrequentlyneedtorefertounitsoftimethatarenotuniform,suchas

Page 1454: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

months,whichcanbe28,29,30,or31dayslong.SASprovidesfunctionstocalculateintervalsbecause,inmanycases,simplemathisstillonlyanapproximation.SAShasseveralstandardintervaldefinitionsthatareusedwithdates,times,anddatetimesthatrepresentmanyof

Page 1455: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thenormalperiodsoftimethatwereferto,suchasweeksorquarters.Youarenotrestrictedtotheintervalsgiveninthestandarddefinitions,becauseyoualsohavetheabilitytoeasilymodifythem.Youcanusemultipliersand/orashiftindexinconjunctionwiththe

Page 1456: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

standardintervals.Multipliersenableyoutodefineintervalsthataremultiplesofastandardintervalandarenotalreadydefined,suchasadecadeoracentury.Ashiftindexenablesyoutodefineintervalsthatdonotcorrespondwiththestartingvaluesusedbyaninterval(standard

Page 1457: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ORwithamultiplier),suchasafiscalyearthatbeginsinJulyinsteadofJanuary,or,togiveyouanexamplewithamultiplier,adecadethatstartsinyearsendinginthenumber'5,'insteadofyearsendingin0.Section5.6willdiscusstheconceptsofmultipliersandthe

Page 1458: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftindexindetail.Ifyouneedintervalsthatcannotbedescribedbyusingmultipliersand/orashiftindexwiththestandardSASintervals,SAShasthecapacityforyoutodefineyourownintervals,andthisiscoveredindepthinSection5.7.

Page 1459: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Fortheremainderofthisbook,whentheterm"interval"isusedinafunctiondefinition,itmeansaSASintervalname,alongwithanymultiplierand/orshiftindexunlessexplicitlyspecifiedotherwise.Wewillbeginourdiscussionofintervalsbyprovidingalistofallthestandard

Page 1460: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervaldefinitionsandtheperiodsthattheydescribeinTable5.5.

Table5.5SASIntervalDefinitionsUsedwithDates,Times,andDatetimes

Category IntervalName

Page 1461: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Date DAY

WEEK

WEEKDAYdaysW

Page 1462: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1463: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TENDAY

Page 1464: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SEMIMONTH

MONTH

Page 1465: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

QTR

SEMIYEAR

YEAR

Page 1466: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Datetime DTDAY

DTWEEK

DTWEEKDAYdaysW

Page 1467: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1468: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTTENDAY

Page 1469: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTSEMIMONTH

DTMONTH

DTQTR

Page 1470: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTSEMIYEAR

DTYEAR

DTSECOND

DTMINUTE

Page 1471: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTHOUR

Time SECOND

MINUTE

HOUR

5.5.1TheIntervalCalculation

Page 1472: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Functions:INTCK()andINTNX()TheintervalcalculationfunctionsINTCK()andINTNX()useSASintervaldefinitions.INTCK()countsthenumberofintervalsbetweentwogivendates,times,or

Page 1473: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimes.INTNX()calculatesthedate,time,ordatetimethatresultsafteragivennumberofintervalshavebeenaddedtoaninitialdate,time,ordatetimevalue.

ThesyntaxfortheINTCKfunctionisasfollows.

Page 1474: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTCK(interval,start-of-period,end-of-period,method);intervalistheSASdesignationforaperiodoftime,andcanbeacharacterliteralorcharactervariablethatcorrespondstooneofthedefinedtimeintervals(seeTable5.5).start-of-periodis

Page 1475: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thebeginningdate,time,ordatetimevalue,whileend-of-periodistheendingone.Bothstart-of-periodandend-of-periodcanbeanythingthatevaluatestoavalidSASdate,time,ordatetimevalue.

AsofSASversion9,methoddetermineshow

Page 1476: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASisgoingtocounttheintervals.Therearetwopossiblevalues:CONTINUOUS(orCorCONT)andDISCRETEor(DorDISC).ThedefaultisDISCRETE,andthishasbeenhowintervalshavetraditionallybeencalculatedinSAS.WhenmethodisDISCRETE,theINTCK()

Page 1477: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functioniscountingthenumberoftimesthattheperiodintervalbeginsbetweenstart-of-periodandend-of-period,inclusive.Itdoesnotcountthenumberofcompleteintervalsbetweenstart-of-periodandend-of-period.Thisalsomeansthatthecountdoesnotbeginwithstart-of-

Page 1478: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

period,butatthebeginningofthefirstintervalafterthat.ThefollowingexampledemonstrateshowINTCK()countsusingtheDISCRETEmethod.Forexample,takethedatesSaturday,December31,2011,andSunday,January1,2012.

Page 1479: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.2:HowtheINTCK()FunctionCountsbyDefault(methodIsDISCRETE)

FunctionCall

INTCK('DAY','31dec2011'd,'01jan2012'd);

INTCK('WEEK','31dec2011'd,'01jan2012'd)

INTCK('MONTH','31dec2011'd,'01jan2012'd)

INTCK('YEAR','31dec2011'd,'01jan2012'd)

Page 1480: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Alloftheintervalsareequalto1eventhoughonlyonedayhaspassed!January1,2012,isthestartofday,week,month,andyearintervals.ThestartingdayoccurredonDecember31,andtheendingdaybeganonJanuary1,soitis

Page 1481: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

obviousthattheresultfortheDAYintervalshouldbe1,becausetheDAYintervalboundarywascrossedonJanuary1.However,whenitcomestotheWEEKinterval,Sundayisthebeginningoftheweek.Therefore,theweekcontainingDecember31ststartedonSunday,

Page 1482: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

December25.Sunday,January1,isthebeginningofthenextweek,soyoucrosstheWEEKintervalboundaryatJanuary1.ThismeansthatoneWEEKintervalhaselapsedbetweenthestartoftheweekinDecemberandthestartoftheweekinJanuary,sothatcausestheresult

Page 1483: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

forWEEKtobe1.Similarly,themonthcontainingDecember31startedonDecember1,2011,andthemonthforJanuary1,2012,startedonJanuary1.YouarecrossingtheMONTHintervalboundaryonJanuary1,sooneMONTHintervalhaselapsedbetweenthestartofthe

Page 1484: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalsforthetwodates,andtherefore,thatresultis1aswell.Finally,theyearforDecember31,2011,startedonJanuary1of2011,whiletheyearforJanuary1,2012,startsonthesamedate.YoucrosstheYEARintervalboundaryatJanuary1,whichcausestheYEAR

Page 1485: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalresulttobe1.Allofthevaluesareequalto1becausetheINTCK()functioniscountingtheDAY,WEEK,MONTH,andYEARintervalboundary,whichoccursatSunday,January1,2012,andnotbecauseofthenumberofdays,weeks,months,oryearsthat

Page 1486: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

havepassed.

Nowlet'slookatthecorrespondingresultswhenyouuseCONTINUOUSforthemethod.

Example5.3:HowtheINTCK()FunctionCountsWhenmethodIsCONTINUOUS

Page 1487: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FunctionCall

INTCK('DAY','31dec2011'd,'01jan2012'd,'C');

INTCK('WEEK','31dec2011'd,'01jan2012'd,'C')

INTCK('MONTH','31dec2011'd,'01jan2012'd,'C'

INTCK('YEAR','31dec2011'd,'01jan2012'd,'C')

Onedayhaspassed,asintheDISCRETEexample.However,this

Page 1488: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

looksmorelikewhatyoumightexpectwhenyouaskhowmanyweeks,months,andyearshavepassedbetweenDecember31,2011,andJanuary1,2012.Onlyonedayhaspassed,notanentireweek,month,oryear.TheCONTINUOUSmethodcalculatescontinuoustime,andit

Page 1489: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usesthecalendardefinitionofaweek,month,oryear,startingonthedatesuppliedasthefirstargument.Let'schangetheendingdatesappropriatelytoreproducetheresultsinExample5.2.

Example5.4:HowtheINTCK()FunctionCountsWhenmethod

Page 1490: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IsCONTINUOUS

FunctionCall

INTCK('DAY','31dec2011'd,'01jan2012'd,'C');

INTCK('WEEK','31dec2011'd,'07jan2012'd,'C')

INTCK('MONTH','31dec2011'd,'31jan2012'd,'C'

INTCK('YEAR','31dec2011'd,'31dec2012'd,'C')

Page 1491: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NowthepictureshouldbealittlemoreclearthatusingCONTINUOUScausestheINTCK()functiontolookatcontinuouselapsedtime,notintervalboundaries.TofirmupthisdemonstrationoftheCONTINUOUSmethodfortheINTCK()function,let'slookat

Page 1492: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

howthedefinitionoftheMONTHintervalcanchange.

Example5.5:HowtheINTCK()FunctionCountsWhenmethodIsCONTINUOUS

FunctionCall

A INTCK('MONTH','01jan2012'd,'01feb2012'd,'C');

Page 1493: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B INTCK('MONTH','01jan2012'd,'31jan2012'd,'C');

C INTCK('MONTH','01apr2012'd,'01may2012'd,'C');

D INTCK('MONTH','28feb2012'd,'28mar2012'd,'C');

E INTCK('MONTH','29feb2012'd,'28mar2012'd,'C');

F INTCK('MONTH','28feb2013'd,'28mar2013'd,'C');

RowsAandBlooknormal:31daysisa

Page 1494: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month,while30daysisnotamonth.However,rowCstatesthat30daysisamonth.ThisisonlytrueforthemonthsofApril,June,September,andNovember.Finally,rowsDthroughFdemonstratethat,whenyouareusingtheCONTINUOUSmethod,theMONTHinterval

Page 1495: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

changesbasedonthemonthandtheyear.Inleapyears,aperiodof29daysacrossFebruaryisconsideredamonth(rowD).However,aperiodof28daysisnotconsideredamonthinleapyears(rowE).Ontheotherhand,ifitisnotaleapyear,28daysinFebruaryisamonth.

Page 1496: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheCONTINUOUSmethodisusefulforcalculatinganniversariesandmilestonestiedtodates,times,anddatetimes.Althoughthismethodseemsmuchmoreintuitivethanthetraditional(andstilldefault)waythatSAShandlesintervals,youwillhave

Page 1497: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tobeverycautiousinchoosingwhichmethodyouuseandwhen.Itisnotrecommendedthatyoureplaceallofyourexistingcodetousethismethodinsteadofthedefault,becausethetwomethodsdifferintheirdefinitionofintervalboundaries.TheCONTINUOUSmethodshiftsthe

Page 1498: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalbythestartingdateprovided.WiththeDISCRETEmethod,allvalueswithinanintervalboundaryareconsideredtobeequivalent.Thatmakesitpossibletogroupobservationsoccurringwithinanintervalforanalysis.

Sincethe

Page 1499: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CONTINUOUSmethodismoreintuitivetothewaywetendtolookatperiodsoftime,theremainderofthissectionontheINTCK()functionwillbedevotedtothetraditionalDISCRETEmethod.PleaserememberthattheDISCRETEmethodisthedefault,andin

Page 1500: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ordertousetheCONTINUOUSmethod,youwillhavetosupplythepropermethodargumentintheINTCK()function.Allofthefollowingexamplesrelyonthedefaultmethod,sothereisnomethodargumentusedinthefunctioncalls.

Page 1501: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TocompletethepictureofhowthetraditionaluseoftheINTCK()functionworks,let'slookattheeffectthattheendingdatehasonINTCK().

Example5.6:HowtheINTCK()FunctionCounts

FunctionCall

Page 1502: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTCK('DAY','31dec2014'd,'06jan2015'd);

INTCK('WEEK','31dec2014'd,'06jan2015'd)

INTCK('MONTH','31dec2014'd,'06jan2015'd

INTCK('YEAR','31dec2014'd,'06jan2015'd)

Hereyoucanseethatalthough6dayshaveelapsed,stillonly1week,month,andyear

Page 1503: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

haveelapsedaccordingtoINTCK()!Thatisbecausethestartoftheweek,month,andyearintervalforJanuary6,2015,isstillJanuary1,2015,andthatiswhatINTCK()iscounting.HerearesomemoreexamplesoftheuseoftheINTCK()functionwithitsdefaultoftheDISCRETEmethod.

Page 1504: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.7:TheINTCKFunction:TheBasics

FunctionCall

INTCK('DAY','15jun2013'd,'22jun2013'd);

INTCK('WEEK','01jan2016'd,'01jan2017'd);

INTCK('DTDAY','01oct1872:08:00:00'dt,'20dec1872:18:00:00'dt);

INTCK('MONTH','05mar2000'd,'01may2000'd);

Page 1505: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.8:TheINTCKFunction:CountingBackward

FunctionCall

INTCK('YEAR','22dec2015'd,'16jul2010'd);

Inthisexample,thefromdateisafterthetodate,andtherefore,theanswerisnegative.

Page 1506: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SinceINTCK()countsintervalboundaries,theansweris–5becauseitstartscountingatthestartofayear.ThestartoftheyearintervalforDecember22,2015,isJanuary1,2015,soitisnotcounted.January1,2014;January1,2013;January1,2012;January1,2011;and

Page 1507: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January1,2010,aretheboundariesoftheYEARintervalsthatitiscounting,whichcorrespondstothebeginningdatesofeachyear.

Example5.9:TheINTCKFunction:CountingWeekdays

FunctionCall

Page 1508: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

A INTCK('WEEKDAY17W','12JAN2014'd,'14JAN2014'd);

B INTCK('WEEKDAY17W','13JAN2014'd,'14JAN2014'd);

C INTCK('WEEKDAY17W','17JAN2014'd,'18JAN2014'd);

Ifyouarecountingthenumberofworkweekdaysthathaveelapsed,youmustbecarefultorememberthatINTCK()counts

Page 1509: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalboundariesandthatthestartingdateisnotcountedintheanswer.RowAaboveseemsperfectlyreasonable.YouwouldexpectthattherewouldbetwoweekdaysbetweenSunday,January12,2014,andTuesday,January14,2014.Eventhoughthestartingboundaryfor

Page 1510: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDAY17WisMonday,thestartingdateisSunday,January12,2014,soitcountsMondayandTuesday.However,youmightthinkthattherearetwoweekdaysinthespanMonday,January13,2014,toTuesday,January14,2014.InrowB,theINTCK()functioniscounting

Page 1511: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

only1weekday,becauseitdoesn'tincludeMonday,January13,2014(thestartargumentusedinthefunction),whenitcountstheintervalboundaries.WhyisrowCequaltozerothen?First,Fridayisthestartingday,andthestartingdayisneverincludedinthecount.

Page 1512: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January18,2014,isaSaturday,andsincewe'vedefinedtheweekenddaysasSaturdayandSunday,thosedaysareoutsideofanyintervalboundaries,sobydefinition,nointervalboundarieshavebeenpassed.

Example5.10

Page 1513: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

illustratesthedifferencebetweenthethreemethodsthathavebeendiscussedforcalculatingelapsedyearsusingSAS.WewillusetheYRDIF()functionusingboththe'AGE'basisand'ACT/ACT'basis.SincetheINTCK()functionalsocountselapsedintervals,wewilluseit

Page 1514: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andshowboththeDISCRETEandCONTINUOUSmethods,andcomparethosewithmathematicalestimation.

Example5.10:TheYRDIF()FunctionasOpposedtoMathematical

Page 1515: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

EstimationandINTCK()Thisusesthesamedates,August7,1963,andMay8,2014,forallfivemethods,butrowAusestheYRDIF()functionwith'ACT/ACT,'rowBusesthesamefunction,butwiththe'AGE'basis.Thetwovaluesare

Page 1516: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

equivalentwiththeYRDIF()function,butaswehaveseenearlier,'AGE'and'ACT/ACT'onlyyielddifferentresultswhentheyearbeingtestedisaleapyear.Themathematicalapproximationdividesthenumberofdaysbetweenthetwodatesby365.25,andalthoughthe

Page 1517: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

discrepancyisminuteinthisexample,thedifferenceiscausedbythefactthatthenumberofleapyearsintheperiod(11)isnotevenlydivisibleby4,renderingthevalue365.25anapproximation.TheINTCKfunctionusingtheDISCRETEmethodcountsinterval

Page 1518: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

boundariesfromtheirbeginning,soitiscountingthenumberofJanuaryfirstsbetweenJanuary1,1963,andJanuary1,2015.Ineffect,unlessyouwerebornonJanuary1,usingtheDISCRETEmethodwithINTCK()tocalculateyouragewillmakeyouoldbeforeyourtime!Itis

Page 1519: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

moreappropriateinthistypeofscenariotousetheCONTINUOUSmethodwiththeINTCK()function,whichyieldstheanswer50yearsold.

Calculationmethod

A UsingYRDIF()withactualdaysinmonth,actualyear

B UsingYRDIF()withAGEbasis

Page 1520: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

C Using('08may2014'd-'07aug1963'd)/365.25

D UsingINTCK('YEAR','07aug1963'd,'08may2014'd,'DISCRETE')

E UsingINTCK('YEAR','07aug1963'd,'08may2014'd,'CONTINUOUS')

ThosearesomeexamplesoftheuseoftheINTCK()function.

Page 1521: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Remember,asofSASversion9,theINTCK()functionhasanadditionalargumentthatyoucanusetochangethewaythatINTCKcountsintervalboundaries.However,anyexistingcodeusingINTCK()doesnotneedtobechanged,becausethedefaultmethodisthe'DISCRETE'method,

Page 1522: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichishowINTCK()hasalwaysworked.Onceagain,itisnotagoodideatochangeexistingcodewithoutconsideringhowINTCKisbeingused,becausethemethodsdiffersignificantlyintheircalculations.

Thenextoftheintervalfunctionsthatwewill

Page 1523: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

consideristheINTNX()function.ThisfunctiontakesagivenSASdate,time,ordatetimevalueandcalculatesanewvaluebyincrementingbythegivennumberofintervals.WhereINTCK()calculatesthenumberofintervalsbetweenanytwodate,time,ordatetimevalues,INTNX()takes

Page 1524: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thestartoftheperiodandincrementsitbyanumberofintervalstogivetheendoftheperiod.ThesyntaxoftheINTNX()functionisasfollows:

INTNX(interval,start-from,number-of-increments,alignment);INTNX(interval,start-

Page 1525: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

from,number-of-increments,alignment);isoneoftheSASintervalsdefinedinTable5.5andcanbeacharacterliteralorcharactervariablethatevaluatestooneofthedefinedintervals.start-fromisthestartingdate,time,ordatetimevalue,whichcanbeaconstant,numeric

Page 1526: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variable,oraSASexpression.number-of-incrementsisanintegerconstantoranumericvariablethatindicateshowmanyintervalstoadvance.Ifitisnotaninteger,onlytheintegerportionofthevaluewillbeused.alignmentsetsthereturneddate,time,ordatetimevalue

Page 1527: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

accordingtooneoffourpredefinedsettings.Thefunctioncalculatesthedatesatthebeginningoftheintervalperiod,andthenthealignmentargumentadjuststheresult.Thevaluesare:BeginningorB,MiddleorM,EndorE,andSameorS(Samedayisalsoacceptableasan

Page 1528: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

alias).ThedefaultvalueforalignmentisBeginning.TheSamedayargumentcannotbeusedwiththeDTQTR,DTSEMIYEAR,orDTYEARintervals,andithasnoeffectontimevalues.

Thenextseriesofexamplesdemonstrates

Page 1529: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

varioususesandeffectsoftheINTNX()function.Thefirstexampleisthedefaultuseofthefunctionwitheachofthedate,time,anddatetimeintervals,whilethesecondshowswhathappenswhenyouuseanon-integerastheincrementtothefunction.Example5.14illustratestheuseof

Page 1530: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thealignmentargumentstoyieldspecificdates.

Example5.11:TheINTNX()FunctionwithDefaultAlignmentEachoftheexamplesbelowincrementsthedate,time,ordatetimevalueby3ofthe

Page 1531: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalshowninbolditalics.Fordatesanddatetimes,thestartdateisthesame:Thursday,November6,2014.Theonlydifferenceisthatdatetimeintervals(intervalnamesstartingwithDT)returndatetimevalues,notdates.Theintervalvaluesfordatetime

Page 1532: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesarecalculatedinseconds,notdays.Bydefault,theINTNX()functionreturnsthebeginningoftheintervalgiven.Ifyouwanttochangethis,usethealignmentargumentdiscussedabove.

FunctionCall

Page 1533: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DAY','06NOV2014'd,3)

INTNX('DTDAY','06NOV2014:15:00:00'dt,3)

INTNX('WEEKDAY17W','06NOV2014'd,3)

Page 1534: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1535: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTWEEKDAY17W','06NOV2014:15:00:00'dt,3)

INTNX('WEEK','06NOV2014'd,3)

Page 1536: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTWEEK','06NOV2014:15:00:00'dt,3)

INTNX('TENDAY','06NOV2014'd,3)

Page 1537: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTTENDAY','06NOV2014:15:00:00'dt,3)

Page 1538: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('SEMIMONTH','06NOV2014'd,3)

Page 1539: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTSEMIMONTH','06NOV2014:15:00:00'dt,3)

INTNX('MONTH','06NOV2014'd,3)

Page 1540: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1541: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTMONTH','06NOV2014:15:00:00'dt,3)

INTNX('QTR','06NOV2014'd,3)

Page 1542: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTQTR','06NOV2014:15:00:00'dt,3)

INTNX('SEMIYEAR','06NOV2014'd,3)

Page 1543: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTSEMIYEAR','06NOV2014:15:00:00'dt,3)

INTNX('YEAR','06NOV2014'd,3)

Page 1544: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('DTYEAR','06NOV2014:15:00:00'dt,3)

INTNX('SECOND','8:00:00AM't,3)

INTNX('MINUTE','8:00:00AM't,3)

INTNX('HOUR','8:00:00AM't,3)

Page 1545: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.12:TheINTNX()Function:UsingNon-IntegerIncrements

INTNX('HOUR','16:45't,2.5)

Whenthenumberofincrementsargumentis

Page 1546: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notaninteger,SASwilltaketheintegerpartasthevalue.Inthiscase,2.5becomes2.Thebeginningofthefirsthourincrementis17:00,andthesecondis18:00.SincealignmentisthedefaultvalueofBorbeginning,thatsetstheanswertothebeginningofthehour,

Page 1547: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichgivestheresultof18:00.

Example5.13:CountingBackwardwiththeINTNX()Function

INTNX('DAY','29NOV2014'd,-4)

INTNX()alwaysaddsthenumberofintervalstothestartingdate,

Page 1548: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time,ordatetime.Ifyouwanttofindapriordate,thenuseanegativeincrement.

Example5.14:TheINTNX()FunctionwithAlignmentArguments

INTNXCall

INTNX('WEEK','02NOV2014'd,3,'

Page 1549: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('WEEK','02NOV2014'd,3,'

Page 1550: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('WEEK','02NOV2014'd,3,'

Page 1551: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('WEEK','02NOV2014'd,3,'

Page 1552: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AlthoughthealignmentargumentsdochangethevaluereturnedbytheINTNX()function,itmustbereiteratedthatthefunction

Page 1553: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

performsthecalculationbasedonthebeginningoftheinterval,andtheadjustmenttothefinalresultonlyoccursafterthatfirstcalculationhasbeendone.

5.6ModifyingSASIntervals

Page 1554: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThedefaultSASintervalsdon'tmatcheverysituation.However,therearetwowaystocustomizeSASintervals:YoucanmodifytheexistingSASintervalswithmultipliersandashiftindex,oryoucancreateyourownintervalsfromaSASdataset.Thissection

Page 1555: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willdiscussmultipliersandtheshiftindex.Section5.7willdiscusscreatingyourownintervals.

TheintervalfunctionINTCK(interval,start-of-period,end-of-period,method)countsthenumberofintervalsbetweentwogivenSASdates,times,or

Page 1556: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimes,whiletheINTNX(interval,start-from,number-of-increments,alignment)functionadvancesagivendate,time,ordatetimebyaspecifiednumberofintervals.BothfunctionsusethestandardSASintervaldefinitions.(SeeTable5.6,below.)WiththeDISCRETEmethod,the

Page 1557: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTCK()functioncountsatthestartingpointofagiveninterval,whiletheCONTINUOUSmethodadjuststhestartingpointoftheintervalbasedonthestartingdateandcountsfromthere.Eachmethodhasitsmerits,andwhichoneyouusewilldependonyour

Page 1558: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

analysisorreportingneeds.

TheINTNX()functionalwaysadvancestothebeginningofthegiveninterval.ThealignmentargumentavailablewithINTNX()willadjusttheresulttothemiddle,end,orthesamedayoftheinterval.Nonetheless,

Page 1559: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theintervalsarestillmeasuredfromtheirstartingpoint,andtheadjustmentisonlyappliedafterINTNX()hasmovedthespecifiednumberofintervals.

Thisbehaviorcanleadtoproblemswhenyourdefinitionofanintervaldoesn'texactlymatch

Page 1560: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thestandardSASdefinitionofthatsameinterval.Forexample,thestandardSASdefinitionofyearhasthefirstdayoftheyeardefinedasstartingJanuary1.WhathappensifyourfiscalyearstartsonJuly1,andthatishowyouwanttomeasureyouryear?Whatifyour

Page 1561: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

semi-monthperiodistruly14dayslong?

Ashiftindexandintervalmultipliersenableyoutodothis.Youhavetheabilitytodefinethestartofanintervaldefinitionbyaddingashiftindextoit.Theshiftindexdefinesthenumberofshiftpointstomovethe

Page 1562: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startoftheinterval.Thereisonerestrictiononthevalueoftheshiftindex:Itmustbelessthanthenumberofshiftpointswithintheinterval.Forexample,youcanshiftthestartofaYEARintervalbyupto12months,butnot13,asthereareonly12monthsinayear.Thismakessense

Page 1563: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asifyouweretoshiftayearby13months,youhaveshiftedintothenextYEARinterval,andthisismuchlessintuitivethanadding1yeartothestartingdate(s)thatyousendtotheintervalfunction.

Table5.6showstheSASintervalname,definition,andtheshift

Page 1564: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

incrementperiodforeachone.

Table5.6:SASIntervalsandTheirShiftPoints

Category Interval

Date DAY

WEEK

WEEKDAY<daysW>

Page 1565: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1566: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TENDAY

SEMIMONTH

Page 1567: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MONTH

QTR

SEMIYEAR

YEAR

Datetime DTDAY

Page 1568: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTWEEK

DTWEEKDAY<daysW>

Page 1569: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1570: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTTENDAY

DTSEMIMONTH

DTMONTH

DTQTR

DTSEMIYEAR

Page 1571: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTYEAR

DTSECOND

DTMINUTE

DTHOUR

Time SECOND

MINUTE

Page 1572: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOUR

Forourfirstexample,let'susethestandardcaseofafiscalyearthatstartsonJuly1.Technically,ashiftindexsaysthatyouaremovingthestartoftheintervaltothebeginningofoneofthesubperiodswithinthat

Page 1573: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interval.Yearsaresubdividedintomonths,sothatistheshiftunit.Thestartpointisalwaysincludedinthecountofsubperiodstobeshifted.Theeasiestwayformetorememberthisisthatshiftinganintervalbyonesubperiodisthesameastheunshiftedbase

Page 1574: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interval.Thinkofthisanotherway:whilethestartoftheyearisJanuary1,thebeginningofthefirstmonthintheyearisalsoJanuary1.Ifweuseashiftindexof1withourYEARinterval,weareshiftingtothebeginningofthefirstmonthintheyear.

Page 1575: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Therefore,inordertomoveyourYEARintervalbysevenmonths,youneedtomoveittothebeginningoftheseventhsubperiod.(January1,February1,March1,April1,May1,June1,July1—that'sseven).Theshiftvalueisaddedtotheintervalnameby

Page 1576: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

appendingitwithadecimalpoint.

Therefore,inordertoadvancethestartoftheYEARintervalbysevenmonthstoJuly1,youwouldusetheinterval"YEAR.7,"asillustratedbythefollowing:

Example5.15:

Page 1577: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MovingtheStartofaYearIntervalfromJanuary1toJuly1 SampleFunctionCall

A INTCK('YEAR','01jan2013'd,'06jul2014'd,'DISCRETE');

B INTCK('YEAR','01jan2013'd,'06jul2014'd,'CONTINUOUS');

Page 1578: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

C INTCK('YEAR.7','01jan2013'd,'06jul2014'd,'DISCRETE');

D INTCK('YEAR.7','01jan2013'd,'06jul2014'd,'CONTINUOUS');

INTCK()countsthestartofintervalboundarieswiththeDISCRETEmethod(rowsAandC).Whentheintervalis"YEAR,"itismeasuringfromJanuary1,2013,until

Page 1579: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January1,2014(thestartoftheyearcontainingJuly6,2014).Whentheintervalis"YEAR.7,"youhaveshiftedthebeginningoftheYEARintervalby7months,whichdeclaresthattheyearstartsonJuly1.Intheexampleabove,thatshiftcausesthemeasurementtobegin

Page 1580: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

onJuly1,2012(thestartoftheshifted"year"containingJanuary1,2013)andendonJuly1,2014,whichisthestartoftheyearcontainingJuly6,2004.ThatiswhythevaluereturnedinrowCis2,not1.WiththeCONTINUOUSmethod,theshiftedyearstartsonJuly1,2013,sothe

Page 1581: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startdateisbeforethestartofthemeasurement.Therefore,theonlyintervalboundarycrossedisonJuly1,2014,sotheanswerinrowDis1.

WhileashiftindexenablesyoutomovethestartingpointofanygivenSASinterval,

Page 1582: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anintervalmultiplierenablesyoutodefinethelengthofyourownintervals.Youdefineyournewintervalbyapplyingamultipliervaluetoaninterval.Forexample,ifyouwanttomeasurebiweekly(14-day)periods,youwouldtaketheWEEKintervalandmultiplyitby2.

Page 1583: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YouaddthenumericmultipliertotheendoftheSASintervalname,sointhiscaseyourintervalnamewouldbe"WEEK2."Itmeasures14-dayperiodsstartingonSundaybecausetheregularSASintervalWEEKstartsonSunday.Example5.16demonstratesthecreationanduseofa

Page 1584: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

custominterval,byusingtheexampleofamultiplierof2fortheWEEKinterval.Italsoillustratessomeofthefeaturesofcustomintervals.

Example5.16:UsinganInterval

Page 1585: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MultipliertoCreateaCustomIntervalSampleFunctionCall

INTNX('WEEK','09feb2014'd,2,'S');

INTNX('WEEK2','09feb2014'd,2,'S');

Page 1586: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Intheaboveexample,thestartoftheWEEKintervaltwoweeksfromFebruary9isFebruary23,soitmakessensethatthestartofthesecondbiweeklyperiodfromFebruary9isMarch9,

Page 1587: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

becauseFebruary2014has28days,whichisthesameastwoofyourWEEK2intervals.Thisisnotassimpleasitseems.WhenusingSASintervalswiththeINTNX()function,youmustkeepinmindthatintervalsarealwaysmeasuredfromthebeginningofthestartingintervaltothe

Page 1588: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginningoftheendinginterval.Thisisindependentofthestartingandendingdatesthatyousupply.Let'smovethestartingdateinExample5.16backwardbyoneweektoFebruary2,2014.

SampleFunctionCall

1 INTNX('WEEK','02feb2014'd,2,'S');

Page 1589: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2 INTNX('WEEK2','02feb2014'd,2,'S');

AdvancingbytwoWEEKS(row1)isstilladifferenceof14days,asexpected.What

Page 1590: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

happenedinrow2?Shouldn'tyouget28daysinsteadof21?No,becausethestartoftheWEEK2intervalcontainingFebruary2,2014,isJanuary24,2014,andthatiswheretheINTNX()functionbeginsitscount.Hereisasampleprogramtoproducethestartingdatesoftheintervals:

Page 1591: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.17:WEEK2IntervalsfromJanuary1,2014

DATAtricky;DOintervals=0TO5;interval_start=INTNX('WEEK2','01jan2014'd,intervals);OUTPUT;END;RUN;

PROCREPORTDATA=tricky

Page 1592: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NOWDSPLIT='\';COLUMNSintervalsinterval_start;DEFINEintervals/DISPLAY"NumberofWEEK2\intervalsfrom\January1,2014"STYLE={CELLWIDTH=1.25inTEXT_ALIGN=C};DEFINEinterval_start/FORMAT=weekdate32."StartingDateofInterval";RUN;

Page 1593: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheaboveprogramcreatesatablethatshowsthestartingdatesforthefirstfiveWEEK2intervalsof2014.Ifthedate(s)suppliedfallbetweenthestartingdatesofanytwoboundaries,theintervalcount(orincrementing)willbeginfromthestartingdateoftheprevious

Page 1594: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interval.ThisiswhymovingthestartingdateoftheINTNXfunctionbyoneweekdoesn'tnecessarilymovetheresultbyoneweek.Youcanmoveupto14dayswithinaWEEK2intervalbeforeyouchangetheintervalboundary.

Page 1595: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NumberofWEEK2intervalsfromJanuary1,2014

StartingDateofInterval

0 Sunday,December29,2013

Page 1596: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

29,2013

1 Sunday,January12,

2014

2 Sunday,January26,

2014

3 Sunday,February9,

2014

Page 1597: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014

4 Sunday,February23,

2014

5 Sunday,March9,2014

HowdoesSASdeterminewhatthe

Page 1598: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startingdateofagivenintervalisifyouuseamultiplier?Ittakesthemultipliedintervalthatyou'vecreatedandstartscountingbeginningwithJanuary1,1960.ThisistrueforallmultipliedintervalsexceptmultipliedWEEKintervals.MultipliedWEEKintervalsarecounted

Page 1599: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startingfromSunday,December27,1959,becauseweeksaredefinedasstartingonSundays,andJanuary1,1960,wasaFriday.

Youcanalsouseamultiplierandashiftindicatortogetherifneeded.Intervalmultipliersaredirectlyappendedtothe

Page 1600: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalname(forexample,WEEK2),andshiftindicatorsareappendedtotheintervalnamewithaleadingdecimalpoint(forexample,WEEK.5).Touseboththemultiplierandashiftindex,youfirstappendthemultipliertotheintervalnametocreateanewintervalname

Page 1601: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(forexample,WEEK2),andthenyouappendtheshiftindicatortotheintervalname.Givenamultiplierof2,andashiftof5,theintervalnamebecomes"WEEK2.5."Again,youcannotuseashiftindexthatisgreaterthanthenumberofshiftpointsintheinterval.InthecaseofaWEEK2

Page 1602: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interval,youwouldnotbeabletouseashiftindexgreaterthan14,asthatwouldplacethestartingpointofyourintervalintothenextWEEK2interval.Inthatcase,itwouldbemoreintuitivetochangethestartingdate(s)thatyouwillsupplytotheintervalfunction.

Page 1603: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Todemonstrate,let'sexpandonthescenariousedinexample5.17.WhatifyouwantedyourbiweeklyperiodstostartonJanuary1,2014?January1,2014,wasaWednesday,soyouwanttomovethestartingdatetothefourthdayoftheweek(Sunday=1,therefore,Wednesday=4).Now

Page 1604: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

we'llgeneratethesametableasabove,usingashiftindicatorof4daysinadditiontothebiweeklyintervalofWEEK2.

Example5.18:UsingBothanIntervalMultiplierand

Page 1605: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ShiftIndextoCreateaCustomInterval

DATAtricky;DOintervals=0TO5;plain_interval_start=INTNX('WEEK2','01jan2014'd,intervals);shifted_interval_start=INTNX('WEEK2.4','01jan2014'd,intervals);OUTPUT;END;RUN;

Page 1606: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCREPORTDATA=trickyNOWDSPLIT='\';COLUMNSintervalsplain_interval_startshifted_interval_start;DEFINEintervals/"NumberofWEEK2\intervalsfrom\January1,2014"STYLE={CELLWIDTH=1.25inTEXT_ALIGN=C}DISPLAY;DEFINEplain_interval_start/."StartingDateofWEEK2Interval"FORMAT=weekdate32.;

Page 1607: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DEFINEshifted_interval_start/"StartingDateofWEEK2.4Interval"FORMAT=weekdate32.;RUN;

NumberofWEEK2Intervalsfrom

StartingDateof

StartingDateof

Page 1608: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January1,2014

WEEK2Interval

WEEK2.4Interval

0 Sunday,December29,2013

Wednesday,January1,

1 Sunday,January12,2014

Wednesday,January15,

2 Sunday,January

Wednesday,January29,

Page 1609: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January

26,2014

January29,

3 Sunday,February9,2014

Wednesday,

4 Sunday,February23,2014

Wednesday,

5 Sunday,March9,

Wednesday,March12,

Page 1610: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014

Whenyouuseamultiplier,youalsohavetheabilitytodefineyourshiftswithintheentireintervalcreatedbythemultiplier.Forexample,let'screateadecadeintervalby

Page 1611: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usingYEAR10astheinterval.Rememberthatintervalsstartatthebeginningoftheboundary,sothedecadeswouldstartatthebeginningofthefirstyearofthedecade.WhatcanyoudoifyouwanttodefinethedecadeasstartinginMayofthemiddleyearinthedecade(for

Page 1612: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

example,Mayof1955asopposedtoJanuaryof1950)?

Toshiftintervalsacrossyears,youneedtousethefirstnestedintervalwithintheYEARinterval,whichisMONTH.Soyouwoulduse(numberofyearstoshift*12)tocalculatethenumberofmonths

Page 1613: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatyouneedtoshift.Ifyouwantedtoshift5years,youwoulduse5*12=60.Add5tothat,whichshiftsthestartingmonthfromJanuarytoMay,andyourintervaldefinitionisnowYEAR10.65.ThatwouldbedecadesstartinginMayofyearsthatendin5.Thecodebelowshowstheeffect

Page 1614: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofmovingtheintervalby65months.

Example5.19:ShiftingaMultipliedInterval

DATAtricky3;DOintervals=0TO7;plain_interval_start=INTNX('YEAR10','01sep1950'd,intervals);shifted_interval_start

Page 1615: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

=INTNX('YEAR10.65','01sep1950'd,intervals);OUTPUT;END;RUN;

odsrtffile="c:\book\2ndEd\examples\ex5.6.5.rtf";

PROCREPORTDATA=tricky3NOWDSPLIT='\';COLUMNSintervalsplain_interval_startshifted_interval_start;DEFINEintervals/DISPLAY"Numberof\intervals

Page 1616: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

from\September1,1950"STYLE={CELLWIDTH=1inTEXT_ALIGN=C};DEFINEplain_interval_start/FORMAT=weekdate32."StartingDateofYEAR10Interval";DEFINEshifted_interval_start/FORMAT=weekdate32."StartingDateofShiftedYEAR10Interval";RUN;

Page 1617: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NumberofIntervalsfromSeptember1,1950

StartingDateofYEAR10Interval

0 Sunday,January1,

1950

1 Friday,January1,

Page 1618: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

January1,

1960

2 Thursday,January1,

1970

3 Tuesday,January1,

1980

4 Monday,January1,

Page 1619: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1990

5 Saturday,January1,

2000

6 Friday,January1,

2010

7 Wednesday,January1,

Page 1620: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2020

ThefirstthingtonoteisthateventhoughthedatethatwespecifiedisSeptember1,thestartingdateoftheintervalisJanuary1,becausethatisthestartoftheYEARinterval.Inthesecondcolumn,youcanseethatthe

Page 1621: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalhasbeenshifted.EventhoughthestartingdateoftheYEAR10.intervalisJanuary1,1950,theshiftedintervalitselfstartsonTuesday,May1,1945(bolditalicsadded),notMay1,1955.Why?BecauseitisthestartoftheintervalthatcontainsJanuary1,1950.

Page 1622: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thenextexamplewilldemonstratetheimportanceofmakingsurethatyouusethecorrectshiftindexandthatyouhavetobeawareofthestartingdateand/ordatetimeforyourshifted,multipliedinterval.

Example5.20:

Page 1623: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheInteractionbetweentheStartingDateandtheIntervalStartingPoint:The"WorkingShift"IntervalAcompanyhasa24-hourproductioncyclethatconsistsofthree8-

Page 1624: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hourworkingshiftsperday.ItiseasytocreateanintervalthatrepresentseachshiftbyusingHOUR8asyourbaseinterval,butshiftsdonotstartatmidnight,8a.m.,and4p.m.,whichwouldbethedefaultfortheHOUR8interval.ThetablebelowshowswhentheHOUR8

Page 1625: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalstartsbasedonthedefault.

FunctionCall

INTNX('HOUR8','00:00't,0)

INTNX('HOUR8','00:00't,1)

INTNX('HOUR8','00:00't,2)

Page 1626: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

However,inthiscase,theactualshiftsstartat6a.m.,2p.m.,and10p.m.,sowewillneedtouseashiftindextoalignourHOUR8intervalwiththeactualshifthours.Itisimportanttorememberthatwhenyouare

Page 1627: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftingintervals,ashiftindexof1movesthestartoftheintervaltothestartofthefirstsubperiodwithintheinterval—whichisalwaysthesameasthestartoftheunshiftedinterval.Lookatthefollowingtable,whichwilltelluswhatintervalshiftwillgiveusthestartingpointat

Page 1628: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6a.m.

FunctionCall

INTNX('HOUR8','6:00't,0);

INTNX(HOUR8.1,'6:00't,0)

Page 1629: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX(HOUR8.2,'6:00't,0)

INTNX(HOUR8.3,'6:00't,0)

INTNX(HOUR8.4,'6:00't,0)

INTNX(HOUR8.5,'6:00't,0)

Page 1630: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX(HOUR8.6,'6:00't,0)

INTNX(HOUR8.7,'6:00't,0)

Noticethatintheabovetable,we'veused6a.m.asourreferencetimeintheINTNX()

Page 1631: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functioncall,andwegetthestartingpointoftheintervalbyadding0intervalstoourreferencepoint.Thismethodworksforfindingthestartingpointofanyintervalinrelationtothereferencepoint.Nowwe'llrepeatourfirsttable,showingthestartingpointsfora24-

Page 1632: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hourperiodstartingat6a.m.,withourHOUR8.7intervaltodemonstratethateverythingiscorrect.

FunctionCall

INTNX('HOUR8.7','06:00't,0)

Page 1633: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX('HOUR8.7','06:00't,1)

INTNX('HOUR8.7','06:00't,2)

Althoughitmightseemoddthatashiftindexof7isneededtogetachangeof6hours,youmustrememberthat

Page 1634: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theshiftindexisthenumberofsubperiodswithintheintervaltomove.Theshiftisalwaystothebeginningofeachsubperiodwithintheinterval.Therefore,thefirstsubperiodwithinanintervalalwaysstartsatthebeginningoftheinterval.Inthiscase,theHOUR8interval

Page 1635: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

startsatmidnight,andthefirsthourwithintheHOUR8intervalalsostartsatmidnight.Thatiswhytheshiftindexnecessaryis7,not6.

Insummary,themostimportantthingtorememberaboutusingintervals,multipliers,andtheshiftindexis

Page 1636: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatallintervals,nomatterhowtheyaredefined,aremeasuredfromtheirbeginning.IfyouareusingtheINTCK()function,thedefinitionof"beginning"isdependentuponthemethodthatyouuse,DISCRETEorCONTINUOUS.WithDISCRETE,itis

Page 1637: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

consideredtobethebeginningoftheintervalboundary,butwithCONTINUOUS,beginningisdefinedbasedonthestartdateprovided.TheINTNX()functionmeasuresthebeginningfromtheintervalboundary;ifitiseasiertothinkofINTNXasalwaysusingtheDISCRETEmethod,

Page 1638: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thenyoucandothat.However,rememberthatthealignmentarguments"B","M","E",and"S"fortheINTNX()functiondonotadjustthedateuntilafterthefunctionhasexecutedandcalculateditsstart-of-intervalresult(alsorememberthatthealignmentargumentsonlyworkwiththe

Page 1639: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX()function).Nomatterwhat,youcanusetheintervalmultipliersandashiftindextomovethestartingpointofaninterval,andyoucanusethemanywherethatyoucanuseadate,time,ordatetimeinterval.

Page 1640: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.7CreatingYourOwnSASIntervalsAnotherwaytocreateintervalsforusewithintervalfunctionsisbycreatingaSASdatasetthatdefinestheboundariesofyourinterval.Thisdatasetdefinesthedurationofperiodswithintheintervalandtheperiod

Page 1641: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

acrosswhichtheintervalappliesaswell,whichisanimportantconceptthatwillbediscussedwhenitcomestoerrorswithusinguser-definedintervals.User-definedintervalsenableyoutocreateintervalsthatdonotcorrespondwithshiftedand/ormultipliedSAS

Page 1642: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervals,suchasintervalswithirregularboundaries,thosethatdonotencompassanentirepredefinedSASinterval,orintervalsthatarespecifictoyourjob,company,orindustry.However,ifyouneededtocountworkingshiftsinasituationwheretherearetwo10-hourwork

Page 1643: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftsperdaywithafour-hourbreakin-betweenwhilethemachineryresets,youcouldnotdoitbyusingmultipliersandashiftindex.

Youcanalsoredefineaninterval.Forexample,theshiftpointforadayistheday;whathappensifyou

Page 1644: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wantyour"day"tostartat6a.m.insteadofmidnight?DTHOUR24.7wouldcreateanintervalthatruns24hoursstartingat6a.m.(Seeexample5.20,whichdoesthesameforanHOUR8interval.Theonlydifferenceisthatthereare24-hour-longsubperiodsina

Page 1645: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DTHOUR24interval.)Unfortunately,theoriginalquestionwastoshiftaDAYintervalby6hours,butyoucan'tdothatwithadatevalue.DTHOURintervalsonlyapplytodatetimevalues.Youwouldhavetoconvertallofyourdatedatatodatetimedatainordertousethatinterval.

Page 1646: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Definingyourownintervalwheredaysstartatthedesiredtimewouldsolvethisproblem.

TheSASdatasetyouhavetocreateconsistsofatleastonevariablenamedbegin.Itcanalsohavetwootheroptionalvariables,endandseason.Ifyoudo

Page 1647: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notincludetheendvariable,itisassumedtobeonelessthanthestartofthenextperiodwithinthatinterval;forexample,ifyouruser-definedintervalisbasedondays,theendwillbethedaybeforethebeginvalueforthenextrecord.Whileseasonalityisatimeseriesconceptoutside

Page 1648: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthescopeofthisbook,itcanbeaccountedforwiththevariableseason.Oneadditionalrequirementisthatbegin(andend,ifpresentinthedataset)musthaveanappropriatedate,time,ordatetimeformatassignedatthetimethatthedatasetiscreated.Inorderto

Page 1649: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

associatethisdatasetwithauser-definedinterval,youneedtousetheINTERVALDSsystemoption.

OPTIONSINTERVALDS=(interval-name1,dataset-with-interval-records1,interval-name2,dataset-with-interval-records2…)

Page 1650: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thisiswhereyounameyouruser-definedintervalandtellSASwhichdatasettouseforthatinterval.Youcanspecifyasmanyuser-definedintervalsasyouneedinasingleOPTIONSINTERVALDSstatement.interval-namemustconformtostandardSASnamingconventions,andit

Page 1651: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

cannotbeaSASreservedword.Ifyoutrytouseareservedword,youwillgetanerrorattheOPTIONSstatement.ThisappliestonamesofexistingintervalsdefinedbySASaswell.YoucannotchangethedefinitionofanexistingSASintervalbycreatingoneofyour

Page 1652: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ownwiththesamename.dataset-with-interval-recordsisastandardSASdatasetreferenceofthetypelibref.dataset-name.Sincethisstatementmerelyidentifiesadatasetforassociationwithyourintervalname,datasetoptionsarenotallowed.

Page 1653: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Forourfirstexample,let'sdefineaSEMESTERinterval,basedonanacademiccalendarwithtwosemestersandasummersession.Thesessionsaredefinedasfollows:ThefallsemesterbeginsthedayfollowingthefirstMondayinSeptemberandendsonthethird

Page 1654: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FridayinDecember.ThespringsemesterbeginsonthesecondMondayinJanuary,unlessthatdayfallsontheMartinLutherKing,Jr.,holiday.Inthatcase,itbeginsonthedayfollowingtheholiday.ThesummersessionstartsonthelastTuesdayinMayandproceedsthrough

Page 1655: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thethirdFridayinAugust.ThefollowingexampleusesboththeNWKDOM()andHOLIDAY()functionsdescribedinsections5.3and5.4,respectively.

Example5.21:AcademicCalendar:

Page 1656: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CreatingaSEMESTERInterval

OPTIONSINTERVALDS=(semester=semester);❶

DATAsemester;

DOyear=2014TO2017;❷

/*FallSemester*/begin=NWKDOM(5,2,8,year);/*LastMondayinAugust*/

Page 1657: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

end=NWKDOM(3,6,12,year);/*ThirdFridayinDecember*/OUTPUT;

/*SpringSemester*/begin=NWKDOM(2,2,1,year+1);/*SecondMondayinJanuaryoffollowingcalendaryear*/ifbegineqHOLIDAY('MLK',year+1)then/*IfMLKday,thenTuesday*/

Page 1658: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

begin=begin+1;end=NWKDOM(2,6,5,year+1);/*SecondFridayinMay*/OUTPUT;

/*SummerSession*/begin=NWKDOM(1,2,6,year+1);/*FirstMondayinJune*/end=NWKDOM(3,6,8,year+1);/*ThirdFridayinAugust*/OUTPUT;END;FORMATbeginend

Page 1659: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKDATE.;/*Required*/RUN;

❶tellsSASthatwearedefininganintervalnamed"semester,"anditistobebasedonthedatasetWORK.SEMESTER.

❷setsadefinitelimitontheinterval,fromthestartofthe2014academicyear

Page 1660: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

throughthe2017academicyear,whichendsinAugust2018.Belowistheresultingdataset:

begin end

Monday,August25,

2014

Friday,December19,2014

Page 1661: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Monday,January12,

2015

Friday,May8,2015

Monday,June1,2015

Friday,August14,

2015

Monday,August31,

2015

Friday,December18,2015

Page 1662: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Monday,January11,

2016

Friday,May13,2016

Monday,June6,2016

Friday,August12,

2016

Monday,August29,

2016

Friday,December16,2016

Page 1663: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Monday,January9,

2017

Friday,May12,2017

Monday,June5,2017

Friday,August11,

2017

Monday,August28,

2017

Friday,December15,2017

Page 1664: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Monday,January8,

2018

Friday,May11,2018

Monday,June4,2018

Friday,August10,

2018

YoucanthenusetheSEMESTERintervalthatyouhavecreated

Page 1665: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intheINTCKorINTNXintervalfunctions.ThefollowingtableshowshowmanysemestershaveelapsedfromSeptember15,2014,totherespectiveenddatesshown.

Example5.22:UsingaUser-DefinedInterval

Page 1666: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

withINTCKFunctionCall

INTCK('SEMESTER',"15SEP2014"d,"22JUN2015"d);

INTCK('SEMESTER',"15SEP2014"d,"24NOV2016"d);

INTCK('SEMESTER',"15SEP2014"d,"01MAY2018"d);

Forasecondexample,let'sconsideracasewhereweneedto

Page 1667: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

counttheactualnumberofworkingdaysforeachyear.TheWEEKDAYintervalisausefulapproximation,butitdoesn'taccountforholidays,soifyouneedtoknowtheexactnumberofworkingdaysbetweentwodates,thiswillsolvethatproblem.

Page 1668: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.23:CustomizedCompanyWorkingDays

OPTIONSINTERVALDS=(WorkingDays=Workdays);DATAworkdays(KEEP=begin);/*Endvariablenotneeded*/start='01JAN2014'D;stop='31DEC2015'D;nweekdays=INTCK('WEEKDAY',start,stop);

Page 1669: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

/*BasedonMon-Friweekdays*/DOi=0TOnweekdays;begin=INTNX('WEEKDAY',start,i);year=YEAR(begin);

/*Company-specificholidays*/

/*Companyclosesdayafterthanksgiving*/xthanks=HOLIDAY("THANKSGIVING",year)+1;/*ChristmasEve*/xmaseve=

Page 1670: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HOLIDAY('CHRISTMAS',year)-1;/*FridaybeforeEaster*/sprng=HOLIDAY("EASTER",year)-2;/*FoundersDayCompanyHoliday.Ifonweekend,moveforwardorback*/

founders=MDY(8,6,year);SELECT(WEEKDAY(founders));WHEN(6)founders=founders-1;WHEN(1)founders

Page 1671: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

=founders+1;OTHERWISEfounders=founders;END;

/*Excludedatesofstandardandcompanyholidaysfromintervaldataset*/IFBEGINneHOLIDAY("NEWYEAR",year)ANDBEGINneHOLIDAY("MLK",year)ANDBEGINneHOLIDAY("USPRESIDENTS",year)AND

Page 1672: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

BEGINneHOLIDAY("MEMORIAL",year)ANDBEGINneHOLIDAY("USINDEPENDENCE",year)ANDBEGINneHOLIDAY("LABOR",year)ANDBEGINneHOLIDAY("VETERANS",year)ANDBEGINneHOLIDAY("THANKSGIVING",year)ANDBEGINneHOLIDAY("CHRISTMAS",year)AND

Page 1673: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

BEGINnexmaseveANDBEGINnexthanksANDBEGINnesprngANDBEGINnefoundersTHENOUTPUT;END;FORMATbeginDATE9.;RUN;

Thefollowingcodegeneratesatableshowingthedifference

Page 1674: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenthenumberofcalendardays,thenumberofweekdays,andthenumberofworkingdaysforthiscompanyforthecalendaryear2014.

DATACountDays;start='01JAN2014'D;stop='01JAN2015'D;ActualDays=INTCK('DAYS',start,stop);Weekdays=

Page 1675: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTCK('WEEKDAYS',start,stop);ProductionDays=INTCK('WORKINGDAYS',start,stop);LABELActualDays="ActualDays"Weekdays="Weekdays"ProductionDays="ProductionDays"start="StartingDate"end="EndingDate";FORMATstartstopDATE9.;RUN;

Page 1676: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCPRINTDATA=CountDaysNOOBS;RUN;

start stop

01JAN2014 01JAN2015

Howmuchofadifferencecanthismake?Considerthatthiscompanyhassixproductlinesandthe

Page 1677: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timetomanufactureeachproductvaries.Acustomercomesinwithalargeorderandwantstoknowwhatthedeliverydatewouldbeforeachproduct.Itiscriticalthatthedeliverybeguaranteedtotheday,sothecontractcallsforasubstantialdiscountifthedeliverydateisnot

Page 1678: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

met.Inthefollowingtable,theorderdateisJune27,2014.WeusetheINTNX()functiontoaddthecorrespondingnumberofproductiondaysforeachproducttotheorderdate.TheapproximatedeliverydateiscalculatedusingtheSASWEEKDAYinterval,whilethetrue

Page 1679: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

deliverydateusesouruser-definedWORKINGDAYSinterval.

DATAproduction_lines;LENGTHproduct$40;orderDate="27JUN2014"d;Product="StdProduct1";days_from_order=23;OUTPUT;Product="StdProduct2";

Page 1680: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

days_from_order=32;OUTPUT;Product="StdProduct3";days_from_order=35;OUTPUT;Product="CustomProduct1";days_from_order=33;OUTPUT;Product="CustomProduct2";days_from_order=42;OUTPUT;Product="CustomProduct3";

Page 1681: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

days_from_order=56;OUTPUT;FORMATorderdateweekdate.;RUN;

DATAordertime;SETproduction_lines;deliveryDate=INTNX('WORKINGDAYS',orderdate,days_from_order,'S');approx_deliveryDate=INTNX('WEEKDAY',orderdate,days_from_order,'S');FORMATdeliverydateapprox_deliveryDateweekdate.;RUN;

Page 1682: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCREPORTDATA=ordertimeNOWDSPLIT='\';COLUMNSproductdays_from_orderapprox_deliveryDatedeliveryDate;DEFINEproduct/"Product";DEFINEdays_from_order/"ProductionDays\Required"STYLE={TEXT_ALIGN=C};DEFINEapprox_deliveryDate/STYLE={TEXT_ALIGN=L}

Page 1683: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

"~S={text_align=c}ApproximateDeliveryDate\usingWEEKDAYSInterval";DEFINEdeliveryDate/"~S={TEXT_ALIGN=C}ActualDeliveryDate\usingCustom\WORKINGDAYSInterval"STYLE={TEXT_ALIGN=L};RUN;

ApproximateDelivery

Page 1684: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Product

ProductionDaysRequired

DateUsingWEEKDAYSInterval

StdProduct1

23July30,2014

StdProduct2

32

Page 1685: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

StdProduct3

35 Friday,August

CustomProduct1

33

CustomProduct2

42

Page 1686: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2

CustomProduct3

56September15,

Thereisonespecificproblemtobeawareofwhenyouareworkingwithuser-definedintervals.Ifaspecific

Page 1687: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date,time,ordatetimeisnotcoveredintheintervaldataset,andyoutrytouseoneofthesevaluesinafunction,oroneofthesevaluesistobereturnedbyafunction,youwillnotgettheanswerthatyouexpect.Suchunexpectedresultscanbeavoidedbyensuringthatthe

Page 1688: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datesthatyoudefineinthedatasetencompassallthedatesthatyoumayencounter.Thenexttwoexampleswilldemonstratewhathappenswhenyouuseadatethatisoutoftherangeofyouruser-definedinterval.WewillusetheWORKINGDAYSintervalasdefinedin

Page 1689: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thepreviousexample.Thatintervalisonlydefinedforaperiodof2years,fromJanuary1,2014,throughDecember31,2015.WhathappensifyoutrytocalculatethenumberofworkingdaysusingtheINTCK()functionwithendingdatesoutsideofthatrange,oryoutryto

Page 1690: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

projectadateusingtheINTNX()functionthatwouldbebeyondthatrange?

Example5.24:Out-of-IntervalCalculationUsingINTCK()and

Page 1691: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

"WORKINGDAYS"CustomIntervalObs startDate endDate

1 Wednesday,December23,2015

Thursday,December24,2015

2 Wednesday,December

Friday,December

Page 1692: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

23,2015 25,2015

3 Wednesday,December23,2015

Saturday,December26,2015

4 Wednesday,December23,2015

Sunday,December27,2015

5 Wednesday,December

Monday,December

Page 1693: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

23,2015 28,2015

6 Wednesday,December23,2015

Tuesday,December29,2015

7 Wednesday,December23,2015

Wednesday,December30,2015

8 Wednesday,December

Thursday,December

Page 1694: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

23,2015 31,2015

9 Wednesday,December23,2015

Friday,January1,

10 Wednesday,December23,2015

Saturday,January2,

11 Wednesday,December

Sunday,January3,

Page 1695: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

23,2015

12 Wednesday,December23,2015

Monday,January4,

13 Wednesday,December23,2015

Tuesday,January5,

14 Wednesday,December

Wednesday,January6,

Page 1696: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

23,2015

15 Wednesday,December23,2015

Thursday,January7,

Rows1through4makesense.TherearenoworkingdaysfromThursdaythroughSunday,asDecember24isacompany

Page 1697: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

holiday.Rows5through8countaworkingdayforMondaythroughThursdayasyouwouldexpect.Friday,January1,2016,isalsoacompanyholiday.Therefore,youwouldnotexpectittocountasaworkingday,norwouldyouexpectthefollowingSaturdayand

Page 1698: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Sundaytocountasworkdays.ButwhataboutthatfirstfullweekinJanuary,rows12through15?Thosedaysaren'tcompanyholidays,sowhyaren'ttheybeingcountedasworkingdays?

Theansweristhattheyaren'tpartoftheWORKINGDAYS

Page 1699: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interval.ThedefinitionoftheintervalendedonDecember31,2015.Becausetheyarenotinthedataset,anydatesbeforeorafterthedatesspecifiedinthedatasetwillnotbeconsideredworkingdays.Thismakessensewhenyouconsiderhowweinitiallycreatedthecompanyholidays.We

Page 1700: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

didnotincludethosedatesinthedatasetthatcreatedtheinterval.Therefore,thisproblemactuallystartswithJanuary1,2016.ItismerelycoincidentalthatJanuary1stisaholiday,andthe2ndand3rdareweekenddays;thatisnotwhatispreventingthemfrom

Page 1701: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beingcountedasworkingdays.Theirabsencefromthedatasetweusedtocreatetheintervalistheonlyreasonthattheyarenotbeingcounted.ThisnextexampleshowsthetypeofproblemthatcanarisewhenusingtheINTNX()functioninasimilarfashion.

Page 1702: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example5.25:Out-of-IntervalCalculationwithINTNX()and"WORKINGDAYS"CustomIntervalObs

1 INTNX('workingdays','26dec2015'd,1,'S');

Page 1703: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2 INTNX('workingdays','26dec2015'd,2,'S');

3 INTNX('workingdays','26dec2015'd,3,'S');

4 INTNX('workingdays','26dec2015'd,4,'S');

Page 1704: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5 INTNX('workingdays','26dec2015'd,5,'S');

6 INTNX('workingdays','26dec2015'd,6,'S');

7 INTNX('workingdays','26dec2015'd,7,'S');

8 INTNX('workingdays','26dec2015'd,8,'S');

Whathappenedhere?Rows1through4counttheworkingdaysasexpected,Monday

Page 1705: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

throughThursday.However,ifyoutrytoincrementthestartingdatebymorethan4workingdays,thefunctioncallreturnsamissingvalue.SAScannotdothecalculationbecausetheresultisundefined;itranoutofpossibleresultstoreturnatDecember31,2015,

Page 1706: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

becauseyoudidnotprovideanyinthedataset.

TheabilitytodefineyourownintervalswillcoversituationswhenthecombinationofstandardSASintervals,amultiplier,andashiftindexdoesnotsuffice.Itisnotdifficulttocreatethedataset

Page 1707: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

uponwhichSASwillbaseyouruser-definedinterval,butbeawarethatdatesthatarenotincludedintheintervalcancauseproblemswhenyoutrytousetheintervalsthatyoucreate.Ifyouseeunexpectedresultsfromusingyouruser-definedinterval,thefirstthingto

Page 1708: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

investigateistomakesurethattherangethatyouhavedefinedforyourintervalencompassestherangeofpossibleresults.

5.8IntervalFunctionsaboutIntervals

Page 1709: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAShasaseriesoffunctionsthatprovideinformationaboutintervalsthatarebeingused.Theycantellyouwhichintervalisthebestfitbetweentwoorthreedates,orsuggestaformatforagiveninterval.Thereisafunctiontotellyoutheshiftpointineffectforagiveninterval.(For

Page 1710: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

basicintervals,thisreturnsthevaluesinTable5.5,butintervalmultiplierscanchangethereturnedvalue.)Thereisalsoafunctionthatwilltellyouiftheintervalnamethatyouareusingisvalid.Thiscanallowforincreasedautomationandbuildingrobustapplications.

Page 1711: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.8.1INTFIT(argument-1,argument-2,type)INTFIT(argument-1,argument-2,type)takestwodate,datetime,orobservationnumbersasthefirsttwoargumentsandreturnstheintervalbetweenthemdeemed

Page 1712: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tobe"thebestfit."Thisfunctionassumesthatthealignmentis"SAME,"sothattheexactvaluesarematchedbetweenthetwoarguments.typecanbeoneofthreecase-insensitivevalues:

IntervalName Definition

Page 1713: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

D UsewhenthetwoargumentsareSASdatevalues.Thesecanbedateliteralsorvariablescontaining

Page 1714: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASdatevalues

DT UsewhenthetwoargumentsareSASdatetimevalues.Thesecanbedatetime

Page 1715: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

literalsorvariablescontainingSASdatetimevalues

OBS Usethiswhenyouwanttofindtheinterval

Page 1716: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweentwoobservationsbyprovidingobservationnumbers.Thiswillreturnanintervalwiththebasenameof"OBS."

Page 1717: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Youcanusethisfunctiontogiveyoutheintervalbetweentwopointsintime.Theintervalreturnedmightcontainamultiplierand/orashiftindex.Asyoucanseeinthetablebelow,someoftheintervalsarenotasyoumightanticipate.

Page 1718: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

start end

A Tuesday,April1,2014

Wednesday,April2,2014

B Tuesday,April1,2014

Thursday,April3,2014

C Tuesday,April1,2014

Friday,April4,2014

D Tuesday,April Saturday,April

Page 1719: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1,2014 5,2014

E Tuesday,April1,2014

Sunday,April6,2014

F Tuesday,April1,2014

Tuesday,April8,2014

G Tuesday,April1,2014

Wednesday,April9,2014

H Tuesday,April Friday,April

Page 1720: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1,2014 11,2014

I Tuesday,April1,2014

Sunday,April13,2014

J Tuesday,April1,2014

Tuesday,April15,2014

K Tuesday,April1,2014

Wednesday,April16,2014

Page 1721: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

L Tuesday,April1,2014

Thursday,April17,2014

M Tuesday,April1,2014

Saturday,April19,2014

N Tuesday,April1,2014

Monday,April21,2014

O 5/1/20149:00AM

5/1/20142:00PM

Page 1722: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

P 5/1/20149:00AM

5/1/20148:45PM

Q 5/1/20149:00AM

5/2/20143:30AM

Theitalicizedrowsshowexamplesofintervalsthatcanbepuzzling.Rememberthatthefunction

Page 1723: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

providestheexactintervalsbetweenthepairofdatesordatetimes,andthelowestcommondenominatorwillbeusedforbaseinterval.Inaddition,thedatesaremeasuredusingthesamedayalignment,whichmeansthattheintervalismeasuredfromthebeginningof

Page 1724: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theinterval,andthenadjusted.Atfirstglance,theDAY5.5resultinrowEdoesn'tseemtoberight;Tuesdayisonlythethirddayoftheweek,soshouldn'ttheshiftbe3asitisfortheDAY3andDAY4intervals?ThestartofDAY5intervalsareSunday,thenFriday.Ifyou

Page 1725: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wanttomeasureaDAY5intervaltoaSundayfromaTuesday,thenyouwillhavetomeasurefromtheFriday,whichgivesyouashiftof5.RowsPandQdemonstratethatitmightbecomeevenmoreconfusingwithtimesand/ordatetimes,asthebaseintervalshiftpointsbecome

Page 1726: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

minutesand/orseconds,sotheresultingmultipliersandshiftindicesmightbelargeenoughsothatyoulosethecontextofthereferencepoint.

ThefollowingtableshowsasampleresultofusingtheINTFIT()functionwiththe'OBS'type.Therecordsare

Page 1727: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fromasequentialdatasetwithWednesday,April2,2014,asthefirstdate.Sincethedatesarekeyedbyobservation,theresultoftheINTFIT()functionshowstheintervalastherelationshipbetweenobservations,nottheactualdatevaluesthemselves.

Page 1728: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall Date

INTFIT(1,1,'OBS') Wednesday,April2,2014

INTFIT(1,4,'OBS') Saturday,April5,2014

Page 1729: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTFIT(1,5,'OBS') Sunday,April6,2014

INTFIT(1,10,'OBS') Friday,April11,2014

INTFIT(1,12,'OBS') Sunday,April13,2014

Page 1730: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTFIT(1,13,'OBS') Monday,April14,2014

INTFIT(1,14,'OBS') Tuesday,April15,2014

INTFIT(1,16,'OBS') Thursday,April17,

Page 1731: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014

INTFIT(1,19,'OBS') Sunday,April20,2014

5.8.2INTFMT('interval','size')

INTFMT('interval','size')

Page 1732: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

takestheintervalthatyougiveandreturnsasuggestedformatfordate,time,ordatetimevaluesusingthisinterval.intervalcanbeanystandardSASinterval(withmultipliersand/orashiftindexifdesired),orauser-definedintervalthatyouhavecreated,asshownin

Page 1733: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Section5.6.Ifintervalisrepresentedbyacharacterstring,thenitmustbeenclosedinquotationmarks,butyoucanuseacharactervariablecontainingthenameofanintervalinstead.sizereferstowhethertheformatreturnedwillhaveatwo-digityear('short'/'s')orafour-

Page 1734: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digityear('long'/'l').Thisargumentmustbeenclosedinquotationmarksalso,butaswiththeintervalargument,youcanuseacharactervariablefortheargument.Thereisoneimportantusagenoteaboutthesizeargument:Ifyoudonotuselowercase,thefunctionmightreturn

Page 1735: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anunpredictableresult(seeRowA)intheexampletablebelow:

SampleFunctionCall

A INTFMT('WEEK','LONG')

B INTFMT('WEEK','long')

C INTFMT('MONTH','long')

Page 1736: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

D INTFMT('QTR','short')

E INTFMT('QTR','long')

F INTFMT('YEAR','long')

Page 1737: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

G INTFMT('YEAR10','l')

H INTFMT('YEAR100','l')

I INTFMT('DTMONTH','l')

J INTFMT('DTYEAR','l')

Page 1738: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

K INTFMT('HOUR','l')

L INTFMT('WORKINGDAYS','l')

ThisfunctionismostusefulwhenyouusetheINTFITorINTGETfunctionstodetermine

Page 1739: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theintervaldynamically,sothatyouwillalwaysdisplayaresultinthepropercontext.Seeexample5.23toseehowthiscanbeusedtodynamicallyformatresults.

5.8.3INTGET(argument1,argument2,argument3)

Page 1740: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTGET(argument1,argument2,argument3determinesanintervalfromthreedateordatetimevaluesthatyouprovide.Theargumentscanbevariablesordate/datetimeliterals,buttheymustbeofthesametype;youcannotmixdatesanddatetimesasarguments.The

Page 1741: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functioncalculatesallintervalsbetweenthefirsttwoarguments,andthenbetweenthesecondandthirdarguments.Iftheintervalsarethesame,itwillreturnthatinterval.Iftheintervalsarenotthesame,thenthefunctionwilltesttheintervalbetweenthesecondandthird

Page 1742: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

argumentstoseewhetheritisamultipleoftheintervalbetweenthefirsttwoarguments.Ifthisistrue,thenthefunctionwillreturnthesmallerofthetwointervals.Ifneitherofthesecasesaretrue,thentheINTGET()functionwillreturnamissingvalue.

Page 1743: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

A INTGET('05SEP2013'd,20SEP2013'd,05OCT2013'd)

B INTGET('15JAN2014'd,15APR2014'd,15OCT2015'd)

C INTGET('15JAN2014'd,15APR2015'd,15OCT2015'd)

D INTGET('09JUL2015'd,09SEP2015'd,09MAR2016'd)

E INTGET('09JUL2015'd,29AUG2015'd,15SEP2015'd)

InrowA,theperiod

Page 1744: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenthedatesis15days,whichcorrespondstotheSEMIMONTHinterval.RowBdemonstratesthatthedatesdonothavetohavethesameinterval.ThegapbetweenJanuaryandAprilisonequarter,butthegapbetweenAprilandOctoberofthefollowingyearis

Page 1745: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fivequarters.Sincetheintervalforthesecondpairofargumentswouldbe'QTR5,'sinceitisamultipleoftheQTRinterval,thefunctionreturnstheunmultipliedinterval.WhathappenedinrowC?Simply,theintervalbetweenJanuary2014andApril2015isnowQTR5,buttheinterval

Page 1746: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweenAprilandOctober2015isQTR2.SinceQTR2isnotamultipleofQTR5,theresultismissing.RowDshowsthatiftheintervalbetweentwoargumentsisitselfamultipliedinterval(MONTH2versusMONTH6),thefunctionwillreturntheintervalwiththesmallest

Page 1747: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

multiplier,whilerowEshowstheingenuityofthefunction:TheDAY17intervalworksifitisshiftedby15days.

5.8.4INTSHIFT('interval')INTSHIFT('interval')takestheintervalthatyougiveandreturns

Page 1748: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theshiftpointforthatinterval.intervalcanbeanystandardSASinterval(withmultipliersand/orashiftindexifdesired).Ifintervalisrepresentedbyacharacterstring,thenitmustbeenclosedinquotationmarks,butyoucanuseacharactervariablecontainingthenameof

Page 1749: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anintervalinstead.Ifyoutrytousethisfunctionwithauser-definedintervalthatyouhavecreated,thefunctionwillreturnamissingvalue.

SampleFunctionCall

INTSHIFT('WEEK')

INTSHIFT('MONTH')

Page 1750: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTSHIFT('QTR3.4')

INTSHIFT('YEAR')

INTSHIFT('YEAR10.5')

INTSHIFT('YEAR100')

INTSHIFT('DTMONTH')

INTSHIFT('DTYEAR.7')

INTSHIFT('HOUR')

Page 1751: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1752: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTSHIFT('WORKINGDAYS')

Page 1753: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.8.5INTTEST('interval')INTTEST('interval')takestheintervalthatyougiveandreturnsa1ifitisavalidintervalname,or0ifitisnot.Youcanusethistodeterminewhetheryouhavecreatedavalidintervalwithmultipliersand/ora

Page 1754: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftindex.intervalcanbeanystandardSASinterval(withmultipliersand/orashiftindexifdesired).Ifintervalisrepresentedbyacharacterstring,thenitmustbeenclosedinquotationmarks,butyoucanuseacharactervariablecontainingthenameofanintervalinstead.If

Page 1755: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youtrytousethisfunctionwithauser-definedintervalthatyouhavecreated,thefunctionwillreturnamissingvalue.

SampleFunctionCall

INTTEST('WEEK')

INTTEST('QTR.1')

Page 1756: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1757: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTTEST('QTR3.13')

Page 1758: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTTEST('YEAR.7')

Page 1759: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTTEST('YEAR10.14')

INTTEST('DTMONTH')

INTTEST('HOUR2.3')

INTTEST('WORKINGDAYS')

Page 1760: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 1761: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.9RetailCalendarIntervalsandSeasonalityThissectioncoversintervalsandfunctionsthataremostfrequentlyusedwithtimeseriesanalyses.Althoughthetopicoftimeseriesanalysisisbeyondthescopeof

Page 1762: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thisbook,theseintervalsandfunctionsaredescribedherebecausetheintervalscanbeusedwithINTNX()andINTCK(),whilethefunctionsareapartofBaseSAS.However,theyarepresentedwithoutcontext.

Page 1763: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.9.1RetailCalendarIntervalsSAShasaddedintervalsthatarespecificallydesignedfortheretailindustry.TheseintervalsareISO8601compliant,andcanbeusedwithanyoftheintervalfunctions.They

Page 1764: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

facilitatecomparisonsacrossyears,becausetheweeksremainconsistentbetweenyears.Inordertofacilitatethis,someyearswillhaveleapweeks.YeardefinitionsarebasedontheISO8601definitionofaweek,whichisthefirstMondayprecedingJanuary4,whichin

Page 1765: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

somecasesmightplacethebeginningoftheweekinDecember.Theseintervalsenableyoutodefinethestructureofyour52-weekyear,expectingthatforthefirst13-weekperiodofyourinterval,therewillbe1monththathas5weeksinit.Thismeansthatyoucansetthemonth

Page 1766: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

patternto5-4-4,4-5-4,or4-4-5.Youcanworkwithyears,months,orquartersinthisfashion.ThefulllistofretailintervalsisgiveninTable5.7.

Table5.7:RetailCalendarIntervals

Interval Description

YEARV Specifies

Page 1767: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601yearlyintervals.TheISO8601yearbeginsontheMondayonorimmediately

Page 1768: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

precedingJanuary4.NotethatitispossiblefortheISO8601yeartobegininDecemberofthe

Page 1769: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

precedingyear.Also,someISO8601yearscontainaleapweek.Thebeginningsubperiod

Page 1770: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

iswritteninISO8601weeks(WEEKV).

R445YR IsthesameasYEARVexceptthatintheretailindustrythe

Page 1771: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginningsubperiodis4-4-5months(R445MON).

R454YR IsthesameasYEARVexceptthatintheretail

Page 1772: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

industrythebeginningsubperiodis4-5-4months(R454MON).

R544YR IsthesameasYEARVexceptthat

Page 1773: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intheretailindustrythebeginningsubperiodis5-4-4months(R544MON).

R445QTR Specifies

Page 1774: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

retail4-4-5quarterlyintervals(every13ISO8601weeks).Somefourthquarterscontaina

Page 1775: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leapweek.Thebeginningsubperiodis4-4-5months(R445MON).

R454QTR Specifiesretail4-5-4

Page 1776: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

quarterlyintervals(every13ISO8601weeks).Somefourthquarterscontainaleapweek.

Page 1777: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thebeginningsubperiodis4-5-4months(R454MON).

R544QTR Specifiesretail5-4-4quarterly

Page 1778: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervals(every13ISO8601weeks).Somefourthquarterscontainaleapweek.The

Page 1779: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginningsubperiodis5-4-4months(R544MON).

R445MON Specifiesretail4-4-5monthly

Page 1780: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervals.The3rd,6th,9th,and12thmonthsarefiveISO8601weekslongwiththe

Page 1781: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

exceptionthatsome12thmonthscontainleapweeks.AllothermonthsarefourISO8601weeks

Page 1782: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

long.R445MONintervalsbeginwiththe1st,5th,9th,14th,18th,22nd,27th,31st,35th,40th,

Page 1783: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

44th,and48thweeksoftheISOyear.Thebeginningsubperiodis4-4-5months(R445MON).

Page 1784: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

R454MON Specifiesretail4-5-4monthlyintervals.The2nd,5th,8th,and11thmonthsarefiveISO

Page 1785: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8601weekslongwiththeexceptionthatsome12thmonthscontainleapweeks.R454MON

Page 1786: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intervalsbeginwiththe1st,5th,10th,14th,18th,23rd,27th,31st,36th,40th,44th,and49thweeks

Page 1787: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oftheISOyear.Thebeginningsubperiodis4-5-4months(R454MON).

R544MON Specifies

Page 1788: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

retail5-4-4monthlyintervals.The1st,4th,7th,and10thmonthsarefiveISO8601weekslong.All

Page 1789: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

othermonthsarefourISO8601weekslongwiththeexceptionthatsome12thmonths

Page 1790: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

containleapweeks.R544MONintervalsbeginwiththe1st,6th,10th,14th,19th,23rd,27th,32nd,

Page 1791: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

36th,40th,45th,and49thweeksoftheISOyear.Thebeginningsubperiodis5-4-4months

Page 1792: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(R544MON).

WEEKV SpecifiesISO8601weeklyintervalsofsevendays.EachweekbeginsonMonday.

Page 1793: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thebeginningsubperiodsiscalculatedindays(DAY).NotethatWEEKVdiffersfromWEEKin

Page 1794: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatWEEKV.1beginsonMonday,WEEKV.2beginsonTuesday,andsoon.

Page 1795: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

5.9.2SeasonalityFunctionsSeasonalityisusedintimeseriesanalysisandcanbeusedinSAS/ETS.Ithelpstoaccountfornormalseasonalvariationsinpatternsinsideananalysis.Whileitisnot

Page 1796: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

strictlyadateandtimematter,itdoesuseintervals.Therefore,theseasonalityfunctionsaredocumentedhere,butwithoutcontext.Formoreinformationaboutseasonalityanditsapplication,youcanrefertotheSAS/ETSdocumentation,andsupport.sas.comisa

Page 1797: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

greatplacetofindmorehelponthistopic.

INTCINDEX('interval',argument)INTCINDEX('interval',argumentreturnstheindexoftheseasonalcyclebasedonintervalforargument,whereargumentisaSASdate,time,ordatetimevalue.intervalcanbeanystandard

Page 1798: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASinterval(withmultipliersand/orashiftindexifdesired).

INTCYCLE('interval',seasonality)INTCYCLE('interval',seasonalityreturnstheintervaloftheseasonalcycle.intervalcanbeanystandardSASinterval(withmultipliersand/orashiftindexif

Page 1799: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

desired).seasonalityisanoptionalargumentthatenablesyoutodefineseasonalcycles,anditcanbeanumberoracycle(suchas'YEAR').Forexample,youcanusetheseasonalityargumenttospecifyyouryearashaving53weeksinsteadof52.

Page 1800: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTINDEX('interval',argument,seasonality)INTINDEX('interval',argument,seasonalityreturnstheseasonalindexwhengivenaninterval,aSASdate,time,ordatetimeoftheseasonalcycle.intervalcanbeanystandardSASinterval(withmultipliersand/orashiftindexifdesired.argumentisaSASdate,

Page 1801: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

time,ordatetimevalue.Notethattheintervalspecifiedmustbeappropriatefortheargument.seasonalityisanoptionalargumentthatenablesyoutodefineseasonalcycles,anditcanbeanumberoracycle(suchas'YEAR').TheINTINDEXfunctionreturnstheseasonalindex,while

Page 1802: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theINTCINDEXfunctionreturnsthecycleindex.

INTSEAS('interval',seasonality)INTSEAS('interval',seasonalityreturnsthenumberofintervalsinaseasonalcycle.intervalcanbeanystandardSASinterval(withmultipliersand/ora

Page 1803: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftindexifdesired).seasonalityisanoptionalargumentthatenablesyoutodefineseasonalcycles.Thisisagoodfunctiontobeawareofifyouarenotfamiliarwithconceptsofseasonality,becausethenumberofintervalsinaseasonalcyclemightnotbeintuitive.Forexample,whileyou

Page 1804: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mightexpectthenumberofintervalsfortheQTRintervaltobe4(becausethereare4quartersinayear),thenumberofintervalsforaDAYintervalis7,forthenumberofdaysintheweek.Thiscanhelpyoucheckyourexpectations.

Page 1805: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Chapter6DeeperintoDatesandTimeswithSAS

6.1Macro

Page 1806: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VariablesandDates6.2GraphingDates6.3TheBasicsofPROCEXPAND6.4InternationalDate,Time,andDatetimeFormatsandInformats

Page 1807: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.5NLSDate,Time,andDatetimeConversionFunctions6.6DateFormatsandInformatsforOtherCalendars6.7OtherSoftware

Page 1808: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andTheirDates(Excel,Oracle,DB2)6.8Conclusion

6.1MacroVariablesandDatesThereisahigh

Page 1809: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

potentialforconfusionwhenitcomestothesubjectofmacrovariablesanddates.AlthoughyouhaveaccesstodatesandtimesintheSASmacrolanguagewiththeautomaticmacrovariables&SYSDATE,&SYSDATE9,&SYSDAY,and&SYSTIME,thedisplay

Page 1810: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthesevaluesisfixed,andthereforetheydonotgiveyouthepowerofSASformats,orofSASdateandtimefunctions.

6.1.1AutomaticMacroVariablesNoneoftheseautomaticmacrovariablescanbe

Page 1811: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

assignedvalueswith%LETorCALLSYMPUT(),orbyanyothermeans.Theyareread-onlyandworkbyreadingtheoperatingsystemclockwhenaSASsessionisstarted.ThismeansthattheydonotchangewithinagivenSASsession.

&SYSDATE

Page 1812: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

&SYSDATEcannotbemodifiedanddisplaysthesystemdateasaSASdatevalueformattedwiththeDATE7.format.IfthedateaccordingtotheoperatingsystemwhentheSASsessionstartsisJuly17,2014,then&SYSDATEwouldbeequalto"17JUL14."

Page 1813: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

&SYSDATE9&SYSDATE9isidenticalto&SYSDATE,exceptthatitformatsthesystemdatewiththeDATE9.formatandisthereforeY2K-compliant.IftheoperatingsystemdateisNovember22,2015,then&SYSDATE9wouldbeequalto

Page 1814: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

"22NOV2015."

&SYSDAY&SYSDAYdisplaysthenameofthedaythattheSASsessionbegan(accordingtotheoperatingsystem).IfyoustartedaSASjoborsessiononMonday,April14,2014,&SYSDAYwouldbe

Page 1815: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

equalto"Monday."

&SYSTIME&SYSTIMEdisplaysthesystemtime(accordingtotheoperatingsystem)inTIME5.format.Ifthesystemtimeis3:36p.m.whenyoustartSAS,then&SYSTIMEwouldbeequalto"15:36."

Page 1816: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.1.2PuttingDatesintoTitlesOneoftheprimeusesofdatesinmacrovariablesisforcustomtitlesandfootnotes.Touseamacrovariableinatitle,youjustneedtoenclosetheTITLEorFOOTNOTEstatementthatcontainsthemacrovariablewithdouble

Page 1817: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

quotationmarkslikethis:

TITLE"Today'sDateis&SYSDATE9";

Toseethisincontext,lookatthefollowingprogram:

Example6.1:UsingAutomaticMacroVariablesinaTitle

Page 1818: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TITLE"Thisishowtoputamacrovariableinatitle:Today'sDateis&SYSDATE9";ODSRTFFILE="examples\title.rtf";PROCPRINTDATA=sashelp.company(OBS=5);IDjob1;VARdepthead;RUN;ODSRTFCLOSE;

Hereistheresultingoutput:

Page 1819: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whiletheautomaticmacrovariables&SYSDATE,&SYSDATE9,&SYSDAY,and&SYSTIMEmightgiveyoutheinformationthatyouneed,theirformatsarefixedand

Page 1820: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

cannotbechanged.Giventhemultiplewaysthatdates,times,anddatetimescanbedisplayed,itwouldbegoodifyoucouldusetheformatsandfunctionstogetthedisplayofdatesandtimesthatyouwantinyourtitles.

Page 1821: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.1.3Using%SYSFUNC()toCreateDates,Times,andDatetimesinMacroVariablesManyofthedateandtimefunctions,aswellasformats,areavailableintheSAS

Page 1822: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

macrolanguagethroughthe%QSYSFUNC()and%SYSFUNC()macrofunctions.Whenyouareusingoneofthesemacrofunctionswithdatesandtimes,youcanusetwoarguments:Thefirstisthedate,time,ordatetimefunctionthatyouwishtouse.Thesecond

Page 1823: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

argumentisoptional,butyoucanspecifytheformatthatshouldbeappliedtotheresult.

Example6.2:DateFunctionswith%SYSFUNC()Thisexampleputstheformattedvalueoftoday'sdateintothemacrovariable&DATE

Page 1824: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddemonstratesthedifferencebetweenunformattedandformatteddatesinmacrovariables.Thedatevaluein&RAWDATEisstoredinmacrospaceasatextstringandwillneedtobeconvertedifyouwanttouseitforanycalculations.&DATEisalsoatext

Page 1825: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringinmacrospace,butitshowsthattheWORDDATE.formathasbeenappliedtothevalue.

%LETrawdate=%SYSFUNC(DATE());%LETdate=%SYSFUNC(DATE(),WORDDATE32.);

TITLE"Today'sSASDateValueis&rawdate";TITLE2"Formatteddate:&date";

Page 1826: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODSRTFFILE="c:\book\2ndEd\examples\title2.rtf";PROCPRINTDATA=sashelp.class(OBS=5);IDname;VARage;RUN;ODSRTFCLOSE;

Nowyoucanusetheresultinatitleorfootnote.

Page 1827: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YoucanalsousethisasapartofanoutputfilenameorExcelworksheetname.

Example6.3:PuttingaDateValueintoanOutputFileName

%LET

Page 1828: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

filedate=%SYSFUNC(DATE(),yymmddd10.);

ODSRTFFILE="Car_Report_&filedate..rtf";PROCPRINTDATA=sashelp.carsNOOBSLABEL;RUN;ODSRTFCLOSE;

Whenyoutrythisathome,youwillseethatyouhavegeneratedanRTFfilenamed"Car_report_yyyy-mm-

Page 1829: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dd.rtf."

Example6.4:NamingWorksheetswiththeDateInthisexample,wecreateacustomformattodisplaydatesasthefullmonthnameand4-digityearandapplyittoacopyofthedatevariableinthedataset.

Page 1830: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThenweusethatvariableastheBYvariableinthePRINTprocedure.Notonlydoesthisputthenameoneachworksheet,butitalsofiltersthedatesintotheircorrectworksheetwithoutmuchextracodingrequired.Therearemanyotherwaysthatyoucanaccomplish

Page 1831: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thistask,butthisisstraightforwardandsimple.Ifyouhaveagreatdealofdata,youmightwanttoinvestigateamoreefficientmethodthancreatinganadditionalvariableinadataset.Lines1–5createtheformatusingthePICTUREstatementinPROCFORMATand

Page 1832: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datedirectivesdiscussedinsection2.6,andlines6–13createourduplicatevariablewithourcustomformatandalsosubsetthedataset.NotethedateliteralsusedasvaluesintheBETWEENoperatorinline10.WeremovethedefaultBYlinefromtheoutputinline14.

Page 1833: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheSHEET_NAMEoptionallowsustousetheBYvaluetoken(#BYVAL1)asthenameoftheworksheet.

1PROCFORMAT;2PICTUREnamefmt3LOW-HIGH='%B%Y'(DATATYPE=date)4;5RUN;

6PROCSQL;7CREATETABLEsubset

Page 1834: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AS8SELECTdateassheet_dateFORMAT=namefmt15.,*9FROMsashelp.citiday10WHEREdateBETWEEN'01JAN1988'dAND'31AUG1988'd11ORDERBYDATE12;;;;13QUIT;

14OPTIONSNOBYLINE;15ODSTAGSETS.EXCELXPFILE="ex6.1.4.xml"OPTIONS

Page 1835: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(SHEET_NAME="#BYVAL1");16PROCPRINTDATA=subsetLABELNOOBS;17BYsheet_date;18RUN;19ODSTAGSETS.EXCELXPCLOSE;

Thisistheresultingworkbook:

Page 1836: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.1.4UsingDatesinMacrosWhileSAScanhandledates,times,anddatetimevaluesasnumbers,gettingthemacroprocessortoacceptthemasnumbersmightinvolveanextrasteportwo.Let'slookatthefollowingcode:

Page 1837: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1%MACROdate_loop;2%DOI='05JUN2014'd%TO'12JUN2014'd;3TITLE"HappyKidsIceCreamCo.Salesfor&i";4PROCPRINTDATA=sample.dailysales;5IDdistrict;6VARsales;7WHERE

Page 1838: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateEQ&i;8RUN;9%END;10%MEND;

Wehaveusedtwodateliteralstoprovidetheperiodoverwhichwewantthereportrunanduseditinthetitleaswell.Whathappenswhenthisissubmitted?

ERROR:Acharacter

Page 1839: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

operandwasfoundinthe%EVALfunctionor%IFconditionwhereanumericoperandisrequired.Theconditionwas:'05JUN2014'd

ERROR:The%FROMvalueofthe%DOIloopisinvalid.

ERROR:Acharacteroperandwasfoundinthe%EVALfunctionor%IFconditionwhereanumericoperandisrequired.Theconditionwas:'12JUN2014'd

Page 1840: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ERROR:The%TOvalueofthe%DOIloopisinvalid.

ERROR:ThemacroDATE_LOOPwillstopexecuting.

Tothemacroprocessor,thedateliteralsareonlytext.TheydonotfunctionastheydointherestofSAS.Howcanwegetaroundthis?Byusing

Page 1841: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

the%SYSEVALF()macrofunctiontoforcetheinterpretationofthedateliterals.Let'sreplaceline2withthefollowingline:

%DOi=%SYSEVALF('05JUN2014'd)%TO%SYSEVALF('12JUN2014'd);

Nowtheloopwillexecuteproperly.

Page 1842: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

However,thedateinthetitledoesnotlookgoodatall.

Thedateinthetitledoesn'tlooklikeadate.Weneedtomakeonemoremodificationtotheprogramtogetour

Page 1843: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

titletolookright.The%LETstatementaddedinline3createsaformattedvalueofthedateinthemacroloopforuse.ThePUTN()functionisusedhereinsteadofthePUT()functionbecauseyoucan'tusethePUT()functionwith%SYSFUNC()orQSYSFUNC().

Page 1844: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1%MACROdate_loop;2%DOI=%SYSEVALF('05JUN2014'd)%TO%SYSEVALF('12JUN2014'd);3%LETTITLEDATE=%SYSFUNC(PUTN(&i,WORDDATE.));4TITLE"HappyKidsIceCreamCo.Salesfor&titledate";5PROCPRINTDATA=sample.dailysales;6IDdistrict;

Page 1845: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

7VARsales;8WHEREdateEQ&i;9RUN;10%END;11%MEND;

Andnowwegetthedesiredresult.

Page 1846: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Ourfinalexampleofusingdatesinmacrosdemonstrateshowtogetadatefromadatasetintoamacro.Forthisexample,wewanttoputthelatestdatefromagivencolumnintoamacrovariablesothatitcanbeusedinatitle.Let'slookatsometimingvariablesfromthissampledata

Page 1847: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

set.Wewanttoputthelatestdateandtime(whichisinrecord4)fromthevariableMODDATE(nicelyformatted,ofcourse)onthetitleofeachpage.

Page 1848: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Method1:UsingCALLSYMPUTTheCALLSYMPUT()andSYMGET()functionsarealsousedtocommunicate

Page 1849: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

betweentheDATAstepandthemacroworld.CALLSYMPUT()takesavalueandstoresitinmacrospacefromwithinaDATAstep,whileSYMGET()takesamacrovariableandenablesyoutouseitinaDATAstep.Inthissituation,IrecommendthatyouuseCALLSYMPUTX()insteadof

Page 1850: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CALLSYMPUT(),becauseCALLSYMPUTX()suppressesanytrailingblanksinthemacrovariablecreated,andthepresenceoftrailingblanksmightaffectthejustificationofyourtitleorfootnote.Thedifferencebetweenrun-timeandcompiletimeisveryimportant.You

Page 1851: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

cannotusea%LETstatementtostoremacrovaluesthataredefinedduringexecutionofaDATAstep.Youalsocannotuseamacrovariablereference(withanampersand[&])inthesameDATAstepwherethemacrovariableiscreatedwithCALLSYMPUT().Thenext

Page 1852: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

exampleuseswhatwasdiscussedinsection6.1.2toshowhowyoucanobtainadatevaluefromaSASdatasetandthenputitinamacrovariabletouseinatitle.SincetheDATETIME.formatisn'tformalenoughforthisreport,we'regoingtocreateaRPTDATE.customformat.

Page 1853: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCFORMAT;PICTURErptdate(DEFAULT=32).-.Z='Missing'LOW-HIGH='%B%d,%Yat%I:%0M%p'(DATATYPE=DATETIME);RUN;

/*Getthemaximumdateinthevariable*/PROCMEANSDATA=sample.pmdataMAXNOPRINT;VARmoddate;OUTPUTOUT=tmpMAX=max;RUN;

Page 1854: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

/*TransferittoamacrovariableusingCALLSYMPUTX()*/DATA_NULL_;SETtmp;CALLSYMPUTX('lastmod',STRIP(PUT(max,rptdate.)));RUN;

%PUTlastmod=&lastmod;

PartialLog42%PUTlastmod=&lastmod;lastmod=February7,2014at4:18PM

Page 1855: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Method2:UsingPROCSQLForsomethingassimpleasthehighest(orlowest)valueinadateordatetimevariable,youdon'thavetouseaSASprocedureandaDATAstep.PROCSQLcantaketheplaceofboth.

PROCSQLNOPRINT;

Page 1856: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SELECTDISTINCTSTRIP(PUT(MAX(moddate),rptdate.))LENGTH=32INTO:lastmodFROMsample.pmdata;QUIT;

%PUTlastmod=&lastmod;

TheLog1PROCSQLNOPRINT;2SELECTDISTINCTSTRIP(PUT(MAX(moddate),rptdate.))LENGTH=32INTO3!:lastmod4FROMsample.pmdata;5QUIT;

Page 1857: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NOTE:PROCEDURESQLused(Totalprocesstime):realtime0.08secondscputime0.00seconds

67%PUTlastmod=&lastmod;lastmod=February7,2014at4:18PM

Page 1858: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whichevermethodyouchoose,themacrovariable&LASTMOD,containingthevalue"February7,2014at4:18PM",isreadytouseinyourreporttitle.

6.2GraphingDatesWhenyouusedates,times,ordatetime

Page 1859: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesinyourSASgraphics,youhavetorememberthattheyarenumbers.Thishasalargeimpactontheaxesandlabelingofyourgraphics.Attemptingtographaresultoveraperiodoftimewithoutusingformatsorintervalswillusuallyresultinagraphthatisnotclear

Page 1860: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

orwell-defined.ThegoodnewsisthatyoucanuseallthefeaturesassociatedwithSASdatestoimproveyourgraphics,suchasformatsandintervals.Thefollowingseriesofexampleswillillustrate.Example6.5willdemonstratethegraphingofdateswithSAS/GRAPH,while

Page 1861: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example6.6willuseODSgraphicstoproduceaseriesplot.

Johnny'sSavingsAccountWhenJohnnyturned10yearsoldonSeptember1,1975,hetookallthemoneythathehadinhispiggybankanddepositedit

Page 1862: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intoabankaccountthatpaid3.5%interestannually,compoundeddaily.Hepromisedhimselfthathewouldaddtwodollarsattheendofeachweekandthathewouldtakethemoneyoutwhenhereachedtheripeoldageof50.Whenhewas10,Johnnythoughthemighthave$4,000by

Page 1863: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thetimehereachedage50.Hekeptthatpromise,solet'slookatJohnny'searningsfromthetimehewas10untilhewas50usingthefollowingprogram:

Example6.5:SAS/GRAPH

TITLE"Johnny's50thBirthdayFund";PROCGPLOT

Page 1864: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATA=book.graph1;PLOTfund*date/VREF=5000LV=1CV=blue;RUN;

Hereistheresultinggraph:

Page 1865: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Unfortunately,theresultofthissimpleexampleisn'thelpful.WecanseethatJohnnystartedaround6000,andheturned50somewherearound20000.Whatdoesthat

Page 1866: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mean?Thisshouldbeeasyenoughtofix.Don'twejustneedtoaddaFORMATstatement?

TITLE"Johnny's50thBirthdayFund";PROCGPLOTDATA=book.graph1;PLOTfund*date/VREF=5000LV=1CV=blue;FORMATdatemmddyy10.;/*AddFORMATstatement*/RUN;

Page 1867: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HereisthenewGPLOTgraphwiththeFORMATstatement:

Page 1868: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WhathashappenedhereisthatSASchosetheboundariesandfiguredoutmajorandminortickmarks.Inthisexample,ithasselectedmajorintervalsatdecadeboundaries,

Page 1869: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andminoronesatyearboundaries.That'snotabadchoiceforthisexample,butSAS/GRAPHdoesn'talwaysmakesuchagoodchoicewhenselectingtheboundariesofanaxis.Can'tyoudefinethehorizontalaxisyourself?Ofcourse,youcan!Johnnywas

Page 1870: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

borninSeptember,soitwouldmakesensetocharthisprogressathisbirthdaysandrestrictthespanofthehorizontalaxistotheperiodduringwhichhe'scontributing.

TITLE"Johnny's50thBirthdayFund";PROCGPLOTDATA=book.graph1;PLOTfund*date/

Page 1871: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

VREF=5000LV=1CV=blueHAXIS='01SEP1975'dTO'01SEP2015'dbyYEAR;FORMATdatemmddyy10.;RUN;

HereisthenewgraphthatwegetfromusingtheHAXISoptioninthePLOTstatementtospecifytheaxisrange:

Page 1872: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whathappened?Wedefinedthehorizontalaxisashavingtickmarkseveryyear,soSASaccommodatedourrequest.Sincetherewasnotenoughroomtodisplaythevalues

Page 1873: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

horizontally,SASautomaticallyrotatedthevalues90degrees.Unfortunately,thatleftlessspaceforthegraphitself.Weneedbetterspacingonourhorizontalaxis.

Sincedecadesseemedtoworkwell,let'susethoseasourintervals,butwewanttostarton

Page 1874: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Johnny's10thbirthday,anddefinethemajorhorizontalaxispointsatSeptember1,1975,September1,1985,September1,1995,September1,2005,andSeptember1,2015.Anintervalmultiplierof10willcreatethedecadeinterval,andashiftoperatorof69(60[monthsin5years]

Page 1875: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

plus9[monthsfromJanuarytoSeptember])willmovethestartingdateofthe10-yearintervaltoSeptember1975sothatitmatchesthestartingpointofthehorizontalaxis.Notethattheonlychangefromthepreviousversionisintheintervaldefinition.

Page 1876: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TITLE"Johnny's50thBirthdayFund";PROCGPLOTDATA=book.graph1;PLOTfund*date/VREF=5000LV=1CV=blueHAXIS='01SEP1975'dTO'01SEP2015'dbyYEAR10.69;FORMATdatemmddyy10.;RUN;

Hereisthefinalproduct:

Page 1877: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Nowthat'swhatwewanted.Thisdemonstratesthatyouhavealloftheintervaltypes,aswellastheirmultipliersandshiftoperators,availablewhenyouaredefining

Page 1878: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

axesthatinvolvedate,time,anddatetimevaluesinSAS/GRAPH.Itmakesdefiningtheexactscopeofthegraphmucheasier,nottomentioncomprehendingwhatyou'vegraphed.

Example6.6:ODSGraphics

Page 1879: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WewillcontinuetouseJohnny's50thbirthdayfundfortheSGPLOTseriesofexamples.TheSGPLOTprocedurehasawayofassigningatimescaletoanaxisbysettingtheaxisTYPEoptionintheXAXISstatementequaltoTIME,andtheSERIESstyleofplotisperfectforgraphsof

Page 1880: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

longitudinaldata.

Here'sthebasicSGPLOTfromtheabovecode.

AswiththeGPLOTexample,an

Page 1881: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

unformatteddatevariableisn'tveryuseful.However,eventhoughwegotourgraph,SGPLOTdidnotrunsmoothly.Inordertoproducethisgraph,theproceduremadesomeadjustmentsinordertoproducethegraph.Let'slookatthelogfromtheprogramthatproducedthe

Page 1882: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

abovegraph.

3TITLE"Johnny's50thBirthdayFund";4PROCSGPLOTDATA=book.graph1;5XAXISTYPE=time;6SERIESX=dateY=fund;7RUN;

NOTE:PROCEDURESGPLOTused(Totalprocesstime):realtime1.20

Page 1883: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

secondscputime0.09seconds

NOTE:Timeaxiscanonlysupportdatetimevalues.TheaxistypewillbechangedtoLINEAR.❶NOTE:Therewere2435observationsreadfromthedatasetBOOK.GRAPH1.

Note❶istellingusthatyoucan'tusethe

Page 1884: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TYPEvalueofTIME.Butaren'twegraphingdates?TheSGPLOTprocedurereliesonformatstodeterminethatthedatabeinggraphedaredatesortimes.Withoutit,SGPLOTerrsonthesideofcautionandjustassumesthatyouaregraphinglineardata.Thiswillcreateaplot

Page 1885: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fordatagraphedalongasequence(suchasdates),soitlooksright,butitisnotclearthatyouaregraphingdates.

Let'saddthemissingformattoourSGPLOT:

TITLE"Johnny's50thBirthdayFund";PROCSGPLOTDATA=book.graph1;XAXISTYPE=time;

Page 1886: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SERIESX=dateY=fund;FORMATdatemonyy7.;RUN;

Thatlooksbetter,eventhoughSASdidnotusetheMONYY7.formatthatyourequestedand

Page 1887: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

substitutedoneofitsownasseeninnote❶below.

51TITLE"Johnny's50thBirthdayFund";52PROCSGPLOTDATA=book.graph1;53XAXISTYPE=time;54SERIESX=dateY=fund;55FORMATdatemonyy7.;56RUN;

NOTE:PROCEDURESGPLOT

Page 1888: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

used(Totalprocesstime):realtime0.90secondscputime0.20seconds

❶NOTE:ThecolumnformatMONYY7isreplacedbyanauto-generatedformatontheaxis.

Thisisreallythesameproblemaswith

Page 1889: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SAS/GRAPHwhenyouhaven'tdefinedthetickmarksontheXaxisproperly.TheVALUES=optionintheXAXISstatementallowsustodefineourscaleinthesamewaythattheHAXIS=optioninthePLOTstatementdoesinSAS/GRAPH.Fora50-yearperiod,let'stry5-

Page 1890: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearintervals,startingwhenJohnnymadehispromiseonSeptember1,1975.

TITLE"Johnny's50thBirthdayFund";PROCSGPLOTDATA=book.graph1;XAXISTYPE=timeVALUES=('01sep1975'dTO'01sep2015'dBYYEAR5);SERIESX=dateY=fund;FORMATdatemonyy7.;RUN;

Page 1891: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Nowthedisplaylooksmuchnicer,andwehaveourMONYY7.formatdisplayed.

6.3TheBasicsof

Page 1892: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCEXPANDTheEXPANDprocedureisapartofSAS/ETS,whichisusedtopreparetimeseriesdataforfurtheranalysis.ItcreatesaSASdatasetanddoesnotproduceprintedoutput.

6.3.1

Page 1893: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CapabilitiesofPROCEXPANDPROCEXPANDwillchangethesamplingfrequencyofthedatathatyouhaveandconvertittoadifferentone.Itcaninterpolatevaluesintimeseriesdata,forexample,whenyouhavequarterlydatathatyou

Page 1894: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

needtoreportoranalyzeonamonthlybasis.Itcanperformthereverseoperation,thatis,toaggregate(collapse)datafromahighersamplingfrequencytoalowerone,suchastakingmonthlydataandturningitintoquarterlydata.PROCEXPANDcaninterpolatemissing

Page 1895: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuesevenifyouaren'tchangingthesamplingfrequency.ItalsoprovidesforextensivedatatransformationsandperformsallofthesefunctionswithoutalotofDATAstepprogramming.TheSAS/ETSdocumentationprovidesdetailonthe

Page 1896: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

procedure,itsstatements,andtheoptionsforthosestatements.ThereareotherproceduresavailableinSAS/ETS,suchasTIMESERIESandTIMEDATA,thatcanbeusedtomanipulatetimeseriesdata,andifyouneedtoworkwithtimeseriesdataonaregularbasis,

Page 1897: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youshouldinvestigatetheSAS/ETSproductanditsdocumentationmorecarefully.

PROCEXPANDusesSASintervaldefinitions.Thisincludesintervalmultipliersandtheshiftindex.Foradetailedexplanationofintervals,interval

Page 1898: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

multipliers,andtheintervalshiftindex,seeSections5.5and5.6ofthisbook.Whenyouusetheseintervaldefinitions(plusanyshiftindexand/ormultipliers),PROCEXPANDwillautomaticallyadjustforanycalendareffects(leapyears,varyingnumberofdaysina

Page 1899: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month).Aswithanythingthatusestheseintervaldefinitions,allmeasurementsandcalculationsareconsideredtobeatthebeginningoftheinterval(s)specified.ItispossibletochangethatdefinitionwithoptionsinoneofthePROCEXPAND

Page 1900: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statements,andthosearediscussedinSection6.3.5.

PROCEXPANDSampleDataThefollowingdatasetwillbeusedfortheexamplesinthissection.Thisismonthlytotallightrailridershipdataobtainedfromthe

Page 1901: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AmericanPublicTransportationAssociationfortheyears2003and2004andisusedwiththeirpermission.ThevaluesforOctoberandNovember2003wereremovedfromthedatatodemonstratesomeofthecapabilitiesofPROCEXPAND.

Page 1902: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DateRiders

(thousands)

JAN2003 2679.9

FEB2003 2421.9

MAR2003 2704.6

APR2003 2778.3

MAY2003 2718.6

JUN2003 2618.2

Page 1903: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JUL2003 2999.0

AUG2003 3504.7

SEP2003 3329.4

OCT2003 .

NOV2003 .

DEC2003 2888.6

JAN2004 3132.9

FEB2004 2814.3

Page 1904: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MAR2004 3067.3

APR2004 2928.8

MAY2004 2958.3

JUN2004 2966.3

JUL2004 3000.8

AUG2004 3071.2

SEP2004 2958.9

OCT2004 2992.8

Page 1905: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NOV2004 3017.5

DEC2004 3038.4

6.3.2UsingPROCEXPANDtoConverttoaHigherFrequency

Page 1906: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YoucanusePROCEXPANDtoconvertdatafromalowersamplingfrequencytoahighersamplingfrequency(forexample,convertingmonthlydatatodailyorweeklydata).Itdoessobyinterpolation,andthesyntaxtoconverttoahighersamplingfrequencyisasfollows:

Page 1907: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example6.7:ConvertingtoaHigherSamplingFrequency

1PROCEXPANDDATA=book.monthOUT=seven_daysFROM=MONTHTO=WEEK;2IDdate;3CONVERTriders;4RUN;

ThePROCEXPAND

Page 1908: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statementinline1specifiestheoutputdataset(SEVEN_DAYS)andexplainshowthedatainBOOK.MONTHshouldbeconverted,fromMONTHintervalstoWEEKintervals.TheIDstatementinline2indicatesthevariablethatidentifiesthetimeofeachrecord.SincetheWEEKinterval

Page 1909: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginsonSunday,thedateswillbealignedonSundays.Ifyouwantthedatestoaligntoadifferentdayoftheweek,useashiftindicator.TheWEEK.2intervalwillalignthedatestoMondays;theWEEK.3intervalalignstoTuesdays,andsoon.

Youwillusuallyusean

Page 1910: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IDstatementwithPROCEXPAND.Otherwise,SASwillcreateanIDvariablefortheinputrecordsandusethestartingpointofJanuary1,1960,whichmightnotbewhatyouwant.TheCONVERTstatementidentifiesthevariable(s)toconvert.Youcanalsorename

Page 1911: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thevariable(s)beingconvertedintheoutputdatasetlikethis:CONVERTinput-var=output-var;HerearethefirsteightobservationsfromthedatasetSEVEN_DAYSproducedbytheabovecode:

Riders

Page 1912: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date (thousands)

29DEC2002 2770.23

05JAN2003 2573.57

12JAN2003 2449.76

19JAN2003 2393.52

26JAN2003 2391.24

02FEB2003 2429.29

Page 1913: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

09FEB2003 2494.03

16FEB2003 2571.86

Whenyouinterpolatetimeseriesdatatoahigherfrequency,considerthattheinterpolatedvaluesdonotaddanynewinformationtothedatabecausetheyhavebeen

Page 1914: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

derivedmathematically,notobserved.Therefore,anystatisticalanalysesperformedwiththeseinterpolateddatashouldbeviewedwithcaution.

6.3.3UsingPROCEXPANDtoConverttoa

Page 1915: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LowerFrequencyYoucanconvertdatatoalowerfrequencywithPROCEXPANDintwoways:First,youcanusethesamesyntaxaswithconvertingtoahighersamplingfrequency,exceptthattheTO=intervalwouldbeofalowersampling

Page 1916: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

frequency.Whenyouconvertyourdatathisway,PROCEXPANDperformsinterpolationformissingvaluesusingacurve-fittingmethod,whichallowsforconversionbetweenintervalsthataren'tnested.Anestedintervalisonethatfitswhollyinsideanotherinterval(forexample,

Page 1917: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

daysnestwithinweeks,becausethereareexactlysevendaysinaweek,butweeksdonotnestwithinmonths,becausemostmonthshavepartialweeks).Thefollowingprogramwillinterpolateanymissingvaluesinourdatabyfittingacubicsplinefunctionthroughtheexistingdatabefore

Page 1918: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

convertingthedatafromamonthfrequencytoaquarterfrequency.

Example6.8:ConvertingtoaLowerFrequency

PROCEXPANDDATA=book.monthOUT=quarterlyFROM=MONTHTO=QTR;IDdate;CONVERTriders;

Page 1919: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUN;

Obs date

Riders(thousands)After

Interpolation

1 01JAN2003 2679.90

2 01APR2003 2778.30

3 01JUL2003 2999.00

Page 1920: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4 01OCT2003 2993.28

5 01JAN2004 3132.90

6 01APR2004 2928.80

7 01JUL2004 3000.80

8 01OCT2004 2992.80

TheresultingdatasetQUARTERLY(above)haseightobservations,fourforeachyear,

Page 1921: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

synchronizedontheQTRintervalboundaries.Asyoucansee,thedatathatweremissingfromouroriginaldataset(forOctober2003)areinterpolated.

Thesecondmethodenablesyoutoperformsimpleaggregation(addition)without

Page 1922: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

interpolationofmissingvalues.TheAGGREGATEmethodalwaysproducesanexactresultwithoutinterpolation,anditrequiresthattheintervalsbenested.Thisprogramshowstheresultofasimpleaggregationonoursampledata:

Page 1923: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Example6.9:PerformingAggregationwithoutInterpolation

PROCEXPANDDATA=book.monthOUT=annualFROM=MONTHTO=YEAR;IDdate;CONVERTriders/METHOD=AGGREGATE;RUN;

Page 1924: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dateRiders(thousands)

2003 . Thereare2missingobservationsforthisyear,whichyieldsamissing

Page 1925: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

result.

2004 35947.5 Totalacrossall12months.

Example6.10:TheImportanceoftheIDStatementinPROCEXPANDThefollowingPROC

Page 1926: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

EXPANDstephasnoIDstatement.Let'srunitsothatwecanseetheassumptionsthatSASmakesinitsabsence.ThisillustrateswhytheIDstatementisalmostalwaysusedwiththisprocedure:

PROCEXPANDDATA=book.monthOUT=ANNUALFROM=MONTHTO=YEAR;

Page 1927: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CONVERTriders;RUN;

dateRiders(thousands)

01JAN1960 2679.9

01JAN1961 3132.9

Howdidwewindupwithdatafor1960and

Page 1928: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1961whenweuseddatafrom2003and2004?IfthereisnoIDvariabletoindicatethedates,SASwillcreateIDvaluestolabeltheinputrecords,anditwillstartfromitszeropoint,January1,1960.

6.3.4UsingPROCEXPAND

Page 1929: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toInterpolateMissingValuesPROCEXPANDcanalsobeusedtointerpolatemissingvalueswithoutconvertingfrequencies.Therearetwowaystodothis;usetheonethatfitsyoursituation.Ifyouareinterpolatingmissingvaluesat

Page 1930: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specificpointsintime,leaveofftheFROM=andTO=options,butmakesurethatyouuseanIDstatementtoindicatethevariablethatcontainsthetimepointsoftheobservedvalues.Thetimepointsdonothavetobeevenlyspaced,nordoyouneedarecordforeachtimepointwithin

Page 1931: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theinterval.PROCEXPANDwillreadthevaluessuppliedintheIDvariableandwillfitasplinefunctionthroughtheavailabledatapoints.Itthenfillsinthemissingvaluesbasedonthatfittedspline.RememberthatthedataforthemonthsofOctoberandNovemberaremissing

Page 1932: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inoursampletable.Thefollowingprogramdemonstrates:

Example6.11:InterpolatingMissingValues

PROCEXPANDDATA=book.monthOUT=nomiss;IDdate;CONVERTriders;RUN;

Page 1933: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheboldedanditalicizedvaluesinthefollowingtablearetheresultoftheinterpolationperformedbyPROCEXPAND.

dateRiders(thousands)

01JAN2003 2679.90

Page 1934: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

01FEB2003 2421.90

01MAR2003 2704.60

01APR2003 2778.30

01MAY2003 2718.60

01JUN2003 2618.20

01JUL2003 2999.00

01AUG2003 3504.70

Page 1935: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

01SEP2003 3329.40

01OCT2003 2993.28

01NOV2003 2788.68

01DEC2003 2888.60

Thesecondmethodinterpolatesmissingvaluesinatimeserieswithoutconvertingthe

Page 1936: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

observationfrequency.Usethiswhenyouwanttofillinthemissingvaluesandmaintainthesameobservationfrequency.ItrequirestheFROM=option,butleaveofftheTO=option,asshownhere:

PROCEXPANDDATA=book.monthOUT=nomiss2FROM=MONTH;

Page 1937: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IDdate;CONVERTriders;RUN;

Bydefault,theinterpolationisperformedbyfittingthepointstoacubicsplinecurve.YoucanrequestothermethodsofinterpolationwiththeMETHOD=optionintheCONVERT

Page 1938: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statement,andthesearedetailedintheSAS/ETSdocumentation.PROCEXPANDwillignoreobservationsthathavemissingvaluesfortheIDvariable,eveniftherearedatapointsfortheCONVERTvariable(s).Table6.1isasummaryofwhatPROCEXPANDdoes

Page 1939: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whentherearemissingvaluesfortheIDvariableand/orCONVERTdatapoints.

Table6.1:HowPROCEXPANDHandlesInterpolationofMissingValuesinInputData

IDVariable Data

PROCEXPANDWill

Page 1940: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Missing Missing Interpolate

NotMissing

Missing Interpolate

Missing NotMissing

Ignore

6.3.5TheOBSERVED=

Page 1941: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OptionfortheCONVERTStatementinPROCEXPANDAswiththeotherusesofSASdate,time,anddatetimeintervals,thedefaultforPROCEXPANDistoconsiderthevaluesasbeingfromthebeginningof

Page 1942: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theintervalsprovidedintheFROM=andTO=options.Thisisnotalwaysthecasewithreal-worlddata,anditcancauseverydifferentresults,especiallyifthevaluesarenotmeasuredatthebeginningofthegiveninterval(s),ortheydonotrepresentasingleobservedvaluefora

Page 1943: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specificpointintime.YoucancontrolhowtheSASintervalsareusedwiththeOBSERVEDoptionintheCONVERTstatement.TherearesixpossiblevaluesfortheOBSERVED=option,asshowninTable6.2:

Table6.2:Valuesforthe

Page 1944: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=Option

Values Description

BEGINNING Beginningoftheperiod

MIDDLE Middleoftheperiod

END Endofthe

Page 1945: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

period

TOTAL Totalsfortheperiod

AVERAGE Averagesacrosstheperiod

DERIVATIVE Onlyvalidasthe'to'

Page 1946: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valuewhenthecubicsplinefunctionistheconversionmethod

ThesyntaxoftheOBSERVED=optionis:

Page 1947: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1.OBSERVED=valueOR

2.OBSERVED=(from-value,to-value)

valueisoneofthekeywordsintable6.2,fromindicatesthecharacteristicsofthedatafromwhichyou

Page 1948: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

areconverting,andtorepresentsthecharacteristicsofthedataresultingfromtheconversion.Usingform1isthesameasspecifyingthesamevalueforbothfromandto;thatis,OBSERVED=AVERAGEisthesameasOBSERVED=(AVERAGE,AVERAGE).

Page 1949: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Again,ifyoudonotsupplyanOBSERVEDoptionfortheconversion,itisthesameasusingOBSERVED=(BEGINNING,BEGINNING).

Examples6.12and6.13demonstratehowdifferentcombinationsoftheOBSERVED=optionaffectour

Page 1950: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

sampledatawhenweincreaseanddecreasethesamplingfrequency.Theprogrambelowshowsthecodetoincreasethesamplingfrequency.ItalsoshowshowtorenametheoutputvariablesintheCONVERTstatementbyplacinganequalsign(=)afterthedataset

Page 1951: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variableandprovidingthenewvariablenameafterward.

Example6.12:EffectofDifferentValuesforOBSERVED=OptiononIncreasedFrequency

/*TheeffectofthedifferentvaluesfortheOBSERVEDoptionoftheCONVERTstatement

Page 1952: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inPROCEXPANDonincreasedsamplefrequency*/

PROCEXPANDDATA=book.monthOUT=seven1FROM=MONTHTO=WEEK;IDdate;CONVERTriders=beginning/OBSERVED=BEGINNING/*Default*/;RUN;PROCEXPANDDATA=book.monthOUT=seven2FROM=MONTHTO=WEEK;

Page 1953: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IDdate;CONVERTriders=middle/OBSERVED=MIDDLE;RUN;PROCEXPANDDATA=book.monthOUT=seven3FROM=MONTHTO=WEEK;IDdate;CONVERTriders=end/OBSERVED=END;RUN;PROCEXPANDDATA=book.monthOUT=seven4FROM=MONTHTO=WEEK;IDdate;

Page 1954: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CONVERTriders=total/OBSERVED=TOTAL;RUN;PROCEXPANDDATA=book.monthOUT=seven5FROM=MONTHTO=WEEK;IDdate;CONVERTriders=average/OBSERVED=AVERAGE;RUN;PROCEXPANDDATA=book.monthOUT=seven6FROM=MONTHTO=WEEK;IDdate;CONVERTriders=begend/

Page 1955: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=(BEGINNING,END);RUN;PROCEXPANDDATA=book.monthOUT=seven7FROM=MONTHTO=WEEK;IDdate;CONVERTriders=avetot/OBSERVED=(AVERAGE,TOTAL);RUN;

DATAcompare_lo;MERGEseven1seven2seven3seven4seven5seven6seven7;BYdate;LABEL

Page 1956: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginning="BEGINNING"middle="MIDDLE"end="END"average="AVERAGE"total="TOTAL"begend="BEGINNING,END"avetot="AVERAGE,TOTAL";;;FORMATbeginning--avetot7.2;RUN;

PROCREPORTDATA=compare_loNOWDSPLIT='\';COLUMNSdate('OBSERVED=\OptionValue'beginningmiddleend

Page 1957: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

averagetotal);FORMATdateDATE9.;DEFINEdate/display;WHEREDATELT'01MAR2003'd;RUN;

PROCREPORTDATA=compare_loNOWDSPLIT='\';COLUMNSdate('OBSERVED=\OptionValue'begendavetot);FORMATdateDATE9.;DEFINEdate/display;WHEREDATELT'01MAR2003'd;

Page 1958: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUN;

OBSERVED=OptionIstheSameforFROM=andTO=:

date BEGINNING

29DEC2002 2770.19

05JAN2003 2573.61

Page 1959: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12JAN2003 2449.83

19JAN2003 2393.59

26JAN2003 2391.28

02FEB2003 2429.28

09FEB2003 2493.99

16FEB2003 2571.80

23FEB2003 2649.08

Page 1960: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Asyoucansee,theOBSERVED=optionhasaverylargeeffectontheresultsthatPROCEXPANDyields.TheBEGINNINGcolumnisthedefault,andthatistheinterpolationcalculatedifthenumbersaremeasuredatthebeginningofthemonthandconvertedtothe

Page 1961: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

BEGINNINGoftheweek.MIDDLEandENDdothecalculationasifthenumbersweremeasuredandconvertedatthemiddleandtheendofthemonth,respectively.Thatiswhytheinterpolatedvaluesaremissinginthosecolumnsintheabovechart.Thenumbersare

Page 1962: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

notavailableuntilthebeginningoftheinterval(thebeginningoftheweekcontainingthemiddleandend,respectively,ofthemonth).

Ifyouaremeasuringtotals(whichweare,sincethisismasstransitridershipdata),thevaluesareradically

Page 1963: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

different.TOTALmeansthatthenumberbeinginterpolatedisnotrepresentativeofasinglepointintheFROM=interval,butthatitisobtainedacrossthedurationoftheFROM=interval.Therefore,thenumbersinthatcolumnoftheabovetablerepresentthenumberofriders

Page 1964: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

perweek,sincethatistheintervaltowhichweareconvertingourdata.AVERAGEconsidersthenumberstobeaveragesforboththeFROMandTO=intervals.

ItgetsalittlemoreinterestingwhenweusedifferentvaluesfortheOBSERVEDoption,

Page 1965: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

butitisextremelyimportanttorememberthatthesearemathematicallyderivedandnotdata-based,soyouneedtoexercisesomecautionandjudgmentastotheusefulnessofthesenumbers.TheBEGINNING,ENDcombinationtakestheoriginalvalueasthe

Page 1966: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueatthebeginningoftheMONTHinterval,andthen(using,inthiscase,thedefaultSPLINEmethod)returnsthevaluefromthatcurveattheendoftheWEEKinterval.Similarly,theAVERAGE,TOTALcombinationconsiderstheoriginalvaluetobethemonthlyaverage

Page 1967: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andcalculatesaweeklytotalbasedonthedefaultSPLINEmethod.Thereareothermethodsavailabletouseintheconversionofintervals,andIencourageyoutoreadtheSAS/ETSdocumentationformorein-depthinformationaboutconvertingtoahigher

Page 1968: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

samplingfrequency.

DifferentFROMandTOValuesintheOBSERVED=Option

date BEGINNING,END

29DEC2002 2573.61

05JAN2003 2449.83

Page 1969: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12JAN2003 2393.59

19JAN2003 2391.28

26JAN2003 2429.28

02FEB2003 2493.99

09FEB2003 2571.80

16FEB2003 2649.08

23FEB2003 2712.25

Page 1970: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IncontrasttoExample6.12,Example6.13willshowtheeffectoftheOBSERVED=optiononalowersamplingfrequency.Remember,sinceouroriginaldatahadmissingvalues,someinterpolationwilltakeplace.

Example6.13:EffectofDifferentValuesfor

Page 1971: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=OptiononLoweredFrequency

/*TheeffectofthedifferentvaluesfortheOBSERVEDoptionoftheCONVERTstatementinPROCEXPANDwithadecreasedsamplefrequency*/PROCEXPANDDATA=book.monthOUT=annual1FROM=MONTHTO=YEAR;IDdate;

Page 1972: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CONVERTriders=beginning/OBSERVED=BEGINNING/*Default*/;RUN;PROCEXPANDDATA=book.monthOUT=annual2FROM=MONTHTO=YEAR;IDdate;CONVERTriders=middle/OBSERVED=MIDDLE;RUN;PROCEXPANDDATA=book.monthOUT=annual3FROM=MONTHTO=YEAR;IDdate;

Page 1973: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CONVERTriders=end/OBSERVED=END;RUN;PROCEXPANDDATA=book.monthOUT=annual4FROM=MONTHTO=YEAR;IDdate;CONVERTriders=total/OBSERVED=TOTAL;RUN;PROCEXPANDDATA=book.monthOUT=annual5FROM=MONTHTO=YEAR;IDdate;CONVERTriders=average/

Page 1974: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=AVERAGE;RUN;PROCEXPANDDATA=book.monthOUT=annual6FROM=MONTHTO=YEAR;IDdate;CONVERTriders=begend/OBSERVED=(BEGINNING,END);RUN;PROCEXPANDDATA=book.monthOUT=annual7FROM=MONTHTO=YEAR;IDdate;CONVERTriders=avetot/OBSERVED=(AVERAGE,TOTAL);

Page 1975: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUN;

DATAcompare_hi;MERGEannual1annual2annual3annual4annual5annual6annual7;BYdate;LABELbeginning="BEGINNING"middle="MIDDLE"end="END"average="AVERAGE"total="TOTAL"begend="BEGINNING,END"avetot="AVERAGE,TOTAL";;;FORMATbeginning--avetot11.2;

Page 1976: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUN;

PROCREPORTDATA=compare_hiNOWDSPLIT='\';COLUMNSdate('OBSERVED=\OptionValue'beginningmiddleendaveragetotal);FORMATdateYEAR4.;DEFINEdate/display;RUN;

PROCREPORTDATA=compare_hiNOWDSPLIT='\';COLUMNSdate

Page 1977: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

('OBSERVED=\OptionValue'begendavetot);FORMATdateYEAR4.;DEFINEdate/display;RUN;

OBSERVED=OptionIstheSameforFROM=andTO=:

date BEGINNING MIDDLE

Page 1978: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2003 2679.90 2764.22

2004 3132.90 2972.48

BEGINNING,MIDDLE,andENDdon'tgiveusaverygoodideaofyearlyridership,becausetheyareconsideringtheentireridershipasoccurringonthebeginning,middle,orendofeach

Page 1979: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

observationintheFROM=interval.TOTAListhevaluefortheentireyear,andAVERAGEiscalculatedonthemonthlyvalues.However,allofthesevaluesarecalculatedwithinterpolationofthemissingvaluesinOctoberandNovember2003.Below,theinterpolationforthose

Page 1980: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

missingvaluesisstillperformedeventhoughweareusingdifferentvaluesforthefromandtoobservationcharacteristics.OfspecialinterestistheAVERAGE,TOTALcolumn.Itconsidersthemonthlydatatobetheaveragemonthlyridership,soPROCEXPANDinterpolates

Page 1981: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

forthemissingdataandthenprovidesaridershiptotalforeachyearbasedonthataveragemonthlyridership.

DifferentFROMandTOValuesintheOBSERVED=Option

OBSERVED=OptionValue

Page 1982: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date BEGINNING,END

2003 3132.90

2004 3153.51

PROCEXPANDhasmanymorecapabilities,andtheprecedingexamplesgiveonlythemostbasicinformationabouthowtousethis

Page 1983: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

powerfulprocedurewithtimeseriesdata.YoucanrefertothedocumentationforSAS/ETStogetamuchmorecompleteexplanationofPROCEXPANDanditsoptions.

6.4International

Page 1984: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Date,Time,andDatetimeFormatsandInformatsVersion9ofSAShasformatsfordatesandtimesinlanguagesotherthanU.S.English.ItisincludedinBaseSASasapartofNationalLanguageSupport(NLS).Thekey

Page 1985: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toNLSisintheLOCALE=orDFLANG=systemoptions.ThedefaultvaluefortheLOCALEoptionisdefinedintheSASconfigurationfileandissetduringinstallation,butitcanbechangedwithanOPTIONSstatementorinsidetheOPTIONSwindow.The

Page 1986: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LOCALE=optionimplicitlysetstwootheroptionsthatcanaffectdates,times,anddatetimevaluesinSAS.ItwillsettheDATESTYLE=option,whichdetermineshowtheANYDTinformatswillinterpretcharacterstringswheretheorderofmonth,day,andyearisambiguous.The

Page 1987: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DFLANG=optiondefinesthedefaultlanguagethatSASwilluse.

ThereareafewspecificdateformatsforTaiwanese,Japanese,andHebrew,butyoucanconsiderthemajorityofinternationalformatsandinformatsasfalling

Page 1988: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

intooneoftwoinformalcategories:the"EUR"categoryortheNLScategory.Ingeneral,itisrecommendedthatyouusetheNLSformatsandinformatsbecauseyoudonothavetowritecodespecifictoalanguage.ChanginglanguagesusingtheNLSfacilityinSASisa

Page 1989: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

matterofchangingthevalueoftheLOCALE=option,whichbringstheformatsinlinewiththerestoftheSASsession.However,the"EUR"categorycanbeuseful.YoucanselectthelanguagebasedoneithertheDFLANG=systemoptionorbyreplacingthe"EUR"intheformatnamewitha

Page 1990: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specificlanguageabbreviation.Usingthelanguageabbreviationsishandyifyouareworkingwithmanylanguagesonthesameoutput,becausetheyenableyoutospecifythelanguagewithoutregardtoasystemoption,andyoucanusethemexactlywhereyouneedthem,foras

Page 1991: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

longasyouneedthem.

6.4.1"EUR"FormatsandInformatsEachoftheseformatsandinformatscorrespondtoanEnglishlanguageformatorinformat.However,the

Page 1992: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

minimum,maximum,anddefaultwidthsfortheformatorinformataredependentuponthelanguagebeingusedatthetime.Tables6.3and6.4listtheEnglishlanguageformatsandtheirEURformatnames,andtheEURinformats.

Table6.3:International

Page 1993: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatNamesandTheirEnglishLanguageEquivalents

EnglishLanguageFormatName

InternationalFormatName

DATE. EURDFDE.

DATETIME. EURDFDT.

Page 1994: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYY. EURDFDD.

DOWNAME. EURDFDWN.

MONNAME. EURDFMN.

MONYY. EURDFMY.

WEEKDATX. EURDFWKX.

WEEKDAY. EURDFDN.

WORDDATX. EURDFWDX.

Page 1995: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Table6.4:InternationalInformatNamesandTheirEnglishLanguageEquivalents

Informat Description

EURDFDEw. Readsinternationaldatevaluesintheform

Page 1996: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ddmonyy(yy),whereddrepresentsthedayofthemonth,monisthethree-lettermonthabbreviationinthe

Page 1997: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

language

specifiedbytheDFLANG=systemoptionorbytheappropriatethree-letterprefix,and

Page 1998: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yy(yy)isthetwo-orfour-digityear.

EURDFDTw. Readsinternationaldatetimevaluesintheformddmonyy

Page 1999: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

hh:mm:ss.ssorddmonyyyyhh:mm:ss.ss.

EURDFMYw. Readsmonthandyeardatevaluesintheformmonyyormonyyyy.

Page 2000: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Youreplace"EUR"withaspecificthree-letterlanguageprefixinanyoftheaboveformatsorinformatstodefinethelanguagethatyouwanttouse.ThisoverridestheDFLANG=systemoptionandisagoodwaytodisplaydatesinmultiplelanguagessimultaneously.Table

Page 2001: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.5isalistofallthevalidlanguageswiththeirthree-letterprefix.Inaddition,we'llshowtheeffectofusingeachthree-letterprefixontheEURDFWKX.formatbyusingthereferencedateofTuesday,February18,2014.Asacomparison,thetablealsoincludesthereferencedate

Page 2002: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formattedastheEnglishequivalentusingtheWEEKDATX.format.

Table6.5:InternationalDateFormatswithLanguageAbbreviations

LanguagePrefix Language

Page 2003: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AFR Afrikaans

CAT Catalan

CRO Croatian

Page 2004: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CSY Czech

DAN Danish

NLD Dutch

Page 2005: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FIN Finnish

FRA French

Page 2006: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DEU German

HUN Hungarian

ITA Italian

Page 2007: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MAC Macedonian

NOR Norwegian

POL Polish

PTG Portuguese

Page 2008: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RUS Russian

ESP Spanish

Page 2009: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SLO Slovenian

SVE Swedish

FRS Swiss_French

DES Swiss_German

Page 2010: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.4.2NLSFormatsTheoutputfromtheNLSseriesofformatsisdefinedbytheLOCALE=systemoption.Unlikethe"EUR"series,you

Page 2011: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

cannotspecifyalanguage;thelanguageisdefinedbythecurrentvalueoftheLOCALE=option.Usetheseformatswhenyouroutputmightbegeneratedinseverallocationsaroundtheworld,butyoudon'thavetodisplaymultiplelanguageswithinthesameoutput.

Page 2012: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheseformatsworkbyconvertingaSASdate,time,ordatetimevaluetothatofthespecifiedlocaleandthenformattingtheresult.Theseformatsarealsonoteworthyinthattheresultisleft-justified,asopposedtotheright-justificationofmostoftheotherdate,time,anddatetimeformats.

Page 2013: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThisistrueforallODSdestinationsaswellasfortraditionalcolumn-basedoutput.Ofcourse,withODSdestinations,thejustificationofthecolumnwillbeperformedaccordingtoanySTYLEineffect.

Tables6.6,6.7,and6.8listtheNLSformats

Page 2014: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

availablefordates,datetimes,andtimes,respectively.EachtablewillgivetheNLSformatname,adescriptionoftheoutputcreatedbytheformat,thedefaultformatwidth,andwidthrange.Thedescriptionoftheoutputcanalsocontainarecommendedwidth,

Page 2015: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whichmightdifferfromthedefault.Therecommendedwidthistheminimumformatwidthnecessarytodisplayallpossibledate,datetime,ortimevalues,becausethelengthoftheoutputmightexceedthedefaultwidthinthegivenformatforsomelocaleandencoding

Page 2016: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

combinations.Ifyouusetherecommendedwidthsgiveninthetable(s)below,youwillalwaysgetaccurateoutput.Otherwise,youroutputcouldhaveaseriesofasterisks(*****)inplaceofthedatestringthatyouexpected.Ingeneral,itisalwaysbettertospecifyaformatwidth

Page 2017: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatistoolongratherthanonethatistooshort.ODSwillhandlemostjustificationissuesarisingfromoverestimatinghowmanycharacterswillbereturnedfromaformat.Notethatthewidthspecificationsfordatetimeandtimeformatscanaccommodate

Page 2018: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fractionalseconds(w.d),andtheywillbedisplayedwiththelocale-specificdecimalseparator.BecausetherearesomanyNLSformatsavailable,AppendixBwillshowthedifferenceinoutputresultingfromdifferentLOCALEsettingsusingaspecificdate,datetime,andtimeas

Page 2019: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

appropriateforeachoftheNLSformatsinthefollowingthreetables.

Table6.6:NLSDateFormats

FormatName Description

NLDATEw. Displaysthedateasmonthname,day,and

Page 2020: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearinlocalformat.SASwilluseDATE.inlocalformatorabbreviatethemonthnametofittheformatwidthspecified.Itis

Page 2021: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

recommended

thatyouuseaminimumformatwidthof25toensureaccurateoutputacrossallsupportedlanguages.

Page 2022: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATELw. Displaysthedateasmonthname,day,andyearinlocalformat.SASwillabbreviatemonthnameoruseonlynumbersand

Page 2023: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimiterstofittheformat

widthspecified.

NLDATEMw. Displaysthedateasthelocalabbreviation

Page 2024: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formonthalongwiththedayandyear,butwilluseonlynumbersanddelimiterstofittheformatwidthspecified.

Page 2025: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEMDw. Displaysmonthnameandday(noyear)fromadatevalue.SASwillusethelocalabbreviationforthemonthnameifthe

Page 2026: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatwidthcannotaccommodatethefullmonth

name.

NLDATEMDLw. Displaysthefullmonthnameandday

Page 2027: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(noyear)fromadatevalueinlocalformat.SASwillabbreviatethemonthnameoruseonly

numbersanddelimitersfor

Page 2028: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonthanddayiftheformatwidthcannotaccommodatethefullmonthname.

NLDATEMDMw. Displaysthelocal

Page 2029: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

abbreviationforthemonthnameandtheday(noyear)fromadatevalue.SASwilluseonlynumbersanddelimitersfor

Page 2030: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonthanddaytofittheformatwidthspecified.

NLDATEMDSw. Displaysthemonthandday(noyear)fromadatevalue

Page 2031: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usingonlynumbersanddelimiters.

NLDATEMNw. Displaysthemonthnamefromadatevalueinlocalformat.SASwillabbreviate

Page 2032: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

themonthnametofittheformatwidthspecified.

NLDATESw. Displaysthedateinlocalformatusingnumbersanddelimiters

Page 2033: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

only.

NLDATEWw. Displaysadatevalueasdayoftheweekanddateinlocalformat.SASwillabbreviateday-of-week,and/ormonth

Page 2034: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

nameasnecessarytofittheformat

widthgiven.

NLDATEWNw. Displaysadatevalueasthedayoftheweekinlocal

Page 2035: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

format.SASwillabbreviateasnecessarytofittheformatwidthgiven.

NLDATEYMw. Displaysmonthnameandyearfromadate

Page 2036: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueinlocalformat.SASwillabbreviatemonthnameand/oruse2-digityearasnecessarytofit

theformatwidth.

Page 2037: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thatsomeformatwidthsmightbetoosmalltoaccommodatetheabbreviations.Inthatcase,aseriesof

Page 2038: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asterisks(*****)willbe

displayed.

NLDATEYMLw. Displaysadatevalueasthefullmonthnameandtheyearinlocalformat.If

Page 2039: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessary,SASwillabbreviatethemonthnameoruseonlynumbersanddelimitersforthemonthandyearand/ora2-

Page 2040: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

digityeartofittheformatwidthspecified.

NLDATEYMMw. Displaysadatevalueasthelocalabbreviationformonth

Page 2041: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

nameandtheyear.Ifnecessary,SASwilluseonlynumbersanddelimitersforthemonthandyearand/ora2-digityearto

Page 2042: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fittheformatwidth

specified.

NLDATEYMSw. Displaysthemonthandyearfromadatevalueusingonlynumbersand

Page 2043: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimitersinlocalformat.Willuse2-digityearifformatwidthis5or6.

NLDATEYQw. Displaysadatevalueascalendar

Page 2044: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

quarterandyear.Itisrecommendedthatyouuseaminimumformatwidthof20toensureaccurateoutputacross

Page 2045: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

allsupported

languages.

NLDATEYQLw. Displaysadatevalueasthefulllengthforthecalendarquarterandtheyear(forexample,"3e

Page 2046: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

trimestre2014").Awidthof4willdisplaya2-digityear.SASwillabbreviate

asnecessarytofittheformatwidth

Page 2047: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specified.

NLDATEYQMw. Displaysadatevalueasaquarterabbreviationandtheyear(forexample,"T32015").SASwilluse

Page 2048: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

onlynumbersanddelimiterstofittheformatwidthifnecessary.Awidthof4willdisplaya2-

digityear.

Page 2049: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEYQSw. Willdisplaytheyearandcalendarquarterfromadatevalueinlocalformatusingonlynumbersanddelimiters.A

Page 2050: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthof4willdisplaya2-digityear.

NLDATEYRw. Willdisplaythe2-or4-digityearfromadatevalue.

NLDATEYWw. Displaysadate

Page 2051: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueastheweeknumberandtheyear(forexample,"Week152014").weekalgorithmisused(U,V,or

Page 2052: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

W)variesbasedonthevalueofthe

LOCALE=option.theWEEKU.,WEEKV.,andWEEKW.format

Page 2053: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

discussionsinSection2.4.1DateFormats,formoreinformationabouttheweekalgorithms.

Page 2054: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Table6.7:NLSDatetimeFormats

FormatName Description

NLDATMw.d Displaysadatetimevalueasadatetimeinlocalformat.

Page 2055: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMAPw.d Displaysadatetimevalueasmonthname,day,year,andtimeinlocalformat.SASwillabbreviateas

Page 2056: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

necessarytofittheformatwidthspecifiedandmaysubstitutenumbersanddelimitersforthemonthname,day,

Page 2057: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andyear.

NLDATMDTw.d Displaysthedatefromadatetimevaluewiththemonthname,day,andyearinlocalformat.SAS

Page 2058: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willabbreviatethemonthnameorsubstitutenumbersanddelimitersinlocalformatforthemonthname,day,

Page 2059: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andyearasnecessaryto

fitthesuppliedformatwidth.

NLDATMLw.d Displaysthedatefromadatetimevaluewiththe

Page 2060: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

monthname,day,andyearinlocalformat.SASwillabbreviatethemonthnameorsubstitutenumbersand

Page 2061: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimitersinlocalformatforthemonth

name,day,andyearasnecessarytofitthesuppliedformatwidth.

Page 2062: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMMw.d Displaysthedateandtimefromadatetimevalueusingtheabbreviationformonthname.SAS

Page 2063: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willusenumbersanddelimitersforthedateifthe

formatwidthisnotwideenoughandwillfurtherabbreviatethe

Page 2064: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timetohours:minutes,thenjusthoursifnecessary.

NLDATMMDw.d Displaysthemonthanddayfroma

Page 2065: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalue.SASwillabbreviatethemonthnameorusenumbersanddelimitersinlocalformat

Page 2066: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(noyear)iftheformatwidthcannotaccommodatethefullmonthname.

NLDATMMDLw.d Displaysthemonthanddayfroma

Page 2067: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalue.SASwillabbreviatethemonthnameorusenumbersanddelimitersin

localformat

Page 2068: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(noyear)iftheformatwidthcannotaccommodatethefullmonthname.

NLDATMMDMw.d Displaystheabbreviatedmonthname

Page 2069: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddayfromadatetimevalue.SASwillusenumbersanddelimitersinlocalformat(noyear)iftheformat

Page 2070: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthcannotaccommodatethefullmonthname.

NLDATMMDSw.d Displaysthemonthanddayfromadatetimevalueas

Page 2071: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numbersanddelimitersonly(forexample,mm/ddordd/mm).

NLDATMMNw.d Displaysthemonthnameinlocalformat

Page 2072: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fromadatetimevalue.SASwillabbreviateifthefullmonthnamewillnotfitintheformatwidth

Page 2073: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

supplied.

NLDATMSw.d Displaysadatetimevalueasnumbersanddelimitersonly,inlocalformat(forexample,

Page 2074: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

17/05/201413:47:06).

NLDATMTMw.d Displaystimeofdayfromadatetimevalueinlocaltimeformat.

NLDATMTZw.d Displaystime

Page 2075: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofdayfromadatetimevalueinhoursandminutesandthetimezoneoffsetforthelocale.

NLDATMWw.d Displaysadatetime

Page 2076: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueasdayoftheweek,date,andtimeinthelocalformat.Itisrecommendedthatyouuseaminimumformatwidth

Page 2077: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

of49toensureaccurateoutputacross

allsupportedlanguages.

NLDATMWNw.d Displaysthedayofthe

Page 2078: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekfromadatetimevalueinlocalformat.SASwillabbreviateiftheformatwidthistoosmallto

Page 2079: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

accommodatethefulldayofweekname.

NLDATMWZw.d Displaysdayoftheweekanddatetimeinlocalformat.SASwill

Page 2080: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

abbreviateasnecessary.Itisrecommendedthatyouuseaminimumformatwidthof55to

ensureaccurate

Page 2081: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

outputacrossallsupportedlanguages.

NLDATMYMw.d Displaysthemonthnameandyearfromadatetimevalue.SASwill

Page 2082: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

abbreviatethemonthnameand/orusea2-digityeartofittheformatwidthspecified.

NLDATMYMLw.d Displaysthemonthname

Page 2083: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andyearfromadatetimevalue.SASwillabbreviatethemonthnameorusethemonth

number,and

Page 2084: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usea2-digityearifnecessarytofittheformatwidthspecified.

NLDATMYMMw.d Displaystheabbreviated

Page 2085: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

monthnameandyearfromadatetimevalue.SASwillusethemonthnumberanda2-digityearifnecessaryto

Page 2086: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fittheformatwidthspecified.

NLDATMYMSw.d Displaysthemonthandyearfromadatetimevalueusingnumbersand

Page 2087: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

delimitersonly.SASwillusea2-digityearifnecessarytofittheformatwidthspecified.

NLDATMYQw.d Displaysthe

Page 2088: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearandquarteroftheyearfromadatetimevalueinlocalformat.SASwillabbreviatequarterand

Page 2089: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

usea2-digityearifnecessaryto

fittheformatwidthspecified.Itisrecommendedthatyouuseaminimum

Page 2090: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatwidthof20toensureaccurateoutputacrossallsupportedlanguages.

NLDATMYQLw.d Displaysthe

Page 2091: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearandquarteroftheyearfromadatetimevalueinlocalformat.SASwillabbreviatequarter,use

Page 2092: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numbersanddelimitersonly,andusea2-digityearifnecessarytofittheformat

widthspecified.

Page 2093: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMYQMw.d Displaystheyearandquarteroftheyearfromadatetimevalueasanabbreviationinlocalformat.SAS

Page 2094: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willusenumbersanddelimitersonlyandusea2-digityearifnecessarytofittheformatwidthspecified.

Page 2095: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMYQSw.d Displaystheyearandquarteroftheyearfromadatetimevalueinlocalformatasnumbersanddelimiters

Page 2096: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

only.SASwillusea2-digityearifnecessarytofittheformatwidthspecified.

NLDATMYRw.d Displaystheyearfroma

Page 2097: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalue.SASwillusea2-digityearifnecessarytofittheformatwidthspecified.

NLDATMYWw.d Displaysa

Page 2098: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimevalueastheyearandnameoftheweek.SASwillabbreviateweekand/orusea2-digit

Page 2099: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yearifnecessaryto

fittheformatwidthspecified.

NLDATMZw.d Displaysadatetimevalueasadatetime

Page 2100: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringinlocalformatwiththetimezoneoffset.

Table6.8:NLSTimeFormats

FormatName Description

NLTIMAPw.d Displaysa

Page 2101: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timevalueasthetime,followedbyAMorPMinlocalformat.Itisrecommendedthatyouuseaminimum

Page 2102: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatwidth

of22toensureaccurateoutputacrossallsupportedlanguages.

NLTIMEw.d Displaysatimevalueas

Page 2103: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thetimeinlocalformat.

Similartothe"EUR"informats,youcanuseNLSinformatstoprocessdataaccordingtotheLOCALE=systemoption.Table6.9showstheNLSinformatsavailable,theirdefaultwidth

Page 2104: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

specification,thewidthrange,andtheEnglishlanguageinformattowhichitissimilar.

Table6.9:NLSTimeInformats

CategoryFormatName

Date NLDATEw.

Page 2105: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Datetime NLDATMw.

Time NLTIMAPw.

Page 2106: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLTIMEw.

ThewholepointofNLSformatsandinformatsisthatyoudonothave

Page 2107: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toworryaboutthespecificlanguagethatwillbeusingtheformatorinformat.TheLOCALE=systemoptionwilltakecareofit.Inthisway,thesameSASprogramcanbeusedanywhere,andtheoutputwillbeappropriatetothelocallanguageaslongastheLOCALE=system

Page 2108: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

optionhasbeensetcorrectly.ItisimportanttounderstandthatNLSformatsandinformatsworkjustaswellwiththeEnglishlanguageaswithanyotherlanguage,sothere'snoneedtouseSASprogramcodebeyondtheLOCALE=optiontoswitchbetween

Page 2109: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Englishlanguageformats/informatsandotherlanguages,unlessyouneedaspecificformatthatdoesnothaveanNLSequivalent.Also,bearinmindthatanyPICTUREformatsyoucreatefordatesandtimesareNLS-compatiblebydefault,andthedisplayofdate

Page 2110: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ortimecomponentssuchasmonthnamearelocale-sensitive.

6.5NLSDate,Time,andDatetimeConversionFunctions

Page 2111: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Therearethreefunctionstohelpyouworkwithdate,time,anddatetimevaluesandprovideoutputinthelocallanguagebasedontheLOCALE=systemoptionineffect.TheNLDATE(),NLTIME(),andNLDATM()functionstakeadate,time,ordatetimevalue,

Page 2112: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

respectively,andcreateastringvariableaccordingtoaseriesofdateandtimedirectives.ThisisthesameprocessthatoccursifyoucreateacustomformatusingthePICTUREstatement(seeSection2.6),andthencreateacharactervariablebyusingthePUTfunctionwithyour

Page 2113: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

custompictureformatandadate,time,ordatetimevalue.

Example6.14:CreatingaCharacterValueUsingaCustomPictureFormatandthePUT()

Page 2114: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FunctionPROCFORMAT;PICTUREwordmonth(DEFAULT=15)LOW-HIGH='%B%Y'(DATATYPE=DATE);RUN;

DATApicdate;INPUTdate:mmddyy10.;month_and_year=PUT(date,wordmonth.);FORMATdatedate11.;DATALINES;05/15/2016

Page 2115: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

08/01/201210/31/201303/27/2014;RUN;

ODSRTFFILE="ex6.5.1.rtf";PROCPRINTDATA=picdateNOOBSLABELSPLIT='\';labeldate="OriginalDate"month_and_year='MonthandYear\CharacterValue';RUN;

Page 2116: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thisistheresultingdataset.Notethatthenumericdatevariable,whichhasbeenformattedusingtheDATE.format,isright-justified,whilethecharacterstringwecreatedisleft-justified.

OriginalDate

MonthandYearCharacterValue

Page 2117: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

15-MAY-2016

May2016

01-AUG-2012

August2012

31-OCT-2013

October2013

Page 2118: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

27-MAR-2014

March2014

Essentially,thethreeNLSfunctionsdothesamething,butwithouthavingtocreatethepictureformatusingtheFORMATprocedure.

Page 2119: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Youputthedescriptionoftheoutputstringdirectlyintothefunctionasanargument,whichiscalledthedescriptor.Thedescriptorcanalsocontainfixedtextwiththedatedirectives,butthisissomewhatcountertothepurposeofthesefunctions,asthefixedtextyouinsert

Page 2120: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

willnotchangewiththeLOCALE=optionsetting.

TherearemoredatedirectivesavailablewiththesefunctionsthanwiththePICTUREstatementoftheFORMATprocedure.FollowingisthelistofdatedirectivesyoucanusewiththeNLDATE(),

Page 2121: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLTIME(),andNLDATM()functions.Althoughseveralofthedirectivesmayseemidenticaltothoseusedwithpictureformats,thesedirectivesallinsertaleadingzerobydefault,sokeepthatinmindwhenusingthesefunctions.Youwillreceiveanerrorifyoutrytouseatime

Page 2122: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

directivewithNLDATE(),oradatedirectivewithNLTIME().

Table6.10:NLDATE(),NLTIME(),andNLDATM()DateDirectives

DateDirective Description

# removesthe

Page 2123: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzerofromtheresult.

%% Specifiesthepercent(%)character.

%a Locale'sabbreviated(3-character)

Page 2124: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekdayname.

%A Locale'sfullweekdayname.

%b Locale'sabbreviated(3-character)

Page 2125: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

monthname.

%B Locale'sfullmonthnamewithoutpadding.Whenyouspecify'%B

Page 2126: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%d,'therewillalwaysbeonespacebetweenthemonthandday.

%C Locale'sfullmonthnamewithblank

Page 2127: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

padding.Whenyouspecify'%C%d,'therewillbeoneormorespacesbetweenthemonthand

Page 2128: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

day,dependinguponthelengthofthemonth'sname.

%d Zero-paddednumericaldayofthe

Page 2129: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month.Use'#d'tosuppresstheleadingzero.

%e Blank-paddednumericaldayofthe

Page 2130: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

month.Use'#e'tosuppresstheleadingblank.

%F Locale'sfullweekdayname,paddedwith

Page 2131: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

blanks.Ifyouonlywantasinglespacebetweentheweekdaynameandthenextitem,use

Page 2132: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'%A'.

%j Zero-paddeddayoftheyearasadecimalnumber(001–366).Use'#j'tosuppressany

Page 2133: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzeroes.

%H Zero-paddedhour(24-hourclock)asadecimalnumber(00–23).Use'#H'tosuppress

Page 2134: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theleadingzero.

%I Zero-paddedhour(12-hourclock)asadecimalnumber(01–12).Use'#I'tosuppress

Page 2135: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theleadingzero.

%m Zero-paddedmonthasadecimalnumber(01–12).Use'#m'tosuppressthe

Page 2136: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

leadingzero.

%M Zero-paddedminuteasadecimalnumber(00–59).Use'#M'tosuppresstheleadingzero.

Page 2137: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%o Blank-paddedmonthasadecimalnumber(1–12).Use'#o'tosuppresstheleadingblank.

Page 2138: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%p Locale'sequivalentofa.m.orp.m.

%S Zero-paddedsecondasadecimalnumber(00–59).Use'#S'

Page 2139: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tosuppresstheleadingzero.

%u Weekdayasanumberintherange1–7,where1isMondayand

Page 2140: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Sundayis7.

%U Zero-paddedweek-number-of-year(00–53)usingtheUalgorithm,whereSundayis

Page 2141: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

consideredthefirstdayoftheweek.

%V Zero-paddedweek-number-of-year(00–53)usingtheISO8601-

Page 2142: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

standardValgorithm,whichdefinesthefirstweekoftheyearascontainingbothJanuary4

Page 2143: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andthefirstThursdayoftheyear.Therefore,ifthefirstMondayoftheyearfallsonJanuary2,3,or4,

Page 2144: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theprecedingdaysofthecalendaryearareconsideredtobeapartofweek53ofthe

Page 2145: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

previouscalendaryear.

%w Weekdayasanumberintherange0–6where0isSundayand6is

Page 2146: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Saturday.

%W Zero-paddedweek-number-of-year(00–53)usingtheWalgorithm,whichusesMondayas

Page 2147: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thefirstdayoftheweek.

%y Zero-paddedyearwithoutcenturyasadecimalnumber(00–99).

Page 2148: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%Y Yearwithcenturyasadecimalnumber(4-digityear).Theyearrangesfrom1970to2069.

Page 2149: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HerearetheNLSdatefunctions.Whenyouareusingtextforthedescriptor,thedatedirectivesmustbeenclosedinsinglequotes,orSASwilltrytointerpretthemasmacrocalls.

FunctionCall Explanation

NLDATE(SAS- Convertsa

Page 2150: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date-value,descriptor)

SASdatevalueintoacharacterstringintheformdescribedbydescriptorwhichisa

Page 2151: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

combinationofthedirectivesintable6.10enclosedbysinglequotes.descriptormayalsobe

Page 2152: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

acharactervariablecontainingavaliddescriptorstring.

NLDATM(SAS-datetime-value,descriptor)

ConvertsaSASdatetime

Page 2153: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueintoacharacterstringintheformdescribedbydescriptorwhichisacombination

Page 2154: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofthedirectivesintable6.10enclosedbysinglequotes.descriptormayalsobeacharacter

Page 2155: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variablecontainingavaliddescriptorstring.

NLTIME(SAS-time-value,descriptor)

ConvertsaSAStimevalueintoacharacter

Page 2156: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringintheformdescribedbydescriptorwhichisacombinationofthedirectivesin

Page 2157: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

table6.10enclosedbysinglequotes.descriptormayalsobeacharactervariablecontaininga

Page 2158: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

validdescriptorstring.

Table6.11showstheresultsfordifferentOPTIONSLOCALE=settingswhenyouusetheNLDATEfunctiontocreateacharacterstringcontainingthe

Page 2159: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

day-of-weekname(%A),thethree-letterabbreviatedmonthname(%b),numericalday(%d),andfour-digityear(%Y).

Table6.11:TheNLDATEFunction

SampleFunctionCall

Page 2160: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATE('26OCT2014'd,'%A%b%d%Y');

NLDATE('26OCT2014'd,'%A%b%d%Y');

NLDATE('26OCT2014'd,'%A%b%d%Y');

NLDATE('26OCT2014'd,'%A%b%d%Y');

Page 2161: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATE('26OCT2014'd,'%A%b%d%Y');

Table6.12showstheresultsfordifferentOPTIONSLOCALE=settingswhenyouusetheNLDATMfunctiontocreateacharacterstringcontainingtheabbreviatedday-of-

Page 2162: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

weekname(%a),numericaldaywithouttheleadingzero(%#d),thefour-digityear(%Y),andthecolon-delimited24-hourtimewithzero-paddedhoursandminutes(%H:%M).

Table6.12:TheNLDATMFunction

Page 2163: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SampleFunctionCall

NLDATM('06MAY2014:14:30:00'd,'%a%#d%B%Y%H:%M');

NLDATM('06MAY2014:14:30:00'd,'%a%#d%B%Y%H:%M');

Page 2164: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATM('06MAY2014:14:30:00'd,'%a%#d%B%Y%H:%M');

NLDATM('06MAY2014:14:30:00'd,'%a%#d%B%Y%H:%M');

Page 2165: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATM('06MAY2014:14:30:00'd,'%a%#d%B%Y%H:%M');

Table6.13showstheresultsfordifferentOPTIONSLOCALE=settingswhenyouusetheNLTIMEfunctiontocreateacharacter

Page 2166: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

stringcontainingthecolon-delimited12-hourtimewithzero-paddedhoursandminutes(%I:%M)andthelocale'sAM/PMindicator(%p).

Table6.13:TheNLTIMEFunction

SampleFunctionCall

Page 2167: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLTIME('10:17:00'd,'%I:%M%p');

NLTIME('10:17:00'd,'%I:%M%p');

NLTIME('10:17:00'd,'%I:%M%p');

NLTIME('10:17:00'd,'%I:%M%p');

Page 2168: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLTIME('10:17:00'd,'%I:%M%p');

6.6DateFormatsandInformatsforOtherCalendarsSAShastheabilitytohandledatesfromnon-Juliancalendars,such

Page 2169: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

asHebrew,Japanese,andTaiwanese.ThedatevaluescontinuetobestoredasSASdates,whereJanuary1,1960,isequaltozero,buttheseformatshandletheconversiontotheothercalendarsforthecorrectdisplayofdates.

6.6.1Hebrew

Page 2170: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DateFormats

HDATEw.HDATEw.displaysaSASdatevalueinHebrew.Youwillneedthecorrectcharacterencodinginstalledonyoursystemtodisplaythiscorrectly.TheSASdatewillbedisplayedasyyyymmmmmdd,

Page 2171: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whereyyyyistheyear,mmmmmrepresentsthemonth'snameinHebrew,andddistheday-of-the-month.wcanbefrom9to17,withadefaultwidthof17,anditisright-justified.Useoddnumbersforwtogetthebestdisplay.

HEBDATEw.

Page 2172: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HEBDATEw.displaysaSASdatevalueaccordingtotheJewishcalendar.Itisacombinedsolarandlunarcalendar.TheHebrewyeariscalculatedbyadding3761beginninginautumnofaspecifiedyearintheGregoriancalendar.wcanbefrom7to24,witha

Page 2173: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultwidthof16,anditisright-justified.Therearethreeformsofthedisplay,long,default,andshort,dependentupontheformatwidthspecified.Again,youwillneedthecorrectcharacterencodinginstalledonyoursystem,oryouwillgetsubstitutionsfornonprinting

Page 2174: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

characters.

6.6.2JapaneseandTaiwaneseDateFormats

MINGUOw.MINGUOw.displaysaSASdatevalueasaTaiwanesedatevalueintheform

Page 2175: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

yy(yy)mmdd,whereyy(yy)istheyear,mmisthenumberofthemonth,andddisthedayofthemonth.wcanrangefrom1to10,withadefaultwidthof8,anditisleft-justifiedandzero-filled.TheTaiwanesecalendaruses1912asthebaseyear(thatis,01/01/01isJanuary1,1912).

Page 2176: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Datespriortothiswilldisplayasaseriesofasterisks(*****).Also,theyearvaluescontinuetoincreasepast100;theydonotremaintwo-digityearsandcycle,muchlikeJuliandates.Forexample,January1,2012is"100/01/01,"not"00/01/01."

Page 2177: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NENGOw.NENGOw.writesaSASdatevalueintheforme.yymmdd,whereeisthefirstletterofthenameoftheemperor(Meiji,Taisho,Showa,orHeisei),yyistheyear,mmisthemonth,andddisthedayofthemonth.wcanbefrom2to10,withadefault

Page 2178: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

widthof10,anditisleft-justified.SASwillomittheperiodifwisn'tbigenough.

6.6.3JapaneseandTaiwaneseDateInformats

JDATEMYDw.JDATEMYDw.allows

Page 2179: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youtoconvertJapaneseKanjiintheformyy(yy)monddtoSASdatevalues,whereyy(yy)istheyear,monistheKanjirepresentationofthenameofthemonth,andddrepresentsthedayofthemonth.wcanbefrom12to32,withadefaultwidthof12.Youcanseparate

Page 2180: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

(yy)yy,mon,andddwithspecialcharactersorblanks,butyoumustmakesurethatthewidthspecificationallowsforanyblanksand/orspecialcharactersintheinputfield.Two-digityearswillbetranslatedaccordingtotheYEARCUTOFF=option.

Page 2181: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JNENGOw.JNENGOw.readsJapaneseKanjidatevaluesintheformyymmdd,whereyyistheyear,mmistheKanjirepresentationofthenameofthemonth,andddrepresentsthedayofthemonth.Sinceyyistwodigitslong,thisinformatisalways

Page 2182: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

affectedbytheYEARCUTOFF=option.wcanbefrom16to32,withadefaultwidthof16.Youcanseparateyy,mon,andddwithspecialcharactersorblanks,butyoumustmakesurethatthewidthspecificationallowsforanyblanksand/orspecialcharactersin

Page 2183: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theinputfield.

MINGUOw.MINGUOw.convertsaTaiwanesedatevalueintoaSASdatevalueintheformyy(yy)mmdd,whereyy(yy)istheyear,mmisthenumberofthemonth,andddisthedayofthemonth.w

Page 2184: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

canbefrom6to10,withadefaultwidthof6.Youmayuseseparatorssuchasblanks,dashes,orslashesbetweentheyear,month,anddayvalues,buttheymustbepresentbetweenallofthevalues.TheTaiwanesecalendaruses1912asthebaseyear(thatis,01/01/01

Page 2185: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

isJanuary1,1912).Inaddition,theyearvaluescontinuetoincreasepast100;theydonotcycle.January1,2012,is"100/01/01,"not"00/01/01."Youwillgetamissingvalueifyouusethisformattoreaddatestringswheretheyearcomponentislessthan1.

Page 2186: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.7OtherSoftwareandTheirDates(Excel,Oracle,DB2)MostsoftwarepackageskeeptheirdatesinsomesortofnumericalforminmuchthesamewaythatSASdoes,whileothershaveaspecialvariabletype

Page 2187: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fordates.MicrosoftExcelstoresdatesasintegers,butitusesJanuary1,1900,insteadofJanuary1,1960,asdayzero.TimesarestoredinExcelasfractionsofdays,sonoonofagivendayis.5(exactlyone-halfofaday).DatetimevaluesarestoredinExcelasthe

Page 2188: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dayrelativeto01/01/1900plusthefractionoftheday.InExcel,6p.m.onJanuary1,1900,isrepresentedas.75.Excelalsohasamajorlimitationonitsdatealgorithm:Itcannotstoreitsdatesasnegativenumbers.ThismeansthatanydatepriortoJanuary1,

Page 2189: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1900,isgoingtoberepresentedbyacharacterstring,notanExceldatevalue.Therefore,ifyouhavehistoricaldatesinanExcelspreadsheet,youneedtobeawarethatyouwillhavetoprocessanycolumn(s)withhistoricaldatesascharactercolumns,andusetheINPUT()

Page 2190: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functiontocreateyourSASdatevalues.TheANYDTinformatsmayalsoproveusefulinsituationslikethis.IfyourelyonanautomatedmethodofconversionfromExceltoSAS,thereisthepossibilitythatacolumnwithhistoricaldatesmightbetranslatedasanumeric

Page 2191: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

column.Ifthisoccurs,thenanydates(ordatetimes)priortoJanuary1,1900,willbemissinginyourSASdataset.YouwillrunintothesameproblemexportinghistoricaldatesintoExcelfromSAS.Youwillhavetoexportthecolumnasacharactercolumn,andyouwon'tbeableto

Page 2192: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

useanyoftheExceldateormathfunctionsoncellscontainingtheminsidethespreadsheet.

TheseareconversionissuesspecifictoExcelthatmayarisewhenyouaretryingtoimportorexportdatatoorfromExcel.Whenyouimportdatafrom

Page 2193: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

othersoftwarepackagesintoSASusingtheIMPORTprocedure,oneofthedatabaseengines,orwithpass-throughSQLprocessing,SASshouldunderstandandconvertthedates,eventhoughthereferencedatefortheothersoftwaremaydiffer.Thereareexceptionstothisrule,

Page 2194: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

oneofwhichisusingapass-thruWHEREclauseinsidePROCSQLforforeigndatabases.Youwillhavetoknowthedate,time,ordatetimeformatfortheforeigndatabasetoselectrecordsbasedondates,times,ordatetimes.TherearespecificcaseswhereSASdoesnot

Page 2195: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

haveaninformatforcertaindatetimestringsfromotherdatabasesandcannottranslatethosevaluesintotheirSASequivalent.Thegeneralstrategytopursueinthesecasesistoparsethedatetimestringandworkfromtherewithacombinationofinformatsand/or

Page 2196: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functionstocreateyourSASdatetimevalues.Ifyouwillneedtodothisonaregularbasis,youcouldcreateamacroorusePROCFCMPtoprocessyourtroublesomedatetimestrings.

Sendingdatestootherdatabasesandsoftwarepackagesshouldbefine

Page 2197: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ifyouusetheEXPORTprocedureoroneofthedatabaseengines.Ifyouaredeterminedtosenddatestoanotherdatabaseorsoftwarepackagethehardway,thenyouwillhavetoproduceSASdate,time,ordatetimevaluesascharacterstringsintheformatoftheothersoftwareand

Page 2198: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thenimportthemusingthemethodsavailabletoothersoftwarepackages.YoucanuseapictureformatandthePUTstatementtoaccomplishthis,aslongasyouknowthecorrectrepresentationofthepackageforwhichyouarecreatingthedata.Fordetailsoncreatingpicture

Page 2199: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formats,seeSection2.6.Example6.15showshowthisisdoneforaDB2database.

Example6.15:WritingDatetimeValuesforDB2UsingaPictureFormat

PROCFORMAT;

Page 2200: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PICTUREdbdateLOW-HIGH='%Y-%0m-%0d:%0H:%0M:%0S'(DATATYPE=DATETIME).-.Z='0000-00-00:00:00:00';RUN;

DATA_NULL_;now='01JUL2014:20:18:32'dt;PUT"nowdisplayedasdatetimevalue:"@33now;PUT"nowdisplayedasdatetime19.:"@33nowDATETIME19.;

Page 2201: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PUT"nowdisplayedasdbdate.:"@33nowDBDATE.;RUN;

TheResultnowdisplayedasdatetimevalue:1719865112nowdisplayedasdatetime19.:01JUL2014:20:18:32nowdisplayedasdbdate.:2014-07-01:20:18:32

6.7.1The

Page 2202: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASDATEFMT=SystemOptionThissystemoptioncanbeusefulwhenyouareworkingwithoneofthefollowingdatabases:Aster,DB2underUNIXandPCHosts,Greenplum,Impala,Informix,MicrosoftSQLServer,MySQL,Netezza,

Page 2203: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODBC,OLEDB,Oracle,PostgreSQL,SAPHANA,Sybase,SybaseIQ,Teradata,orVertica.ItenablesyoutochangethedateformatofacolumnintheDBMS.ItisusedtoavoiddatatypemismatchesbetweenSASdate,time,ordatetimevaluesandtheDBMSdatecolumns

Page 2204: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

whenimportingtoSASfromaDBMS,orexportingtoaDBMSfromSAS.Thesyntaxisasfollows:

OPTIONSSASDATEFMT=(DBMS-date-column1=SASdate-format1,DBMS-date-column2=SASdate-format2,…

Page 2205: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DBMS-date-columnn=SASdate-formatn);DBMS-date-columnisthenameofadatecolumnintheDBMS,andSASdate-formatisthenameofaSASdate,time,ordatetimeformat.Thisformatmustalsohaveaninformatofthesamenametoworkinthis

Page 2206: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option.Forexample,DATE9.isbothaformatandaninformat,soitwouldbevalid.

Inorderforthisoptiontohaveaneffect,theDBMScolumnmusthaveatypeofDATE,TIME,orDATETIME;anyotherdatatypewillbeignoredbythis

Page 2207: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option.YouusethiswhenthedefaultSASdateformat(whichisDBMS-anddatatype-specific,seetheSAS/ACCESSdocumentationfordetails)doesnotmatchtheSASdateformatyouwantorvice-versa.

Page 2208: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

6.8ConclusionInsummary,whenworkingwithdatesandtimesanddatetimesfromothersoftware,youfirsthavetomakesurethatyouarenotworkingwithcharacterstringsmasqueradingasdates.Ifyouhaveacharacterstring,youwillhavetoconvertit

Page 2209: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

toaSASdate,time,ordatetimeyourselfwiththeINPUT()function(Section3.3.3).Thereversealsoholdstruewhenyouareexportingtootherdatabases.WhilemostofthemethodstotraversebetweendifferentsoftwareandSASwillhandletheimportandexportofdates,times,

Page 2210: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

anddatetimesaccurately,itisalwaysimportanttochecktheresultsatyourdestination.

Page 2211: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AppendixA:AQuickReferenceGuidetoSASDate,Time,andDatetimeFormats

Page 2212: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thistableshowstheresultwhenthesamedate,time,ordatetimevalueisdisplayedwiththecorrespondingformat,usingthedefaultlengthforthegivenformat.

ThereferencedateforthistableisThursday,September18,2014.

Page 2213: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IfYouWantYourDatetoLookLikeThis

UseThisFormat

18SEP14 DATE.

18 DAY.

18/09/14 DDMMYY.

180914 DDMMYYB.

Page 2214: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

18:09:14 DDMMYYC.

18-09-14 DDMMYYD.

18092014 DDMMYYN.

18.09.14 DDMMYYP.

18/09/14 DDMMYYS.

Thursday DOWNAME.

261 JULDAY.

14261 JULIAN.

09/18/14 MMDDYY.

Page 2215: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

091814 MMDDYYB.

09:18:14 MMDDYYC.

09-18-14 MMDDYYD.

09182014 MMDDYYN.

09.18.14 MMDDYYP.

09/18/14 MMDDYYS.

09M2014 MMYY.

09:2014 MMYYC.

Page 2216: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

09-2014 MMYYD.

092014 MMYYN.

09.2014 MMYYP.

09/2014 MMYYS.

September MONNAME.

9 MONTH.

SEP14 MONYY.

3 QTR.

Page 2217: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

III QTRR.

Thursday,September18,2014

WEEKDATE.

Thursday,18September2014

WEEKDATX.

5 WEEKDAY.

Page 2218: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-W37-05

WEEKU.

2014-W38-04

WEEKV.

2014-W37-04

WEEKW.

September18,2014

WORDDATE.

Page 2219: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

18September2014

WORDDATX.

2014 YEAR.

2014M09 YYMM.

2014:09 YYMMC.

2014-09 YYMMD.

14-09-18 YYMMDD.

140918 YYMMDDB.

Page 2220: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

14:09:18 YYMMDDC.

14-09-18 YYMMDDD.

20140918 YYMMDDN.

14.09.18 YYMMDDP.

14/09/18 YYMMDDS.

201409 YYMMN.

2014.09 YYMMP.

2014/09 YYMMS.

2014SEP YYMON.

Page 2221: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014Q3 YYQ.

2014:3 YYQC.

2014-3 YYQD.

20143 YYQN.

2014.3 YYQP.

2014QIII YYQR.

2014:III YYQRC.

2014-III YYQRD.

2014III YYQRN.

Page 2222: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014.III YYQRP.

2014/III YYQRS.

2014/3 YYQS.

2014W37 YYWEEKU.

2014W38 YYWEEKV.

2014W37 YYWEEKW.

Thereferencetime

Page 2223: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thereferencetimeforthistableis2:45p.m.

IfYouWantYourTimetoLookLike UseThis

Page 2224: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

This Format

14:35 HHMM.

15 HOUR.

875 MMSS.

14:35:00 TIME.

2:35:00PM

TIMEAMPM.

14:35:00 TOD.

Thereference

Page 2225: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thereferencedatetimeforthistableis7:20p.m.onFriday,December12,2014.

IfYouWantYourDatetimetoLookLikeThis

UseThisFormat

12DEC14:07:20:00PM

DATEAMPM.

Page 2226: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12DEC14:19:20:00 DATETIME.

12DEC14 DTDATE.

DEC14 DTMONYY.

Friday,12December2014

DTWKDATX.

2014 DTYEAR.

14:4 DTYYQC.

12/12/20147:20PM

MDYAMPM.

Page 2227: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros
Page 2228: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AppendixB:AQuickReferenceGuidetoNLSDate,Time,andDatetimeFormats

Page 2229: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thistableshowstheresultwhenthesamedate,time,ordatetimevalueisdisplayedwiththecorrespondingformat,usingthedefaultlengthforthegivenformat.

ThereferencedateforthistableisMonday,July7,2014.

Page 2230: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName OPTIONSLOCALE=

'English_UnitedStates'

NLDATE. July07,2014

NLDATEL. July7,2014

NLDATEM. Jul7,2014

NLDATEMD. July07

NLDATEMDL. July07

Page 2231: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEMDM. Jul07

NLDATEMDS. 07/07

NLDATEMN. July

NLDATES. 07/07/2014

NLDATEW. Monday,July7,2014

NLDATEWN. Monday

NLDATEYM. July2014

Page 2232: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEYML. July2014

NLDATEYMM. Jul2014

NLDATEYMS. 07/2014

NLDATEYQ. 3rdquarter2014

NLDATEYQL. 3rdquarter2014

NLDATEYQM. Q32014

NLDATEYQS. 2014/3

NLDATEYR. 2014

NLDATEYW. Week272014

Page 2233: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

➊-TheRussiantextistoolongforthedefaultformatwidth.

Thereferencetimeforthistableis11:20.

FormatName

OPTIONSLOCALE=

Page 2234: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'English_UnitedStates'

NLTIMAP. 11:20AM

NLTIME. 11:20:00

ThereferencedatetimeforthistableisSaturday,September27,2014,05:45:00p.m.

Page 2235: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FormatName OPTIONSLOCALE='English_UnitedStates'

NLDATM. 27Sep2014:17:45:00

NLDATMAP. September27,201405:45:00PM

Page 2236: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMDT. September27,2014

NLDATML. September27,201405:45:00PM

NLDATMM. Sep27,201405:45:00PM

NLDATMMD. September27

NLDATMMDL. September27

NLDATMMDM. Sep27

Page 2237: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMMDS. 09/27

NLDATMMN. September

NLDATMS. 09/27/201417:45:00

NLDATMTM. 17:45:00

NLDATMTZ. 17:45:00-0500

NLDATMW. Saturday,September

Page 2238: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

27,201405:45:00PM

NLDATMWN. Saturday

NLDATMWZ. Sat,Sep27,201405:45:00PM-0500

NLDATMYM. September2014

NLDATMYML. September2014

NLDATMYMM. Sep2014

Page 2239: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATMYMS. 09/2014

NLDATMYQ. 3rdquarter2014

NLDATMYQL. 3rdquarter2014

NLDATMYQM. Q32014

NLDATMYQS. 2014/3

NLDATMYR. 2014

NLDATMYW. Week382014

NLDATMZ. 27Sep2014:17:45:00-0500

Page 2240: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

➊-TheRussiantextistoolongforthedefaultformatwidth.

Page 2241: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

AppendixC:TroubleshootingDates101ThisappendixisintendedtobeaquicksolutionsguideforsomeofthemostcommonissuespeoplehavewithdatesandSAS.Ihopetokeepthe

Page 2242: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

discussiongoingonline,becausepeopleoftenremarktomehowdifficultdatesareinSAS,andtheyshouldn'tbe.Youcanfindoutmoreaboutthistroubleshootingprojectontheauthorpageforthisbook.Again,theseareintendedtobesimplesolutionstocommon

Page 2243: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beginner'squestions.

Everypersonexperiencingaproblemwithdates,times,ordatetimesshouldimmediatelychecktoseeiftheyareworkingwithcharactervaluesornumericvalues.YoucandothisbyusingPROCCONTENTS,bylookingatthe

Page 2244: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

propertiesintheExplorerwindowininteractiveSAS,orbyusingtheDataSetAttributesTask(underTasks→Data)inSASEnterpriseGuide.UnlessyouareworkingwithISO8601durationsandintervals,youshouldbeworkingwithnumericvalues.Ifnot,thenthefirststep

Page 2245: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

youshouldtakeistoconvertthatcharactervalueintoaproperSASdate,time,ordatetimevalue.

Question1:HowdoIconvertmycharactervalueintoaSASdate,time,ordatetimevalue?

Case1:Ifyouare

Page 2246: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

readingaflatfile(CSV,tab-delimited,andsoon),youwillneedtousetheINPUTstatementwiththeappropriatedate,time,ordatetimeinformat.

SampleCodeOPTIONSDATESTYLE=MDY;DATAconvert_char;INFILE'char_dates.txt'

Page 2247: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PADMISSOVER;INPUTid$date1:mmddyy10.date2:date9.date3:anydtdte.;RUN;

DataFile'char_dates.txt'

10104/17/201206dec20150627201410209/29/201415JUN201309feb201410311/12/20133mar201508/19/2015

Page 2248: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Whenyouruntheprogramwiththedataabove,youwillgetthefollowingdataset.ThedatevariableshaveintentionallybeenleftunformattedsoyoucanseethatthedataintheflatfilehasbeenturnedintoSASdatevalues.TheANYDTDTE.informatwasusedtoreadintheDATE3

Page 2249: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variabletodemonstratehowitcanbeusedifyoudon'tknowwhatyourcharacterdateslooklike,oriftheyareindifferentdateforms.Chapter3containsdetaileddescriptionsoftheinformatsthatyoucanuseinconjunctionwiththeINPUTstatement.

Page 2250: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheResultingDataSet

Case2:IfyoualreadyhaveyourdatainaSASdataset,thenyouwillhavetousetheINPUTfunctiontotranslateyourcharacterdatesinto

Page 2251: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASdates.

Hereisthedatasetwehavewithcharacterdates:

CodetoConvertCharacterDateValuesintoSASDateValues

1.OPTIONSDATESTYLE=MDY;

Page 2252: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2.DATAfix_char;3.setalready_char;4.num_date1=INPUT(date1,mmddyy10.);5.num_date2=INPUT(date2,date9.);6.num_date3=INPUT(date3,anydtdte11.);7.RUN;

TheINPUTfunctionisusedinlines4,5,and6tocreatethenewvariablesnum_date1,num_date2,and

Page 2253: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

num_date3.Thereasonwearecreatingnewvariablesisthatyoucannotchangeanexistingvariablefromcharactertonumeric,soyoucannotusethevariablenamesdate1,date2,ordate3becausetheyexistinthedatasetALREADY_CHAR.TheINPUTfunctionusestheinformats

Page 2254: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

detailedinChapter3,justliketheINPUTstatement.

TheResultingDataSet

Ingeneral,whenyouaredealingwithSASdates,times,anddatetimes,youshould

Page 2255: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

beworkingwithnumericvariables.TheexceptiontothisruleisISOdurationsandintervals,whichiscoveredinChapter4.

Question2:WhydoIgetthelogmessage"Variablexxxxxxhasbeendefinedasbothcharacterandnumeric"?

Page 2256: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YouaretryingtoconvertacharactervariableintoaSASdate,butyou'reusingthesamevariablenameintheINPUTfunctionandfortheresult.

date=INPUT(date,mmddyy10.);

Instead,youmustuseadifferentvariablename

Page 2257: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tostoretheresultoftheINPUTfunction.

new_date=INPUT(date,mmddyy10.);

Question3:WhydoIgetthelogmessage"NOTE:InvalidargumenttofunctionINPUT"whenItrytousetheINPUTfunctiontoconverta

Page 2258: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

characterdateintoaSASdate?

Youareusingthewronginformatforthecharactersyouareconverting.Asanexample,ifyourcharactervalueslooklike"04MAY2015,"you'llgetthiserrorifyoudon’tusetheDATE.(orANYDTDTE.)

Page 2259: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informat.

Quickestfix:Useoneofthe"ANYDATE"informats.ReadSection3.4.4fordetailsandthepossiblepitfallsofthisstrategy.

Bestfix:Findthecorrectinformatthatfitsthecharacterstringyou'reconverting.

Page 2260: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Question4:WhydoIgeterrorswhenIamreadingaflatfilewithdates,andIknowI'musingthecorrectinformat?

Thisismostoftenanissueofnotreadingenoughcharactersinthefield,orofreadingtoomany.

Page 2261: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Bestfix:Makesureyouspecifythecorrectlengthonyourinformat,andusethecolon(:)modifierforyourinformat(seeExample3.2).

Question5:Whydoesn'tmydatecomparisonwork?

Thisisgenerallya

Page 2262: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

problemwhenyouareimportingdatafromanotherdatabase.First,ensurethatthevariablesyouareworkingwitharenumeric.Second,manydatabasesstoretheirdatesasdatetimevalues,soevenifusingdatesworksinthedatabase,itwillnotworkwhenyoutryto

Page 2263: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

comparethatwithaSASdatevalue.

Bestfix:Onceyou'veverifiedthatyourdataaren'tcharacter,tryusingtheDATEPART()function(Table5.3)onthevariableyou'veimportedandcomparethatwithyourSASdate.

Page 2264: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Question6:Mydatevalueslookright,sowhycan'tgetImydatecomparisontowork?

Thisfrequentlymeansthatoneofyourvaluesischaracter.RemovingformatsfromaSASdatevalueisagoodwaytomakesurethatyou'vegotaSASdate

Page 2265: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

value.Asanexample,ifthedateisin2014,itshouldbeanumberbetween19724and20088.

Question7:HowcanIsubsetmydatabeforeorafteraspecificdate/time/datetime?

Youhavetouseadateliteralsuchas

Page 2266: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

'01JAN2014'd,atimeliteralsuchas'14:00't,oradatetimeliteralsuchas'17OCT2014:07:00'dt.

DATAjunestock;SETsashelp.citiday;WHEREdateBETWEEN'01JUN1988'dAND'30JUN1988'd;RUN;

Thisisonewayto

Page 2267: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

createasubsetofdataforJune1998fromthedatasetSASHELP.CITIDAY.However,don'tlettheformatofthedatefoolyou.Here'sthesamemethodappliedtothedatasetSASHELP.CITIMON.

DATAstock8081;SETsashelp.citimon;

Page 2268: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WHEREdateBETWEEN'01JAN1980'dAND'31DEC1981'd;RUN;

Eventhoughthedatesaremonthly,theyarestillSASdates,soyouhavetospecifytheboundariesascompleteSASdates,notjustamonthandyear.

Question8:HowdoI

Page 2269: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

getadatefromadateandtime?

UsetheDATEPART()functionafteryou'veconvertedittoaSASdatetimevalue.Ifyoujustneedthetime,usetheTIMEPART()function.Thisexampletakesadatetimevalueandseparatesitintodateandtime,

Page 2270: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formattedandunformattedversions.

DATAdate_from_datetime;datetime='8APR2014:16:00'dt;fmt_datetime=datetime;date=DATEPART(datetime);fmt_date=date;time=TIMEPART(datetime);fmt_time=time;FORMATfmt_datetimemdyampm21.fmt_datemmddyy10.fmt_time

Page 2271: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeampm.;RUN;

PROCPRINTDATA=date_from_datetimeNOOBS;RUN;

datetime fmt_datetime

17125920004/8/20144:00PM

Question9:Whydo

Page 2272: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

mydateslooklikeabunchofnumbersthatdon'tmakeanysense,andhowcanIfixit?

Ifyourdatevaluelookslikeanumbersuchas16789,thenitisdisplayingasitsSASdate,andallyouneedtodoistoformatitusingoneofthedate

Page 2273: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

formatsinAppendixA,orifyouneedoneoftheNLSformatstotranslatethedateintoalanguageotherthanEnglish,seeSection6.4andAppendixB.

Question9a:IformattedmydateandnowallIgetisabunchofasterisks(************).What's

Page 2274: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wrong?

IfyourunformattedSASvalueismorethan7digitslong,oryou'veusedadateformatandyougetasterisks,youprobablyhaveadatetimevalue,andyouneedtouseadatetimeformat.SeeAppendixAforthequicklist.

Page 2275: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Question10:Mydatelookslike"06/25/2014,"butIneedtomakeitlooklike"2014/06/25."Iknowit'saSASdatevalueinthedataset,sohowdoIchangeit?

UsetheFORMATstatement.Itdoesnotmatterwhatformatthe

Page 2276: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variablehasassociatedwithitinthedataset.AslongasitisaSASdate,time,ordatetimevalue,youcanchangethedisplaybychangingtheformatintheprocedurewhereyouaredisplayingthevariable.However,ifyougetanerrormessagealongthelinesof"Format$...not

Page 2277: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

found,"thedollarsign($)istellingyouthatthevariableyouaretryingtoformatisacharactervariable.InthatcaseyouwillhavetoconvertittothecorrespondingSASvalue.

Let'sprintoutsomerecordsfromadatasetusingthiscode:

Page 2278: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ODSRTFFILE="apxc_10.rtf";PROCPRINTDATA=book.dailysales(OBS=5);VARdate;RUN;

Obs date

1 22MAY2014

2 23MAY2014

3 24MAY2014

Page 2279: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4 25MAY2014

5 26MAY2014

WhatyouseeinthedatecolumnisjustaSASdatethathasbeenassociatedwiththeDATE9.formatwhenthedatasetwascreated,sowithoutanyFORMATstatementin

Page 2280: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

thePRINTprocedure,thatiswhatitlookslike.Nowwe'lladdaFORMATstatement.

1PROCPRINTDATA=book.dailysales(OBS=5);2VARdate;3FORMATdateyymmdds10.;4RUN;

Obs date

Page 2281: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1 2014/05/22

2 2014/05/23

3 2014/05/24

4 2014/05/25

5 2014/05/26

Andthat'showyouchangethewayadate,time,ordatetimelooksinoutputfromits

Page 2282: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

defaultformat.

Question11:Mydateisnumeric,like20140815.HowdoIgetittodisplayas08/15/2014?

Thisisusuallytheresultofanimportthatwentwrong.First,makesurethatitreallyisthenumber

Page 2283: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20,140,815.Ifthisistrue,thenyoucanturnthatnumberintoacharacterstringwiththePUTfunctionandconvertittoadatevaluelikeyoudowithanyothercharacterdatestring.Here'soneway:

DATAc11;bad_date=20140815;

Page 2284: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

char_date=PUT(bad_date,10.);SAS_date=INPUT(STRIP(char_date),yymmdd8.);fmt_SAS_date=SAS_date;FORMATfmt_SAS_dateweekdate.;RUN;

PROCPRINTDATA=c11NOOBS;RUN;

bad_date char_date

Page 2285: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

20140815 20140815

Question12:HowcanIdisplayjustthemonthandyear,whenIhavemonth,day,andyear?

Bydefinition,SASdatesareaspecificday

Page 2286: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

inaspecificmonthduringaspecificyear,soyoucan'tjustdeletethedayandstorethemonthandyear.Ifyouwanttodisplaythevalueasonlyamonthandayear,thenjustuseaformatthatonlydisplaysthemonthandyear.

Question13:Howdo

Page 2287: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Iconvertfromdatetimeformattodateformat?

Inordertoanswerthisquestion,youneedtoknowwhatyouwant.IfyouneedaSASdatevalue,thenyouneedtoconvertitfromsecondssincemidnight,January1,1960,todayssinceJanuary1,

Page 2288: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1960,byusingtheDATEPARTfunction.Ifyoujustwantittodisplaylikeadate,anddon'tneedorwanttheextravariableinyourdataset,thenyoucanuseaformat.SeeAppendixAfortheavailableSASdatetimeformatsandtheoutputtheyproduce.

Page 2289: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATAq12;datetime='31AUG2014:10:15'dt;fmt_datetime=datetime;date_from_datetime=DATEPART(datetime);fmt_date=date_from_datetime;FORMATfmt_datetimedtwkdatx.fmt_dateweekdatx.;RUN;

PROCPRINTDATA=q12NOOBS;RUN;

Page 2290: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NotethedifferenceintheactualSASvalues.Eventhoughtheformattedvaluesareidentical,thevariabledatetimeisaSASdatetimevalue,whilethevariabledate_from_datetimeisaSASdatevaluecreatedwiththeDATEPARTfunction.

Page 2291: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetime fmt_datetime

1725099300Sunday,31August2014

Question14:Ican'tfindaSASformattomakemydate/time/datetime

Page 2292: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

lookthewayIwant.OR:Ineedtooutputmydatesoitlookslike…

First,checkAppendixAandtheSASdocumentationtoseeifyoucanfindaformatthatmatchesyourneeds.Ifnot,thenusetheFORMATprocedurewiththePICTURE

Page 2293: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

statement.Section2.7ofthebookprovidesallthedetailsyouneedtocreateacustomformat,includingtheSASdatedirectivesthattellSASwhatyouwantyourdate,time,ordatetimetolooklike.Don'tworrythatyoumaybeduplicatinganexistingSASformat;thatwillnotcauseaproblemas

Page 2294: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

longasyoudonotusethesamenameasaSAS-suppliedformat.Youwillseeanoteinthelog,andyourcustomformatwillnotbecreated.

Question15:HowcanIreadadate/time/datetimeformattedlike…?

Page 2295: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Thesimpleansweristouseoneofthe"ANYDATE"informats.Don'tforgetabouttheDATESTYLE=systemoption,andcheckyourresultscarefullybeforeusingthem.The"ANYDATE"informatsareeasy,buttheycanbefooled,evenwhenfour-digityearsareused.SeeSection3.4.4

Page 2296: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fordetails.

Question16:HowcanIconvertaUTCtimevaluetoaspecifictimezone?

Ifyour"timevalue"isadatetime,usetheTZONEU2S()function.ThischangestheSASvalue,sodon'toverwriteyouroriginal

Page 2297: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

valueunlessthisiswhatyouwanttodo.Ifyouhavejustatimevalue,youwillneedtogettheoffsetviatheTZONEOFF()function,whichwillreturntheoffsetfromGMTforthetimezoneyousupply.

DATAq16;datetime="26MAR2014:11:47:00"dt;gmt_datetime=

Page 2298: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONES2U(datetime);RUN;

PROCPRINTDATA=q16NOOBS;FORMATdatetimegmt_datetimemdyampm21.;RUN;

datetime gmt_datetime

3/26/201411:47AM

3/26/20144:47PM

Page 2299: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Question17:HowcanImakeasingledatevariablefromseparatemonth,day,andyear?

TheMDYfunctionwillletyousupplyanumericmonth,day,andyear,anditwillcalculatetheSASdatevalue.Youseethisoftenwhenreading

Page 2300: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Excelfiles,wherethemonth,day,andyearareinseparatecolumns.We'llusein-streamcomma-separateddatainsteadofanExcelfilefortheexample.

1DATAQ17;2INFILEDATALINESDLM=',';3INPUTmonthdayyear;4SAS_date=

Page 2301: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MDY(month,day,year);5fmt_date=SAS_date;6FORMATfmt_datemmddyyd10.;7DATALINES;82,15,201399,6,20141012,17,2013115,22,201412;;;;13RUN;

14PROCPRINTDATA=q17NOOBS;15RUN;

Page 2302: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TheSAS_datecolumnshowstheSASdatevalueresultingfromtheuseoftheMDYfunctioninline4.

month day year SAS_date

2 15 2013 19404

9 6 2014 19972

Page 2303: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12 17 2013 19709

5 22 2014 19865

Question18:Ihaveadateandatime.HowcanImakeaSASdatetimefromthem?

Page 2304: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ThisoccurswhenyouimportdataintoSASandthedateisinonecolumnandthetimeisinanother.TheDHMS()functioniswhatisusedtocreatedatetimesfromdatesandtimes.Althoughyoumightthinkthatyouhavetobreakthetimevalueintohours,minutes,andseconds

Page 2305: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

tousethefunction,rememberthatSAStimesaremaintainedinsecondssincemidnight,soyoucanleavehoursandminutessettozeroandputthetimevariableintothesecondsparameter.Wewillusein-streamdatafortheexample.Payattentiontoline3.Thisishowyoucreatea

Page 2306: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datetimefromaSASdateandaSAStime.

1DATAq18;2INPUTdate:yymmdd10.time:time.;3datetime=DHMS(date,0,0,time);4fmt_date=date;5fmt_time=time;6fmt_datetime=datetime;7FORMATfmt_datemmddyy10.fmt_timetimeampm.fmt_datetimemdyampm21.;

Page 2307: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8DATALINES;92014-8-175:45102014-03-119:06112015-02-2715:43122014-12-1310:0013;14RUN;

15ODSRTFFILE='apxc_18.rtf';16PROCPRINTDATA=q18NOOBS;17RUN;

dateandtimearethe

Page 2308: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASvaluesasreadfromthedata,anddatetimeistheSASvaluecreatedinline3oftheabovecode.Wehavecreatedduplicatevariablesforformatting.

date time datetime

19952 20700 1723873500

Page 2309: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

19793 32760 1710147960

20146 56580 1740670980

20070 36000 1734084000

Page 2310: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Question19:HowcanIcalculatethenumberofdaysbetweentwodates?

Therearemultiplewaystocalculatethedifferencebetweentwodates,times,ordatetimesinSAS.Thisisoneofthebestthingsabouthavingthemrepresentedas

Page 2311: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

numbers.Thesimplestwaytofindthedifferenceistosubtractonedatefromanother.Itisimportantthatyoumakesurethatyouaresubtractingdatesfromdates,timesfromtimes,anddatetimesfromdatetimes.

Question20:HowdoIconvertanumeric

Page 2312: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

SASdatevalueintocharacterformat?

Thefirstquestionyoushouldaskyourselfiswhy.FormattingtheSASdatevaluewillgiveyouthedisplayyouwant.Evenwhenyouareexportingtoothersoftware,aslongasyouputthedateintoaformthattheother

Page 2313: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

softwareunderstands,itshouldworkfine.Youdon'tneedtodothecharacterconversion,andyouwillnotbeabletodoanycalculationswiththatcharactervariable.TheonlytimethatIwouldconvertadatevalueintoacharactervalueiswhenIneedtoputthedateinsideofa

Page 2314: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

longstringoftextsuchas,"ThesubjectstartedthetrialonJanuary15,2012,andmasteredtaskAwithin15minutes."Itdoesn'tmatterifaformatispermanentlyassociatedwiththedatevariableorwhatitlookslikewhenyoulookatthedataset;rememberthatyoucanchangethe

Page 2315: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

wayadateisdisplayedinanyprocedurewhereyoucanuseaFORMATstatement.

Here'sadatasetwithavariablethathasbeenformattedwiththeWORDDATX.format.ThismeansthatanytimethevariableDATEisdisplayedinaSASprocedure,itwill

Page 2316: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

bedisplayedwiththeWORDDATX.formatasshownbelow.

Now,let'srunaPROCPRINT,butIwantto

Page 2317: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

showthedateasDD-MMM-YYYY,soIusetheFORMATstatementtochangethedisplayforthisrun:

PROCPRINTDATA=q20NOOBS;FORMATdatedate10.;RUN;

FormattedwithDATE10.

Page 2318: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

date

30JUN2014

18SEP2014

14MAR2014

18JUL2014

26APR2014

WhatifyouwanttoseeitasYYYY-MM-DD?

Page 2319: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

PROCPRINTDATA=q20NOOBS;FORMATdateyymmddd10.;RUN;

FormattedwithYYMMDDD10.(forcestheseparatortobethedash)

date

2014-06-30

Page 2320: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2014-09-18

2014-03-14

2014-07-18

2014-04-26

ButwhenweremovealltheformatsbyusingaFORMATstatementwithoutaformatname,weseetheactualSAS

Page 2321: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

datevalue:

PROCPRINTDATA=q20NOOBS;FORMATdate;RUN;

FormatRemoved

date

19904

19984

Page 2322: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

19796

19922

19839

IfyoustillwanttoconvertyourSASdateintoacharactervalue,thenyoucanusethePUTfunction.Line2isaprecautiontomakesurethatthecharacter

Page 2323: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

variableislongenoughtoholdthelongestvalue.Notethatyouhavetouseadifferentnameforthecharactervariableyoucreateinline4.

1.DATAq20_plus;2.LENGTHchardate$31;3.SETq20;4.chardate=PUT(date,weekdate.);5.RUN;

Page 2324: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

chardate date

Monday,June30,2014

19904

Thursday,September18,2014

19984

Friday,March14,2014

19796

Page 2325: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Friday,July18,2014

19922

Saturday,April26,2014

19839

Page 2326: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

IndexSymbolsandNumerics&(ampersand)192=(equalssign)207$(dollarsign)61%%datedirective49

A%adatedirective49%Adatedirective49AFRlanguageprefix214ampersand(&)192

Page 2327: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ANYDTDTEw.informat82–83ANYDTDTMw.informatand84–85ANYDTTMEw.informatand85–86DATESTYLE=systemoptionand81,82–83,85–86troubleshooting239

ANYDTDTMw.informat81,84–85automaticmacrovariables186–189

B

Page 2328: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

%bdatedirective49%Bdatedirective49B8601CIw.dinformat78,108B8601DAw.format93B8601DAw.informat104B8601DJw.dinformat79,108B8601DNw.format99B8601DTw.dformat99–100B8601DTw.dinformat109B8601DXw.dformat100–101B8601DZw.dformat102B8601DZw.dinformat110B8601LZw.dformat96B8601TMw.dformat94

Page 2329: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

B8601TMw.dinformat105B8601TXw.dformat94–95B8601TZw.dformat97–98B8601TZw.dinformat105–106

CcalculationsINTCK()functionand151–156INTNX()functionand156–159numberofdaysbetweendates145–149numberofyearsbetween

Page 2330: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

dates146–147CALLIS8601_CONVERT123–136CALLSYMGET()function192–193CALLSYMPUT()function192–193casesensitivity48–49CATlanguageprefix214CATS()function14CATT()function14

CATX()function14characterconstants61

Page 2331: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

characterstringsDATETIMEinformatand63PUT()functionand55–56

charactervariablesINPUT()functionand61–62INPUTC()function61–62PUT()functionand55–56PUTN()functionand55–56

COMPRESS()function14constants,dateandtimeas3–5CONVERTstatement,EXPANDprocedureconvertingtohigherfrequency202–203METHOD=option206

Page 2332: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=option206–212

CROlanguageprefix214CSYlanguageprefix214

D%ddatedirective49,51DANlanguageprefix214DATAstepFORMATstatement10–137%LETstatementand191,192

DATADIF()function145–146DATALINESstatement61

Page 2333: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DATATYPE=option48,51datedirectives,pictureformat50DATE()function187–188&DATEmacrovariable187–188DATEsystemoption7DATEAMPMw.dformat42–43,233DATEPART()function140datetimeformatsand41–42troubleshooting241,242–243,245

datesautomaticmacrovariables

Page 2334: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

186–189CALLSYMPUT()functionand192–193asconstants3–5countersfor1creatingcharacterstrings55–56customformats47–55datetimevaluesand13–14,41–42defaultjustification13Exceland227–228externalrepresentationof2–3formatsfor14–37,92–103

Page 2335: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

graphing194–200Hebrewformats226informatsfor59,61,64–73,81–86,103–111internalrepresentationof2internationalformatsandinformats212–220intervaldefinitions149–151intervalfunctions151–159Japaneseformats226Japaneseinformats226–227quickreference231–233,235–237shiftingintervals159–168%SYSFUNC()macro

Page 2336: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

functionand187–189Taiwaneseformats226Taiwaneseinformats226–227intitles186–187troubleshooting239–251widthspecification5–6,13YEARCUTOFF=systemoptionand57–59

DATESTYLE=systemoptionANYDTDTEw.informatand82–83,85–86ANYDTDTMw.informatand84LOCALE=systemoption

Page 2337: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

and212missingvaluesexample81troubleshooting246

DATESTYLE=DMYsystemoption81–82DATESTYLE=LOCALEsystemoption81DATESTYLE=MDYsystemoption81–82DATESTYLE=YMDsystemoption81–82DATETIME()function138datetimevaluesautomaticmacrovariablesand186–189

Page 2338: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CALLSYMPUT()functionand192–193asconstants3–5customformats47–55dateformatsand13–14,41–42defaultjustification13Exceland227–228externalrepresentationof2–3formatsfor41–46,99–103informatsfor61,78–81,81–82,108–111internalrepresentationof2internationalformatsand

Page 2339: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

informats212–220intervaldefinitions150–151intervalfunctions151–159quickreference234,235–237shiftingintervals159–1682%SYSFUNC()macrofunctionand187–189widthspecification5–6,37,41–42YEARCUTOFF=systemoptionand57–59

DATETIMEw.informatANYDTDTEw.informatand82

Page 2340: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86characterstringsand63

DATETIMEw.dformat43–44,79,213,233DATEw.format15DTDATEw.formatand44internationalformatfor213quickreference231

DATEw.informat64ANYDTDTEw.informat82ANYDTDTMw.informat84ANYDTTMEw.informat85–

Page 2341: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

86DATJUL()function140–141DAY()function138DAYinterval150INTCK()function152INTNX()function158shiftpoint160

DAYw.format15,231DB2databases227–228DDMMYYB.format17,231DDMMYYC.format17,231DDMMYYD.format17,231DDMMYYN.format231DDMMYYP.format17,231

Page 2342: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DDMMYYS.format231DDMMYYw.format15–16,30–31internationalformatfor213quickreference231YYMMDDw.formatand30–31

DDMMYYw.informat64–65ANYDTDTEw.informatand82ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86DATASTYLE=system

Page 2343: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

option81–82DDMMYYxw.format16–17MMYYxw.formatand20YYMMxw.formatand30YYQxw.formatand33–34

DESlanguageprefix214DEUlanguageprefix214DFLANG=systemoption212DHMS()function141–142,247–248dollarsign($)61dot(.)Seeperiod

DOWNAMEw.format17

Page 2344: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

internationalformatfor213quickreference231

DTDATE9.format44DTDATEw.format44DATEwformatand15quickreference233

DTDAYinterval150INTCK()function155INTNX()function158shiftpoint160

DTHOURinterval151,161DTMINUTEinterval151,161DTMONTHinterval151INTNX()function158

Page 2345: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftpoint161DTMONYYw.format44MONYYw.formatand21quickreference233

DTQTRinterval151INTNX()function157,158shiftpoint161

DTRESETsystemoption7DTSECONDinterval151,161DTSEMIMONTHinterval151INTNX()function158shiftpoint161

DTSEMIYEARinterval151INTNX()function157,158

Page 2346: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftpoint161DTTENDAYinterval151INTNX()function158shiftpoint161

DTWEEKinterval150INTNX()function157shiftpoint161

DTWEEKDAYinterval151INTNX()function157shiftpoint161

DTWKDATXw.format45quickreference233

DTYEARinterval151INTNX()function157,158

Page 2347: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftpoint161DTYEARw.format45,233DTYYQCw.format46,233durations,ISO8601116–136

EE8601DAw.format93E8601DAw.informat104E8601DNw.format99E8601DTw.dformat100E8601DTw.dinformat109–110E8601DXw.dformat101–102E8601DZw.dformat102–103E8601DZw.dinformat110–111

Page 2348: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

E8601LZw.dformat96–97E8601LZw.dinformat107E8601TMw.dformat94E8601TMw.dinformat105E8601TXw.dformat95–96E8601TZw.dformat98–99E8601TZw.dinformat106–107equalssign(=)207_ERROR_automaticvariable6–63ESPlanguageprefix214"EUR"formats213–214"EUR"informats213–214EURDFDD.format213

Page 2349: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

EURDFDEw.format213EURDFDN.format213EURDFDTw.format213EURDFDWN.format213EURDFMN.format213EURDFMYw.format213EURDFWDX.format213EURDFWKX.format213Excel(Microsoft)227–228EXPANDprocedurecapabilities200–202CONVERTstatement206–212convertingtohigher

Page 2350: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

frequency202–203FROM=option206,209–212IDstatement204–205interpolatingmissingvalues205–206TO=option206,209–212

externalrepresentation,dateandtime2–3

F%Fdatedirective223FCMPprocedure52–55FINlanguageprefix214

Page 2351: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FOOTNOTEstatement186FORMATprocedurePICTUREstatement47–52troubleshooting246VALUEstatement47–48

FORMATstatementdatedirectivesand50functionality10–13INFORMATstatementand59–60troubleshooting243–244

formats10–13,59custom47–55dateconstantsand4–5fordates14–37

Page 2352: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

fordatetime41–46datetimevalues99–103"EUR"213–214externalrepresentationofdate,timeand2–3graphingdatesand194–200Hebrew226ISO860192–103,117–121Japanese226"NLS"214–220PUT()functionand55–56quickreference231–233Taiwanese226fortime37–41usingwrong62–63

Page 2353: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

FRAlanguageprefix214FROM=option,EXPANDstatement206,209–212FRSlanguageprefix214FUNCTIONstatement52–55functionscalculatingintervals145–149creatingdate,time140–145currentdate,time137–138extraction138–140

Ggraphingdates194–200

Page 2354: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

GregorianyearJULIANw.informat65PDJULG4.informat66PDJULGw.informat22PDJULIw.format22PDJULIw.informat66

H%Hdatedirective49,223HDATEw.format226HEBDATEw.format226Hebrewdateformats226HHMMSSw.informat73–75HHMMw.dformat38,233

Page 2355: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

HMS()function143HOLIDAY()function148–149,170HOUR()function140HOURinterval151INTNX()function158shiftpoint161

HOURw.dformat38–39,233HUNlanguageprefix214

I%Idatedirective49,223IDstatement,EXPANDprocedure204–205

Page 2356: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

imputeddates52–55INFORMATstatement59–61,59–63informatsabout59ANYDTvariants81–86"EUR"213–214fordates64–73fordatetime78–81fordatetimevalues108–111fortime73–78Hebrew226INFORMATstatement59–63ISO8601103–111

Page 2357: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ISO8601durationandinterval121–123JapaneseandTaiwanese226–227"NLS"220usingwrong62–63

INPUT()functionfunctionality3informatsand59,61–62troubleshooting240,241

INPUTstatement60–61INPUTC()function61–62INPUTN()function61–62INTCINDEX()function183INTCK()function159,161–162

Page 2358: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

calculatingintervals167–168WORKINGDAYSinterval175–176

INTCYCLE()function183internalrepresentation,dateandtime2intervalmultipliersgraphsand194–200shiftingintervalsand162–163

intervalsbasicsof149–151creating169–176custom162–163

Page 2359: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTCK()function151–156intervalfunctions176–181INTNX()function156–159ISO8601116–136measuring167–168numberofdaysbetweendates145–149numberofyearsbetweendates146–147retailcalendar181–183shifting159–168

INTFIT()function177–178INTFMT()function178–179INTGET()function179–180INTINDEX()function183

Page 2360: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX()function151,156–160,167–168INTSEAS()function183INTSHIFT()function180–181INTTEST()function181ISO860191–92durationsandintervals116–136formats92–103informats103–111

ITAlanguageprefix214

J%jdatedirective49,223

Page 2361: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Japanesedateformats226Japanesedateinformats226–227JDATEMYDw.informat226–227Jewishcalendar226JNENGOW.informat227Joshi,Bhairav2JULDATE()function138JULDATE7()function138JULDAYw.format17–18,233JuliandateJULDATE()function138JULDATE7()function138

Page 2362: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

JULDAYw.format17–18JULIANw.format18JULIANw.informat65PDGJULI1.format23PDJULG.informat66PDJULG4.informat66PDJULGw.format22PDJULIw.format22–23PDJULIw.informat66

JULIANw.format231JULIANw.informat65ANYDTDTEw.informatand82ANYDTDTMw.informatand84

Page 2363: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ANYDTTMEw.informatand85–86

justificationdateformats13–14PDJULIw.formatand22

KKanjirepresentation227

L%LEFT()macrofunction192LENGTHstatement5–6%LETstatement191,192LISTINGdestination13–14

Page 2364: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

literalvaluesquotationmarksand3YEARCUTOFF=systemoptionand57–59

LOCALE=systemoption111,214–215

M%mdatedirective49,223%Mdatedirective49MAClanguageprefix214macrofunctions,dateandtimein186–189macrovariables

Page 2365: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CALLSYMPUT()functionand192–193datesand185–193quotationmarksand186

MAKEDATE()function52–55MDY()function143,247MDYAMPMw.format46,233MDYAMPMw.dinformat79–80METHOD=option,CONVERTstatement(EXPAND)206MicrosoftExcel227–228MINGUOw.format226MINGUOw.informat227MINUTE()function140

Page 2366: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MINUTEinterval151INTNX()function158shiftpoint161

missingvaluesDATESTYLE=systemoption81EXPANDprocedureand205–206symbolfor50wronginformatsand62–63

MMDDYw.format18MMDDYYxw.formatand19quickreference231YYMMDDw.formatand30–31

Page 2367: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYY10.format11–13,18MMDDYYB.format19,231MMDDYYC.format19,231MMDDYYD.format231MMDDYYN.format231MMDDYYP.format19,231MMDDYYS.format231MMDDYYw.informat65–66ANYDTDTEw.informatand82ANYDTDTMw.informatand84DATESTYLE=systemoptionand81–82

Page 2368: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MMDDYYxw.format19MMYYxw.formatand20YYMMxw.formatand30YYQxw.formatand33–34

MMSSw.dformat39,233MMYYC.format20,231MMYYD.format20,231MMYYN.format20,232MMYYP.format20,232MMYYS.format232MMYYw.format19–20,231MMYYxw.format20MONNAMEw.format14,21internationalformatfor213

Page 2369: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

quickreference232MONTH()function138MONTHinterval150example202–203INTCK()function152INTNX()function158shiftpoint160,165–166

MONTHw.format21,232MONYYw.format21DTMONYYw.formatand44internationalformatfor213quickreference232

MONYYw.informat66ANYDTDTEw.informatand82

Page 2370: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86explanationof66

MSEC8.informat76

N$N8601BAw.dformat119$N8601Bw.informat121–122$N8601Bw.dformat118$N8601EAw.format120$N8601EHw.format120$N8601Ew.format119

Page 2371: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

$N8601EXw.format121$N8601Rw.informat122–123NationalLanguageSupport(NLS)111,212NENGOw.format227NLDlanguageprefix214NLDATE.format235NLDATE()function221–225NLDATEL.format215,235NLDATEM.format235NLDATEMD.format215,235NLDATEMDL.format215,235NLDATEMDM.format216,235NLDATEMDS.format216,235

Page 2372: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEMDT.format217NLDATEML.format217NLDATEMN.format216,235NLDATES.format216,235NLDATEw.format215NLDATEW.format235NLDATEWN.format216,235NLDATEWw.format216NLDATEYM.format216,235NLDATEYML.format216,235NLDATEYMM.format216,235NLDATEYMS.format216,235NLDATEYQ.format216,235NLDATEYQL.format217,236

Page 2373: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NLDATEYQM.format217,236NLDATEYQS.format217,236NLDATEYR.format217,236NLDATEYW.format217,236NLDATM()function221–225NLDATMAPw.format217NLDATMw.format215,217NLS(NationalLanguageSupport)111,212"NLS"formats214–220"NLS"informats220NLTIME.format236NLTIME()function221–225NLTIMEAP.format236

Page 2374: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

NODATEsystemoption7NORlanguageprefix214numericvariablesdate,timeas3–5functionsfrom140–145INPUT()functionand61–62LENGTHstatementand5–6

NWKDOM()function144,170

O%odatedirective223OBSERVED=option,CONVERTstatement(EXPAND)206–212

Page 2375: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=AVERAGEoption,CONVERTstatement(EXPAND)207–212OBSERVED=BEGINNINGoption,CONVERTstatement(EXPAND)207–212OBSERVED=DERIVATIVEoption,CONVERTstatement(EXPAND)207–212OBSERVED=ENDoption,CONVERTstatement(EXPAND)207–212OBSERVED=MIDDLEoption,CONVERTstatement(EXPAND)207–212

Page 2376: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

OBSERVED=TOTALoption,CONVERTstatement(EXPAND)207–212ODSdestinations13–14,214–215OPTIONSINTERVALDS=statement169–176OPTIONSstatementDATE/NODATEsystemoption7LOCALE=systemoption212

OSTIMEmacro76,78OUTLIB=option52–55

Page 2377: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

P%pdatedirective49,223PDFdestination14PDJULG.informat66PDJULG4.informat66PDJULGw.format22PDJULI1.format23PDJULIw.format22–23PDJULIw.informat23,66PDTIME4.informat76period(.)formatsyntaxand16ininformats59

Page 2378: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

missingvaluesand50PICTUREstatement,FORMATprocedure47–52,246POLlanguageprefix214PRINTprocedure11–12PROCstep11PTGlanguageprefix214PUT()function14,17,28,55–56,61–62,221,244–245PUTN()function14,17,28,55–56

Q%QSYSFUNC()macrofunction

Page 2379: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

187QTR()function138QTRinterval150INTNX()function158shiftpoint160

QTRRw.format23–24,232QTRw.format23,232

R&RAWDATEmacrovariable187retailcalendarintervals181–183RMFDUR4.informat76

Page 2380: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

RMFSTAMP8.informat80RTFdestination14RUSlanguageprefix214

S%Sdatedirective49,223samplingfrequencyconvertingtohigher202–203convertingtolower203–205OBSERVED=optionand207–212

SASDATEFMT=systemoption

Page 2381: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

228–229SAS/ETS183,200SAS/GRAPH194–200seasonalityfunctions183SECOND()function140SECONDinterval151INTNX()function158shiftpoint161

SEMIMONTHinterval150INTNX()function158shiftpoint160

SEMIYEARinterval150INTNX()function158shiftpoint160

Page 2382: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftoperators,intervalsand159–162SLOlanguageprefix214SMFSTAMP8.informat80SMRSTAMP8.informat80SQLprocedure193STIMERsystemoption76STIMERw.informat76–77STRIP()function14SVElanguageprefix214&SYSDATEautomaticmacrovariable185,187&SYSDATE9automaticmacrovariable186,187

Page 2383: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

&SYSDAYautomaticmacrovariable186,187%SYSFUNC()macrofunction187–189&SYSTIMEautomaticmacrovariable186,187

TTaiwanesedateformats226Taiwanesedateinformats226–227TENDAYinterval150INTNX()function158shiftpoint160

Page 2384: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

timeautomaticmacrovariables186–189CALLSYMPUT()functionand192–193asconstants3–5countersfor2customformats47–55defaultjustification13Exceland227–228externalrepresentationof2–3formatsfor37–41,92–103informatsfor73–78,81–82,103–111

Page 2385: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

internalrepresentationof2internationalformatsandinformats212–220intervaldefinitions150–151intervalfunctions151–159quickreference233,235–237shiftingintervals159–168%SYSFUNC()macrofunctionand187–189widthspecification5–6,37

TIME()function138timezonefunctions111–115TIMEAMPM11.format11–13TIMEAMPMw.dformat40

Page 2386: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

clockvaluesand37quickreference233

TIMEPART()function41–42,140troubleshooting242–243

TIMEw.informat77–78ANDTDTEw.informatand82ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86

TIMEw.dformat39–40HHMMw.dformatand38quickreference233

Page 2387: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TIMEZONE=option93,111–112TITLEstatement186titles,datein186–187TO=option,EXPANDprocedure206,209–212TODAY()function137TODSTAMP8.informat78TODw.dformat40–41clockvaluesin37quickreference233

troubleshootingdates239–251TU4.informat78two-digityear

Page 2388: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

extractionfunctionsand138–140YEARCUTOFF=systemoptionsand57–59,79

TZONEDSTNAME()function114TZONEDSTOFF()function114TZONEID()function112TZONENAME()function112–113TZONEOFF()function113,246TZONES2U()function113TZONESTTNAME()function114–115TZONESTTOFF()function115

Page 2389: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TZONEU2S()function115,246

UUalgorithm25,35,69,70,139%udatedirective223%Udatedirective49,223

VValgorithm26–27,36–37,69–71,139,223%Vdatedirective223VALUEstatement,FORMATprocedure47–48variables

Page 2390: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Seecharactervariables;macrovariables;numericvariables

WWalgorithm27,37,69–72,139,223%wdatedirective49,223%Wdatedirective223WEEK()function139WEEKinformats69–70WEEKinterval150INTCK()function152INTNX()function157,159

Page 2391: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

shiftpoint160,161–162,163–164

WEEKDATEw.format11–12,24quickreference232WEEKDATXw.formatand24–25

WEEKDATXw.format24–25DTWKDATXw.formatand445internationalformatfor213–214quickreference232

WEEKDAY()function139WEEKDAYinterval150

Page 2392: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTNX()function157shiftpoint160

WEEKDAYw.format25internationalformatfor213quickreference232

WEEKUw.format25–26quickreference232

WEEKUw.informat70–71WEEKVinterval182–183WEEKVw.format26–27quickreference232WEEKWw.formatand27

WEEKVw.informat71–72WEEKWw.format27,232

Page 2393: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

WEEKWw.informat72–73WHEREclause,SQLprocedure228widthspecificationfordateformats5–6,13–14fordatetimeformats5–6,37,41–42fortimeformats5–6,37formatsand13–14informatsand59

WORDDATEw.format28FORMATstatementand50quickreference232

WORDDATXw.format28–29,249

Page 2394: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

internationalformatfor213quickreference232

WORKINGDAYSinterval175–176

Y%ydatedirective49,223%Ydatedirective49,51,223Y2Kproblem185YEAR()function139YEARinterval150INTCK()function152INTNX()function158shiftpoint160,161–162,

Page 2395: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

165–166YEAR10.interval166–167YEARCUTOFF=systemoptionDATJUL()functionand140–141extractionfunctionsand138–140Japanese/Taiwanesedateinformats226–227MDY()function143PDJULGw.formatand22PDJULI.format23two-digityearand57–59,79YYQ()function144–145

Page 2396: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YEARVinterval182YEARw.format29DTYEARw.formatand45quickreference232

YMDDTTMw.dinformat80–81YRDIF()function146–147,155–159YYMM.format232YYMMC.format30,232YYMMD.format30,232YYMMDDB.format32,232YYMMDDC.format32,232YYMMDDD.format32,232YYMMDDN.format32,232

Page 2397: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDP.format32,232YYMMDDS.format232YYMMDDw.format30–31DATESTYLE=systemoption81–82quickreference232

YYMMDDw.informat66–67ANYDTDTEw.informatand82ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86DATESTYLE=systemoptionand81–82

Page 2398: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYMMDDxw.format31–32YYMMxw.formatand30YYQxw.formatand33–34

YYMMN.format30,232YYMMNw.informat67YYMMP.format30,232YYMMS.format232YYMMw.format29YYMMxw.format30YYMONw.format32,232YYQ()function144–145YYQC.format33DDTYQCw.formatand46quickreference233

Page 2399: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

YYQD.format34,233YYQN.format33,233YYQP.format34,233YYQRC.format35,233YYQRD.format35,233YYQRN.format35,233YYQRP.format35,233YYQRS.format233YYQRw.format34,233YYQRxw.format34–35YYQS.format233YYQw.format32–33,233YYQw.informat67–68ANYDTDTEw.informatand

Page 2400: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

82ANYDTDTMw.informatand84ANYDTTMEw.informatand85–86

YYQxw.format33–34YYWEEKU.format233YYWEEKUw.format35–36YYWEEKV.format233YYWEEKVw.format36YYWEEKWw.format37,233

Page 2401: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

TableofContents

1. AboutThisBook

Page 2402: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

2. Acknowledgments3. Chapter1:IntroductiontoDatesandTimesinSAS1. 1.1HowDoesItWork?(January1,1960,andMidnightasZero)

2. 1.2InternalRepresentation

3. 1.3ExternalRepresentation(BasicFORMAT

Page 2403: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Concepts)4. 1.4DateandTimeasNumericConstantsinSAS

5. 1.5LengthandNumericRequirementsforDate,Time,andDatetime

6. 1.6GeneralSASOptionsforDates

4. Chapter2:DisplayingSASDate,Time,and

Page 2404: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatetimeValuesasDatesandTimesasWeKnowThem1. 2.1HowDoIUseaFormat?

2. 2.2HowManyBuilt-InFormatsAreThereforDatesandTimes?

3. 2.3DateFormats,Justification,andODS

4. 2.4Detailed

Page 2405: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DiscussionofEachFormat1. 2.4.1DateFormats

2. 2.4.2TimeFormats

3. 2.4.3DatetimeFormats

5. 2.5CreatingCustomDateFormatsUsingtheVALUEStatement

Page 2406: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

ofPROCFORMAT

6. 2.6CreatingCustomDateFormatsUsingthePICTUREStatementofPROCFORMAT

7. 2.7CreatingCustomFormatsUsingPROCFCMPforProcessing

Page 2407: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

8. 2.8ThePUT()FunctionandFormats

5. Chapter3:ConvertingDatesandTimesintoSASDate,Time,andDatetimeValues1. 3.1AvoidingtheTwo-DigitYearTrap

2. 3.2UsingInformats

3. 3.3The

Page 2408: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INFORMATStatement1. 3.3.1UsingInformatswiththeINPUTStatement

2. 3.3.2InformatswiththeINPUT()Function

3. 3.3.3When

Page 2409: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

theInformatDoesNotMatchtheDataBeingRead

4. 3.4ListingandDiscussionofInformats1. 3.4.1DateInformats

2. 3.4.2TimeInformats

3. 3.4.3

Page 2410: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatetimeInformats

4. 3.4.4The"ANYDATE"SeriesofInformats

5. 3.4.5SoWhyNotJustUsethe"ANYDATE"SeriesofInformats?

6. Chapter4:ISO8601

Page 2411: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Dates,Times,Datetimes,Durations,andFunctions1. 4.1WhatIsISO8601?

2. 4.2ISO8601Formats1. 4.2.1ISODateFormats

2. 4.2.2ISOTimeFormats

3. 4.2.3ISO

Page 2412: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

DatetimeFormats

3. 4.3ISO8601Informats1. 4.3.1ISODateInformats

2. 4.3.2ISOTimeInformats

3. 4.3.3ISODatetimeInformats

Page 2413: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

4. 4.4TimeZoneFunctions1. 4.4.1Introduction

2. 4.4.2TheTIMEZONE=Option

3. 4.4.3ListofTimeZoneFunctions

5. 4.5ISO8601DurationsandIntervals

Page 2414: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1. 4.5.1ISODurationandIntervalRepresentations

2. 4.5.2ISO8601DurationandIntervalFormats

3. 4.5.3ISO8601DurationandInterval

Page 2415: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Informats4. 4.5.4CALLIS8601_CONVERT

6. 4.6Conclusion7. Chapter5:DateandTimeFunctions1. 5.1CurrentDateandTimeFunctions

2. 5.2ExtractingPiecesfromSASDate,Time,andDatetimeValues

Page 2416: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3. 5.3CreatingDates,Times,andDatetimesfromNumbersorOtherInformation1. 5.3.1Introduction

2. 5.3.2ListofFunctionsandTheirDescriptions

4. 5.4CalculatingElapsedTime,

Page 2417: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andtheHOLIDAY()Function1. 5.4.1CalculatingElapsedTimewithDATDIF()andYRDIF()

5. 5.5TheBasicsofSASIntervals1. 5.5.1TheInterval

Page 2418: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CalculationFunctions:INTCK()andINTNX()

6. 5.6ModifyingSASIntervals

7. 5.7CreatingYourOwnSASIntervals

8. 5.8IntervalFunctionsaboutIntervals1. 5.8.1

Page 2419: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

INTFIT(argument-1,argument-2,type)

2. 5.8.2INTFMT('interval','size')

3. 5.8.3INTGET(argument1,argument2,argument3)

4. 5.8.4INTSHIFT('interval')

5. 5.8.5INTTEST('interval')

9. 5.9RetailCalendarIntervals

Page 2420: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

andSeasonality1. 5.9.1RetailCalendarIntervals

2. 5.9.2SeasonalityFunctions

8. Chapter6DeeperintoDatesandTimeswithSAS1. 6.1MacroVariablesandDates

Page 2421: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

1. 6.1.1AutomaticMacroVariables

2. 6.1.2PuttingDatesintoTitles

3. 6.1.3Using%SYSFUNC()toCreateDates,Times,andDatetimesin

Page 2422: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

MacroVariables

4. 6.1.4UsingDatesinMacros

2. 6.2GraphingDates1. Johnny'sSavingsAccount

3. 6.3TheBasicsofPROCEXPAND1. 6.3.1

Page 2423: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

CapabilitiesofPROCEXPAND

2. 6.3.2UsingPROCEXPANDtoConverttoaHigherFrequency

3. 6.3.3UsingPROCEXPANDtoConverttoa

Page 2424: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

LowerFrequency

4. 6.3.4UsingPROCEXPANDtoInterpolateMissingValues

5. 6.3.5TheOBSERVED=OptionfortheCONVERT

Page 2425: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

StatementinPROCEXPAND

4. 6.4InternationalDate,Time,andDatetimeFormatsandInformats1. 6.4.1"EUR"FormatsandInformats

2. 6.4.2NLSFormats

5. 6.5NLSDate,

Page 2426: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Time,andDatetimeConversionFunctions

6. 6.6DateFormatsandInformatsforOtherCalendars1. 6.6.1HebrewDateFormats

2. 6.6.2JapaneseandTaiwaneseDateFormats

Page 2427: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

3. 6.6.3JapaneseandTaiwaneseDateInformats

7. 6.7OtherSoftwareandTheirDates(Excel,Oracle,DB2)1. 6.7.1TheSASDATEFMT=System

Page 2428: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

Option8. 6.8Conclusion

9. AppendixA:AQuickReferenceGuidetoSASDate,Time,andDatetimeFormats

10. AppendixB:AQuickReferenceGuidetoNLSDate,Time,andDatetimeFormats

11. AppendixC:TroubleshootingDates101

Page 2429: The Essential Guide to SAS® Dates and Times1.droppdf.com/...essential-guide-to-sas-r-dates-and... · SAS Institute Inc. For a web download or e-book: Your use of this ... Macros

12. Index