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!

Super Contributor
Posts: 440

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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: 19,815

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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: 19,815

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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

 

Respected Advisor
Posts: 4,925

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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: 7,808

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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,509

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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

Posted in reply to Astounding

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

Super User
Posts: 11,343

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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,509

Re: How to change specific year in the date variable

[ Edited ]
Posted in reply to psychopedia

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,925

Re: How to change specific year in the date variable

Posted in reply to psychopedia

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
  • 769 views
  • 0 likes
  • 7 in conversation