BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Arjun_Ram
Calcite | Level 5

Hi,

I have a dataset as below

IDCol1Col2MonYear
101XY12021
101XY22021
101XY22021
101XY32021
101XZ42021
101ZY42021
101XY52021
102XY42021
102ZY42021
102AB52021

 

I want the latest col1 and col2 values based on the latest month and year to be populated. Like below.

IDCol1Col2Col1_2Col2_2MonYear
101XYXY12021
101XYXY22021
101XYXY22021
101XYXY32021
101XZXY42021
101ZYXY42021
101XYXY52021
102XYAB42021
102ZYAB42021
102ABAB52021

 

Any suggestions. Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Welcome to the communities!

 

Given that your dataset is already sorted, a double DO loop does this quite nicely:

data want;
do until (last.id);
  set have;
  by id;
end;
col1_2 = col1;
col2_2 = col2;
do until (last.id);
  set have;
  by id;
  output;
end;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Welcome to the communities!

 

Given that your dataset is already sorted, a double DO loop does this quite nicely:

data want;
do until (last.id);
  set have;
  by id;
end;
col1_2 = col1;
col2_2 = col2;
do until (last.id);
  set have;
  by id;
  output;
end;
run;
Patrick
Opal | Level 21

Using a SQL:

proc sql;
  create table want as
    select 
      l.*,
      r.col1_2,
      r.col2_2
  from 
    have l
    left join
      ( select id, col1 as col1_2, col2 as col2_2
        from have
        group by id, year
        having max(mon)=mon
      ) r
  on l.id=r.id
  order by id, year, mon
  ;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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