This tutorial explains how to convert datetime to date in SAS, along with examples.
Let's say you have a sample SAS dataset containing datetime values.
data mydata;
input date anydtdtm.;
datalines;
2024-01-29 05:04:43
2024-02-14 12:30:00
2024-03-01 18:45:30
;
run;
data newData;
set mydata;
date_yymmdd10=put(datepart(date), yymmdd10.);
date_mmddyy=put(datepart(date), mmddyy10.);
date_mmddyy2=put(datepart(date), mmddyyd10.);
date9=put(datepart(date), date9.);
date11=put(datepart(date), date11.);
proc print;
run;
The DATEPART function is used to extract date from datetime variable. The PUT function is used to format it in a specific format.
How to Convert DateTime in Character Format into Date
Suppose you have datetime values expressed in character format and you want to extract date from them.
DATA mydata;
INPUT char_var $23.;
DATALINES;
2024-01-29 05:04:43
2024-02-14 12:30:00
2024-03-01 18:45:30
;
RUN;
DATA newData;
SET mydata;
date=put(input(char_var, ANYDTDTE23.), yymmdd10.);
RUN;
The INPUT function converts the character variable to SAS date values. The ANYDTDTE informat reads and extracts the date value from datetime.
Share Share Tweet