SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rajeshV89
Fluorite | Level 6


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

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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

6 REPLIES 6
novinosrin
Tourmaline | Level 20
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;
Astounding
PROC Star

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

art297
Opal | Level 21

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

 

Ksharp
Super User
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;
mahesh146
Obsidian | Level 7

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;

rajeshV89
Fluorite | Level 6

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;

sas-innovate-wordmark-2025-midnight.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. Sign up by March 14 for just $795.


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
  • 6 replies
  • 6227 views
  • 2 likes
  • 6 in conversation