Is your data in character form? If not, I would say convert it as such to a character.
Then, you can take advantage of the ANYDTDTE format on an input statement.
For example,
data have;
input date_raw $;
datalines;
20160112
201601
160112
;
run;
data want;
set have;
date_n = input(date_raw,anydtdte8.);
format date_n mmddyy10.;
run;
I think that will handle Case 1 and Case 3.
For Case 2, I would expect a missing value to be returned for date_n (SAS can't get full information from just the year and month).
For those missing rows, you could just concatenate on a "01" (or whatever day of the month you wanted to represent these as), and re-run against the ANYDTDTE format.
data want2;
set want;
if missing(date_n) then date_n = input(strip(date_raw)||"01",anydtdte8.);
run;
Or, you can combine these steps:
data want;
set have;
date_n = coalesce(input(date_raw,anydtdte8.),input(strip(date_raw)||"01",anydtdte8.));
format date_n mmddyy10.;
run;
The coalesce function will return the first non-missing value of the two input statements. The first argument will only be missing when SAS doesn't have year, month, and day that it can find.
A few final notes: this format depends on your LOCALE, e.g. is 16/01/12 YMD , MDY, etc. Check your system option for DATESTYLE and make sure it is set to YMD (to fit your examples).
Also, ask yourself: 16/01/12. Assuming we know this is YEAR/MONTH/DAY, is the year 1916? 2016? 2116? You may need to explore one further piece to this (YEARCUTOFF= option).
So given only what you have, it is not as straight forward and elegant as you probably hope... And it by no means is "indefinitely" into the future.
... View more