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-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1103 views
  • 0 likes
  • 5 in conversation