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!
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
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;
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.
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.