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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1194 views
  • 3 likes
  • 3 in conversation