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 Month: Jan, 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!
One thought on “SAS Date”