How to change specific year in the date variable

Reply
Occasional Contributor
Posts: 6

How to change specific year in the date variable

Hi ! I have this date variable in my dataset. Some of the year in the date is wrong and I want to change it. For instance, some of the date value has year 2074, but I want to change it to 2014. Is there anyway I can do that? Thank you!

Regular Contributor
Posts: 177

Re: How to change specific year in the date variable

The easiest way to change the year portion of a date is with by using the intnx function intnx function reference

 

You can use negative values for the increment as well as positive ones.

Super User
Posts: 17,737

Re: How to change specific year in the date variable

Sure, what does your variable look like, is it a SAS date?

 

if year(date)=2074 then date=mdy(month(date), day(date), 2014);
Occasional Contributor
Posts: 6

Re: How to change specific year in the date variable

It has a date9. format. I tried your method first, but the error shows 

ERROR: Undeclared array referenced: year.
ERROR: Variable year has not been declared as an array

for some reason.

Super User
Posts: 17,737

Re: How to change specific year in the date variable

Post your code and log as well as a proc contents from your dataset. 

 

Respected Advisor
Posts: 4,640

Re: How to change specific year in the date variable

I think the only way to get that error is to use brackets [] or braces {} instead of parentheses () to call function Year.

 

if year[date] = 2074 then ...

PG
Super User
Posts: 6,927

Re: How to change specific year in the date variable


psychopedia wrote:

It has a date9. format. I tried your method first, but the error shows 

ERROR: Undeclared array referenced: year.
ERROR: Variable year has not been declared as an array

for some reason.


Then show your code and the portion of the log including the code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,069

Re: How to change specific year in the date variable

Should all the dates be 2014?  If not, how do you know what to change and what to change it to?  One example isn't enough to illustrate the changes that need to be made.

Occasional Contributor
Posts: 6

Re: How to change specific year in the date variable

some of the date is 2074, which should be 2014. The date has the date9. format

Super User
Posts: 10,460

Re: How to change specific year in the date variable

Assuming your date variables are actually SAS dates this is one way.

data want;
  set have;
  if year(date) = 2074 then date=mdy(month(date), day(date),2014);
run;

The MDY function builds a SAS date value from a month, day of month and year. The functions Month and Day give you those bits.

 

I

Super User
Posts: 5,069

Re: How to change specific year in the date variable

[ Edited ]

Just in case you find other cases ... maybe 2073 should become 2013 ... this might be a little more generalizable:

 

data want;

set have;

if year(date) = 2074 then date = intnx('year', date, -60, 'same');

run;

 

If 2073 should be 2013, you could change just the IF condition:

 

if year(date) in (2013, 2014) then ...

Respected Advisor
Posts: 4,640

Re: How to change specific year in the date variable

The 60 year difference probably occurs as a result of an import from Excel or MS-Access. The reference date in MS-Office is Jan 01 1900 (which has value = 1) and is Jan 01 1960 in SAS (which has value = 0). The difference between the two systems is actually 60 years and one day (a difference in value of 21916). You should thus use

 

correctDate = ExcelDate - 21916;

PG
Ask a Question
Discussion stats
  • 11 replies
  • 620 views
  • 0 likes
  • 7 in conversation