DATA Step, Macro, Functions and more

first. and last. usage in sas

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 100
Accepted Solution

first. and last. usage in sas

Hi All: 

Could some one explain first.variable and last. variable concept for me ? 

eg: the data ids2 has been sorted using proc sort, 

 

data dupes;
set ids2;
by id;
if not (first.id and last.id) then output;
run;

 

if not (first.id and last.id) then output;  --> what doest this code do? 

 

is there a paper about first. and last. usage in SAS?

 


Accepted Solutions
Solution
‎05-08-2017 04:46 PM
Super User
Posts: 5,424

Re: first. and last. usage in sas

This means if first. and last. isn't the same observation then output. This will be true for all observations in a BY group with more than one observation.
There are tons of papers. Search support.sas.com and global forum proceedings as a start.
Data never sleeps

View solution in original post


All Replies
Super User
Posts: 19,768

Re: first. and last. usage in sas

I would recommend starting with the documentation. 

 

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n138da4gme3zb7n1nifp...

 

The section related to First/Last is under the section titled:

 

How the DATA Step Identifies BY Groups

Solution
‎05-08-2017 04:46 PM
Super User
Posts: 5,424

Re: first. and last. usage in sas

This means if first. and last. isn't the same observation then output. This will be true for all observations in a BY group with more than one observation.
There are tons of papers. Search support.sas.com and global forum proceedings as a start.
Data never sleeps
Super User
Posts: 5,496

Re: first. and last. usage in sas

The net effect:  any ID values that have just a single observation get deleted.  It's a topic that is well worth studying.  Here is one place to start:

 

http://www2.sas.com/proceedings/forum2007/222-2007.pdf

 

You may need to read a few background pages before it gets to the point of adding the BY statement in the DATA step.

PROC Star
Posts: 7,467

Re: first. and last. usage in sas

You said it was sorted, I'll presume it was sorted by id.

 

In that case, using ID as the by variable, first.id will be equal to 1 when, and only when, it is the first record for that ID.

 

Similarly, last.id will be equal to 1 when, and only when, it is the last record for that ID.

 

As such, think about the statement you asked about: if not (first.id and last.id) then output; 

 

It's opposite if  (first.id and last.id) then output;  would only output records where there was only one record for an ID. For any IDs that had multiple records, it would never be true for any of the records. As such, if not (first.id and last.id) then output; will output all records where there ARE multiple records for a given ID.

 

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 100

Re: first. and last. usage in sas

A big thanks to all the following people

LinusH, Reeza, Astounding & art297. 

 

           The explanantion certainly helped me understand the concept. Thanks a lot. 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 143 views
  • 0 likes
  • 5 in conversation