DATA Step, Macro, Functions and more

how to populate a date if day or month or year missing ?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

how to populate a date if day or month or year missing ?


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

 

Attachment

Accepted Solutions
Solution
4 weeks ago
PROC Star
Posts: 1,584

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89
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;

View solution in original post


All Replies
Solution
4 weeks ago
PROC Star
Posts: 1,584

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89
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;
Super User
Posts: 6,632

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89

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.);

PROC Star
Posts: 8,149

Re: how to populate a date if day or month or year missing ?

[ Edited ]
Posted in reply to rajeshV89

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

 

Super User
Posts: 10,689

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89
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;
Occasional Contributor
Posts: 15

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89

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;

Occasional Contributor
Posts: 17

Re: how to populate a date if day or month or year missing ?

Posted in reply to rajeshV89

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;

Attachment
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 178 views
  • 2 likes
  • 6 in conversation