Hi Team,
Please help me to get year based on month ......
In Below example ...... How to get Excepting output Column
Group Month Expecting output
Abc 11 2021
Abc 12 2021
Abc 1 2022
Abc 2 2022
Abc 3 2022
Abc 4 2022
Abc 5 2022
Abc 6 2022
Abc 7 2022
Abc 8 2022
Abc 9 2022
Abc 10 2022
Abc 11 2022
Abc 12 2022
Abc 1 2023
Abc 2 2023
BVC 12 2021
BVC 1 2022
BVC 2 2022
BVC 3 2022
BVC 4 2022
BVC 5 2022
BVC 6 2022
BVC 7 2022
BVC 8 2022
BVC 9 2022
BVC 10 2022
BVC 11 2022
BVC 12 2022
BVC 1 2023
BVC 2 2023
You can't derive a year from a month without strict rules about how to do this, which you don't explain. Rather than me guess and potentially get it wrong, please explain the logic that creates the expected column.
Using first/last, lag-function and assuming that the data is sorted by "Group":
data want;
set have;
by Group;
retain Year;
LastMonth = lag(Month);
if first.Group then do;
Year = 2021;
LastMonth = Month - 1;
end;
if Month ^= LastMonth + 1 then do;
Year = Year + 1;
end;
drop LastMonth;
run;
From your data, this should do it:
data want;
set have;
by group;
if first.group then year = 2021;
if month = 1 then year + 1;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.