Delete First/Last observation from multiple EU's in a dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Delete First/Last observation from multiple EU's in a dataset

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


Accepted Solutions
Solution
‎01-29-2015 03:06 PM
Grand Advisor
Posts: 10,251

Re: Delete First/Last observation from multiple EU's in a dataset

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


All Replies
Contributor
Posts: 26

Re: Delete First/Last observation from multiple EU's in a dataset


Order your dataset and use .first and .last

if myvar.first OR myvar.last delete;

Occasional Contributor
Posts: 6

Re: Delete First/Last observation from multiple EU's in a dataset

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

Solution
‎01-29-2015 03:06 PM
Grand Advisor
Posts: 10,251

Re: Delete First/Last observation from multiple EU's in a dataset

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>;

Occasional Contributor
Posts: 6

Re: Delete First/Last observation from multiple EU's in a dataset

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



Grand Advisor
Posts: 10,251

Re: Delete First/Last observation from multiple EU's in a dataset

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;

Contributor
Posts: 26

Re: Delete First/Last observation from multiple EU's in a dataset

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 2193 views
  • 3 likes
  • 3 in conversation