BookmarkSubscribeRSS Feed
SMorrison07
Calcite | Level 5


How can I convert a numeric variable to become a date variable. I have a year variable with values such as 2010, 2011, 2012 and when I try and convert from numeric to date it makes it 1965.

2 REPLIES 2
Linlin
Lapis Lazuli | Level 10

example:

data year;

input year@@;

cards;

2000 2010 2013

;

data dates;

set year;

b_date=mdy(1,1,year);

m_date=mdy(6,15,year);

e_date=mdy(12,31,year);

format b_date m_date e_date mmddyy10.;

proc print;run;

hey_tc
Calcite | Level 5

Hi SMorrison07

A date variable in SAS is just another numeric variable. To be meaningful as a date the value is defined as the number of days between January 1st 1960 and the date the value is representing. Therefore is you take your existing numeric value of the year 2000 and try to use it as a date (e.g. in a date function or formatted for display) this will represent 2000 days from January 1st 1960, which is somewhere in 1965 🙂

To 'convert' it to a date you need to convert it to an appropriate number of days. This is what the function mdy is doing in Linlin's response. This function takes 3 arguments: months, days, years. As you only have the year, you need to decide at what point during that year you want to represent the date, e.g. beginning, middle, end, or any other point you choose.

You can then use your modified value with date formats or functions as you like.

Hope that helps

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 502 views
  • 0 likes
  • 3 in conversation