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

Hi....I am trying to find the maximum frequency grouped by ID and either the same Year or consecutive Years, That is, when the difference in Years is more than 1, then the group by ID and Year is a new group. This is the part that I am stuck on. Any suggestions. Thanks. 

 

 

data have;
infile datalines dlm=',';
input ID $ Year $ Freq;
datalines; 
1,2015,2
1,2016,5
1,2016,8
1,2018,6
1,2019,4
1,2019,3
1,2020,7
1,2020,3
2,2018,1
2,2018,3
2,2020,4
2,2021,6
;

data want;
infile datalines dlm=',';
input ID $ Year $ Freq max_Freq;
datalines; 
1,2015,2,8
1,2016,5,8
1,2016,8,8
1,2018,6,7
1,2019,4,7
1,2019,3,7
1,2020,7,7
1,2020,3,7
2,2018,1,3
2,2018,3,3
2,2020,4,6
2,2021,6,6
;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


data have;
infile datalines dlm=',';
input ID $ Year  Freq;
datalines; 
1,2015,2
1,2016,5
1,2016,8
1,2018,6
1,2019,4
1,2019,3
1,2020,7
1,2020,3
2,2018,1
2,2018,3
2,2020,4
2,2021,6
;

data temp/view=temp;
 set have;
 by id;
 if first.id then group=1;
 if dif(year)>1 then group+1;
run;

proc sql;
  create table want as
  select id, year, freq, max(freq) as max_freq
  from temp
  group by id, group;
quit ;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20


data have;
infile datalines dlm=',';
input ID $ Year  Freq;
datalines; 
1,2015,2
1,2016,5
1,2016,8
1,2018,6
1,2019,4
1,2019,3
1,2020,7
1,2020,3
2,2018,1
2,2018,3
2,2020,4
2,2021,6
;

data temp/view=temp;
 set have;
 by id;
 if first.id then group=1;
 if dif(year)>1 then group+1;
run;

proc sql;
  create table want as
  select id, year, freq, max(freq) as max_freq
  from temp
  group by id, group;
quit ;
twildone
Pyrite | Level 9
Hi Novinsorin….it worked...thanks for your Help.

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 25. 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
  • 455 views
  • 0 likes
  • 2 in conversation