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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
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

5 REPLIES 5
Reeza
Super User

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

LinusH
Tourmaline | Level 20
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
Astounding
PROC Star

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.

art297
Opal | Level 21

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

 

buddha_d
Pyrite | Level 9

A big thanks to all the following people

LinusH, Reeza, Astounding & art297. 

 

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

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!

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.

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
  • 5 replies
  • 1070 views
  • 0 likes
  • 5 in conversation