BookmarkSubscribeRSS Feed
AbuChowdhury
Fluorite | Level 6

Hi Experts,

In my dataset, there are a lot of time series observations for each firm. I would like to keep first 25 observations for each firm. How can I do that?

 

 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Try this

 

data test;
   do _N_ = 1 by 1 until (last.stock);
      set sashelp.stocks;
      by stock;
      if _N_ <= 25 then output;
   end;
run;
AbuChowdhury
Fluorite | Level 6

Hi,

The code keeps only one observation for each firm. I wanted to keep 25 observations for each firm.

PeterClemmensen
Tourmaline | Level 20

No. I assume that your data is sorted. Then the 25 first obs for each firm (stock in this case) are outputted

 

log:

 

1    data test;
2       do _N_ = 1 by 1 until (last.stock);
3          set sashelp.stocks;
4          by stock;
5          if _N_ <= 25 then output;
6       end;
7    run;

NOTE: There were 699 observations read from the data set SASHELP.STOCKS.
NOTE: The data set WORK.TEST has 75 observations and 8 variables.
NOTE: DATA statement used (Total process time):
      real time           0.13 seconds
      cpu time            0.04 seconds

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
  • 3 replies
  • 1055 views
  • 0 likes
  • 2 in conversation