BookmarkSubscribeRSS Feed
bollibompa
Quartz | Level 8

Hi

are there any easy way to keep the last

two observations within a group?

Thanks

thomas

8 REPLIES 8
Reeza
Super User

Do you have another variable that controls the sort as well as your group variable?

Astounding
PROC Star

Easy?  No.  But at least it's not terribly lengthy:

data want;

   total_count=0;

   do until (last.group);

      set have;

      by group;

      total_count + 1;

   end;

   running_count=0;

   do until (last.group);

      set have;

      by group;

      running_count + 1;

      if running_count >= total_count - 1 then output;

   end;

   drop running_count total_count;

run;

Good luck.

stat_sas
Ammonite | Level 13

data have;
input group a $ b c;
datalines;
1 A 23 20
1 B 34 32
1 C 29 12
1 D 24 10
2 E 45 65
2 F 87 25
2 G 89 76
;

data want;
set have;
by group;
if first.group then cnt=1;
else cnt+1;
run;

proc sql;
select * from want
group by group
having cnt>max(cnt)-2
order by group,cnt;
quit;

data_null__
Jade | Level 19

Consider this PROC SUMMARY.

proc summary data=sashelp.class nway;
  
class age;
   output out=last2 idgroup(last obs out[2](_all_)=);
   run;

8-13-2014 10-57-19 AM.png

Or using _OBS_: from this step consider this.

data classL2;
   set last2(keep=age _obs:);
   array x
  • _obs:;
  •    do i = dim(x) to 1 by -1;
         
    if missing(x) then continue;
          point = x;
          obs = x;
         
    set sashelp.class point=point;
          output;
         
    end;
      
    drop _obs: i;
       run;

    8-13-2014 11-01-00 AM.png
    Ksharp
    Super User

    How about :

    data have;
    input group a $ b c;
    datalines;
    1 A 23 20
    1 B 34 32
    1 C 29 12
    1 D 24 10
    2 E 45 65
    2 F 87 25
    2 G 89 76
    ;
    run;
    data want(drop=_group);
     merge have have(keep=group rename=(group=_group) firstobs=3);
     if group ne _group;
    run;
    

    Xia Keshan

    Haikuo
    Onyx | Level 15

    Nice! Keep it coming, buddy!

    chrisz
    Calcite | Level 5

    Here's another option

    data have;

    input group a $ b c;

    datalines;

    1 A 23 20

    1 B 34 32

    1 C 29 12

    1 D 24 10

    2 E 45 65

    2 F 87 25

    2 G 89 76

    ;

    run;

    proc sort data=have;

      by  descending group descending a ;

    run;

    data want;

      set have;

      by descending group descending a;

      retain icount;

      if first.group then icount=0;

      if icount<2 then do;

      output;

      icount++1;

      end;

      drop icount;

    run;

    proc sort data=want;

      by group a;

    run;

    bollibompa
    Quartz | Level 8

    Hi,

    Problem solved!

    Many thanks for all brilliant answers!

    /Thomas

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 8 replies
    • 5283 views
    • 5 likes
    • 8 in conversation