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

Hi,

I am trying to figure out how I would remove the first and last observation from a data set with Multiple EU's with 30-40 observations from each EU in chronological order.   How would I remove the first time and last time?

Thanks

Dan

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The syntax is FIRST.variablename not Variablename.first. When you used the dot notation the compiler went looking for some special instruction/behavior named ID which doesn't exist.

Also, you might want consider if you want to delete if there is only one record. In that case First.id and Last.id are the same and it would be deleted.

You can provide an additional test for

If first.id and last.id then <whatever>;

View solution in original post

6 REPLIES 6
morgalr
Obsidian | Level 7


Order your dataset and use .first and .last

if myvar.first OR myvar.last delete;

dandvm
Calcite | Level 5

if ID.first then delete;

This is what I get

ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.

ERROR 557-185: Variable ID is not an object.

What does this mean?

Thanks

ballardw
Super User

The syntax is FIRST.variablename not Variablename.first. When you used the dot notation the compiler went looking for some special instruction/behavior named ID which doesn't exist.

Also, you might want consider if you want to delete if there is only one record. In that case First.id and Last.id are the same and it would be deleted.

You can provide an additional test for

If first.id and last.id then <whatever>;

dandvm
Calcite | Level 5

I initially thought that this was correct.  However, I used the follow code

if first.ID then delete;

if last.id then delete;


It didn't actually do anything.  No records were removed.  I have something still wrong here.


Thanks



ballardw
Super User

I think you may need to post a bit more of the code used.

Also, did you have a BY statement? Without that it will not work:

Data want;

     set have;

     by ID;

     if first.ID or last.ID then delete;

run;

morgalr
Obsidian | Level 7

ballardw,

yes, quite right: I always get the order of the first/last mixed up with the variable--too much object oriented programming--and indeed it does remove any that only have a singular observation.

here is the corrected code:

data myEdit;

set test;

by myID;

if (first.myID or last.myID) then delete;

run;

And here is the code with that extra bit, it only strips the first and last, but it will also leave the observation that is a single for the id:

data myJunk;

  set test;

  by myID;

  if (not (first.myID and last.myID)) and (first.myID or last.myID) then delete;

run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 6 replies
  • 11905 views
  • 5 likes
  • 3 in conversation