data have;
input gp $ Jan Feb March April May June July August;
datalines; 
a     1     .     2     3     2     4     .     4
a     1     .     .     4     .     .     .     5
a     .     .     .     4     .     5     .     5
b     1     .     2     3     2     4     .     4
b     1     .     .     4     .     .     .     5
b     .     .     .     4     .     5     .     5
c     1     .     2     3     2     4     .     4
c     1     .     .     4     .     .     .     5
c     .     .     .     4     .     5     .     5
d     1     2     2     3     2     4     .     .
d     1     2     3     4     4     5     7     5
d     .     .     .     .     .     .     .     .
; run;
proc print data=have; title 'have'; run; title;
proc sql;
     create table results_1 as
     select distinct gp, sum(Jan) as Jan, Sum(Feb) as Feb, Sum(March) as March, 
                         sum(April) as April, sum(May) as May, sum(June) as June,
                         sum(July) as July, sum(August) as August
        from have
        group by gp;
quit;
proc print data=Results_1; title 'Results_1'; run; title;
 
proc transpose data=Results_1 out=have1(rename=(_name_=month));
    by gp;
run;
proc sort data=have1;
     by gp;
run;
data have2;
length add_month $40.;
retain add_month sgp;
    set have1;
    by gp;
    if first.gp then do;
             n = 0;
             add_month = month;
             sgp = 0;
       end;
    if col1 ^=. then do;
         n+1;
          if n=1 then do; 
              add_month=month;
              sgp+1;
          end;
    end;
    if missing(col1) then  do;
          n=0;
          sgp+1;
          call missing(add_month);
    end;
keep gp sgp add_month month n; 
run;
     
proc sort data= have2 out=have3;
    by gp sgp n;
run;
data results_2 (rename=(add_month=month));
     set have3;
     by gp sgp n;
       if n=0 then delete;
       if last.sgp and not first.sgp then  add_month = catx('_', add_month, month);
       if last.sgp then  output;
       drop sgp month;
run;
proc print data=results_2; var gp month n; title 'results_2'; run;
