BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_learneromg
Fluorite | Level 6

I need help formatting a date as it's not something I have experience with. My variables startdate and enddate are coming in as 28Jan2000:00:00:00.000 is the data set I've been provided. How I go about making the dates just mmddyyyy and then keeping only those with a start date between 2006 and 2010?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Assuming your variable is numeric with a datetime format then you can:

 

  • Use DATEPART() to extract the date portion only
  • Apply a format to have it displayed as a date
  • Use the YEAR() function in an IF statement to filter the dates
data want;
set have;

*extracts date portion;
date_value = datepart(dateTime);
*format, primarily for display purposes;
format date_value mmddyy10.;

*filters;
if year(date_value)  >= 2006 & year <=2010;

run;

@SAS_learneromg wrote:

I need help formatting a date as it's not something I have experience with. My variables startdate and enddate are coming in as 28Jan2000:00:00:00.000 is the data set I've been provided. How I go about making the dates just mmddyyyy and then keeping only those with a start date between 2006 and 2010?


If your datetime value is a character you will first need to convert it to a numeric datetime value:

dateTimeVar = input(charVariable, anydtdtm.);
format dateTimeVar datetime.;

View solution in original post

2 REPLIES 2
Reeza
Super User

Assuming your variable is numeric with a datetime format then you can:

 

  • Use DATEPART() to extract the date portion only
  • Apply a format to have it displayed as a date
  • Use the YEAR() function in an IF statement to filter the dates
data want;
set have;

*extracts date portion;
date_value = datepart(dateTime);
*format, primarily for display purposes;
format date_value mmddyy10.;

*filters;
if year(date_value)  >= 2006 & year <=2010;

run;

@SAS_learneromg wrote:

I need help formatting a date as it's not something I have experience with. My variables startdate and enddate are coming in as 28Jan2000:00:00:00.000 is the data set I've been provided. How I go about making the dates just mmddyyyy and then keeping only those with a start date between 2006 and 2010?


If your datetime value is a character you will first need to convert it to a numeric datetime value:

dateTimeVar = input(charVariable, anydtdtm.);
format dateTimeVar datetime.;
SAS_learneromg
Fluorite | Level 6
Thank for the explanation!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 771 views
  • 0 likes
  • 2 in conversation