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
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>;
Order your dataset and use .first and .last
if myvar.first OR myvar.last delete;
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
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>;
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
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;
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.