Pulling Month and Day Out of a Var Then Transforming into a Date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 143
Accepted Solution

Pulling Month and Day Out of a Var Then Transforming into a Date

Hi. I am trying to pull the date out of a variable which contains underscores. The var contains just month and day so I need to add the current year and turn the var into a date (numeric) var.  I tried to use scan and substr but since the month and day are not always in the same place (one digit day or two digit day) I could not get it to work.

This is what I start with:

Old_Date
_12_Jul
_4_Jul
_1_Aug
_5_Jan
_12_Dec
_1_Nov
_10_Jul
_28_Jul
_5_Dec
_7_Nov
_9_Jul
_28_Jul

This is the desired output:

New_date
07/12/14
07/04/14
08/01/14
01/05/14
12/12/14
11/01/14
07/10/14
07/28/14
12/05/14
11/07/14
07/09/14
07/28/14

Accepted Solutions
Solution
‎09-17-2014 05:21 PM
Trusted Advisor
Posts: 1,228

Re: Pulling Month and Day Out of a Var Then Transforming into a Date

data want;

set have;

day=compress(old_date,,'kd');

month=compress(old_date,,'ka');

new_date=input(cats(day,month,'2014'),date9.);

format new_date mmddyy10.;

run;

View solution in original post


All Replies
Occasional Contributor
Posts: 19

Re: Pulling Month and Day Out of a Var Then Transforming into a Date

compress(old_date,'_'), then concat year to old date, followed by formatting.

Super User
Posts: 19,039

Re: Pulling Month and Day Out of a Var Then Transforming into a Date

Date=input(compress(old_date, '_')||'2014', date9.);

format date mmddyy10.;

Frequent Contributor
Posts: 143

Re: Pulling Month and Day Out of a Var Then Transforming into a Date

Hi Thanks!.  Your code didn't work I got back the error message:   Invalid argument to function INPUT.  Not sure why.

Solution
‎09-17-2014 05:21 PM
Trusted Advisor
Posts: 1,228

Re: Pulling Month and Day Out of a Var Then Transforming into a Date

data want;

set have;

day=compress(old_date,,'kd');

month=compress(old_date,,'ka');

new_date=input(cats(day,month,'2014'),date9.);

format new_date mmddyy10.;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 211 views
  • 0 likes
  • 4 in conversation