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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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