SAS: Date and Time

Day 1
SAS Date could be tedious with conversion issues,
Thanks to Yue, she helped me to find out the date trick

Date Trick SAS

Problem 1 :   From date Format(Character) 11-APR-2008 to Format(Character)  2008-04-11

jarmoluk / Pixabay

 

 

 

 

 

 

 

 

 

Solution:
(1) Compress Function to remove the  “- “,
(2) Input Function to convert Character date to numerical date9.
(3) Put  Function to convert Numerical date back to Character date(yymmdd10.)

code:
pedt1=compress(pedt, ‘-‘);
pedt2= input(pedt1, date9.);
pedt3= put(pedt2,yymmdd10.);

Problem 2: Convert MonthJan, Feb, Mar… into 01, 02, 03 … or 1,2,3 ….

Solution:
(1)  Convert the Month in numerical with Input function and add value for Date9.
(2)  Month Function to extract the Month (month: only extract single digit)
(3)  Put Function to convert a Numerical number to Character
(4) Optional: add “0” by using Compress function

Code:
mhdmm1=month(input(cats(’01’,mhdmm,’2013′),date9.))
if . < mhdmm1 <10 then mhdmm2= compress(“0” || put(mhdmm1,2.))
if mhdmm1>9 then mhdmm2=put(mhdmm1,2.);

Additional Trick Log Warning Trick:
I got this from Shirsih and Mounika
if Input gives us ERROR or Warning in log File,
we can solve it by adding “??”, it will resolve the issue
rfstdt=input(scan(rfstdtc,1,’T’),??IS8601DA.);

2012-07-09 to Numeric Date:

format qsdt date9.;
qsdate= substr(qsdtc,1,10);
qsdt= input(qsdate, yymmdd10.);

Last Reminder: always add FORMAT for the desired output!

  • Bonus trick: Convert Time or Date between SAS and Excel
SAS date= Excel date - 21916;
format sdate date9.;
sdate=excel_date-21916;
SAS_time = Excel_time * 86400;
SAS_date_time = (Excel_date_time - 21916) * 86400

* reverse operation for Excel;

 

Ref:

https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Conversion-from-Excel-Date-to-SAS-Date/td-p/475824

  One thought on “SAS: Date and Time

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: