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;

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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