Hi SAS experts,
I would like to get the first observation by firm id and year id. May I seek your advice on a good way to do so ?
The first.firm only keeps the first observation by firm id.
For example, in the following data, I would like to keep the bold observation, which is the first observation by firm and year
| Firm | Year | Return |
| 1 | 2000 | 23 |
| 1 | 2001 | 34 |
| 1 | 2002 | 45 |
| 2 | 1999 | 56 |
| 2 | 2000 | 67 |
| 2 | 2001 | 78 |
| 2 | 2002 | 89 |
| 3 | 2004 | 90 |
| 3 | 2005 | 67 |
Thanks
proc sort data=have;
by firm year;
run;
data want;
set have;
by firm;
if first.firm;
run;
Thanks for your reply!
May I seek your advice on how to update the code in order to keep the following bold data ? that is, the first observation of a firm in a year
| Firm | Year | Month |
| 1 | 2000 | 1 |
| 1 | 2000 | 2 |
| 1 | 2000 | 3 |
| 1 | 2001 | 1 |
| 1 | 2001 | 2 |
| 1 | 2001 | 3 |
| 1 | 2002 | 1 |
| 1 | 2002 | 2 |
| 2 | 2000 | 1 |
| 2 | 2000 | 2 |
| 2 | 2001 | 1 |
| 2 | 2001 | 2 |
| 2 | 2002 | 1 |
| 2 | 2002 | 2 |
Exact same logic only use
data want;
set have;
by firm year;
if first.year;
run;
By group processing a data step adds automatic variables First. and Last. for each variable that appears on the BY statement. These are numeric 0/1 values and SAS will use the 1 as "true" and 0 for "false". So when the data is in the correct order you can test membership of the combinations of all the variables on the By statement if needed.
Just a very small expansion of my code:
proc sort data=have;
by firm year month;
run;
data want;
set have;
by firm year;
if first.year;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.