BookmarkSubscribeRSS Feed
evance1
Calcite | Level 5

Hello,

 

I have a variable called 'Year' that is a character variable with values like '2007'. How can I convert this to a SAS date? Do I need to add a random month and day like July 2nd to all of the values so that I can convert it using MDY? I'm not sure what the best way to do this is.

 

Thanks!

4 REPLIES 4
sbxkoenk
SAS Super FREQ

Hello,

 

MDY function is possible indeed.

 

This is also possible:

data a;        
 jaar = '2007'; 
run;
data b; 
 set a; 
 datum_char = '01JAN'!!strip(jaar); 
 datum_nume = input(datum_char, date9.);
 format datum_nume ddmmyy10.;
run;
proc print data=b; run;

Koen

Tom
Super User Tom
Super User

Do you want a DATE value (number of days) or a DATETIME value (number of seconds)?

 

What day in the year do you want?  If you want a DATETIME value then what time of day on that chosen day?

 

data have;
  year='2017';
run;

data want;
  date = mdy(1,1,input(year,32.));
  datetime = dhms(date,0,0,0);
  format date date9. datetime datetime19.;
run;
Kurt_Bremser
Super User

A date is defined as a specific day within a specific month within a specific year. So you need all three values to get a date value. When you only have a year, you must define a rule for this; this rule can very well be "select a random day", of course.

PaigeMiller
Diamond | Level 26

Okay, let me ask the obvious question. If you have a year value, what is the point of converting it to a datetime value? What can you then do with a datetime value that you couldn't do if you left it as a date?

 

Also, years should be numeric, it makes sense to convert it to a number, 2007. Years should never be character.

--
Paige Miller

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
  • 4 replies
  • 527 views
  • 0 likes
  • 5 in conversation