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?
I would recommend starting with the documentation.
The section related to First/Last is under the section titled:
How the DATA Step Identifies BY Groups
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.
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
A big thanks to all the following people
LinusH, Reeza, Astounding & art297.
The explanantion certainly helped me understand the concept. Thanks a lot.
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.
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.