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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 475 views
  • 0 likes
  • 2 in conversation