BookmarkSubscribeRSS Feed
psychopedia
Calcite | Level 5

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!

11 REPLIES 11
ChrisBrooks
Ammonite | Level 13

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.

Reeza
Super User

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

 

if year(date)=2074 then date=mdy(month(date), day(date), 2014);
psychopedia
Calcite | Level 5

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.

Reeza
Super User

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

 

PGStats
Opal | Level 21

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
Kurt_Bremser
Super User

@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.

Astounding
PROC Star

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.

psychopedia
Calcite | Level 5

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

ballardw
Super User

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

Astounding
PROC Star

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

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 7536 views
  • 0 likes
  • 7 in conversation