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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 394 views
  • 5 likes
  • 4 in conversation