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!
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.
Sure, what does your variable look like, is it a SAS date?
if year(date)=2074 then date=mdy(month(date), day(date), 2014);
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.
Post your code and log as well as a proc contents from your dataset.
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 ...
@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 arrayfor some reason.
Then show your code and the portion of the log including the code.
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.
some of the date is 2074, which should be 2014. The date has the date9. format
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
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 ...
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.