data dates;
input sdate $10.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
run;
if month is missing replace it with 01
if day is missing replace it with 01
need output like this:
2009-10-14
2010-12-24
2001-03-01
2006-06-01
2012-01-01
2002-03-27
2003-02-03
data dates;
input sdate $10.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
run;
data want;
set dates;
new_date=input(sdate,anydtdte21.);
if missing(new_date) then new_date=mdy(1,1,sdate) ;
format new_date yymmdd10.;
run;
data dates;
input sdate $10.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
run;
data want;
set dates;
new_date=input(sdate,anydtdte21.);
if missing(new_date) then new_date=mdy(1,1,sdate) ;
format new_date yymmdd10.;
run;
You already have a character variable with a length of $10. If you want to keep it that way, you could use:
sdate = catt(sdate, '-01-01');
Any extra characters beyond the length of 10 won't fit, and will be dropped.
If you prefer a numeric date that takes advantage of SAS's date-handling capabilities, you would need to convert that result:
sasdate = input(sdate, yymmdd10.);
You could use:
options datestyle=ymd;
data dates;
infile cards truncover;
informat sdate anydtdte10.;
input @;
if length(_infile_) eq 4 then _infile_=catt(_infile_,'-01');
else if length(_infile_) eq 5 then _infile_=catt('2018-',_infile_);
else if length(_infile_) lt 2 then _infile_='2018-01-01';
input sdate;
format sdate date9.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
09-07
2012
2002-03-27
2003-02-03
;
However, you never mentioned what year you want populated in case year is missing.
Art, CEO, AnalystFinder.com
data dates;
input sdate $10.;
year=scan(sdate,1,'-','m');
month=coalescec(scan(sdate,2,'-','m'),'01');
day=coalescec(scan(sdate,3,'-','m'),'01');
want=input(cats(year,month,day),yymmdd10.);
format want yymmdd10.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
run;
proc print noobs;run;
DATA TEMP(DROP=TEMP_MONTH TEMP_DATE);
input sdate $10.;
Year=SCAN(sdate,1,'-');
TEMP_Month=SCAN(sdate,2,'-');
TEMP_Date=SCAN(sdate,3,'-');
IF TEMP_MONTH NE . THEN MONTH=TEMP_MONTH;
ELSE MONTH='01';
IF TEMP_Date NE . THEN DATE=TEMP_Date;
ELSE DATE='01';
FINAL_DATE= MDY(MONTH,DATE,YEAR);
FORMAT FINAL_DATE worddate15.;
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
RUN;
here i hot one solution:
data want;
input sdate $10.;
format sdate_new yymmdd10.;
sdate_new=input(sdate,nd8601da.);
cards;
2009-10-14
2010-12-24
2001-03
2006-06
2012
2002-03-27
2003-02-03
;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.