How to Extract Date from DateTime in SAS

Deepanshu Bhalla Add Comment

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;
How to Extract Date from DateTime in SAS

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.

Convert DateTime in Character Format into Date in SAS
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Extract Date from DateTime in SAS"
Next → ← Prev