BookmarkSubscribeRSS Feed
Sharath_naik
Obsidian | Level 7

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

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
andreas_lds
Jade | Level 19

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;

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 554 views
  • 5 likes
  • 4 in conversation