BookmarkSubscribeRSS Feed
Nieves
Quartz | Level 8

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

FirmYearReturn
1200023
1200134
1200245
2199956
2200067
2200178
2200289
3200490
3200567

 

Thanks

 

 

4 REPLIES 4
Nieves
Quartz | Level 8

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

 

FirmYearMonth
120001
120002
120003
120011
120012
120013
120021
120022
220001
220002
220011
220012
220021
220022
ballardw
Super User

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1297 views
  • 3 likes
  • 3 in conversation