BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JT99
Obsidian | Level 7
Hello! How do I split one table into multiple tables and limit the number of observations to 25 for each table? Then I need to export all tables in excel.
Ex. I have a table with 99 obs and I need to split it to 4 tables with the first 3 tables having 25 obs and the last having 24. The original table can have different number of obs.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%let size = 25;

data want;
set have;
retain group 1;
if mod(_n_,&size.) = 0 then group + 1;
run;

You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Splitting tables is rarely necessary. You can leave this as one big table, with an identifier variable to indicate which of the four parts, then perform analyses BY this identifier variable.

 

Example:

 

data cars;
    set sashelp.cars;
    if mod(_n_,25)=1 then identifier+1;
run;

proc means data=cars;
    by identifier;
run;

 

 

If, for example, you only want to work on the data when identifier=3 (and not the rest of the data), you can do this too

 

proc means data=cars;
    where identifier=3;
run;

 

--
Paige Miller
Kurt_Bremser
Super User
%let size = 25;

data want;
set have;
retain group 1;
if mod(_n_,&size.) = 0 then group + 1;
run;

You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.

mkeintz
PROC Star

@Kurt_Bremser wrote:
%let size = 25;

data want;
set have;
retain group 1;
if mod(_n_,&size.) = 0 then group + 1;
run;

You now have a group designator which can be used in multiple ways, e.g. to switch sheets when using ODS EXCEL and a reporting procedure.


I agree with your comment.  But the code needs a minor tweak.  The first group above would only have 24 observations.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PaigeMiller
Diamond | Level 26

@Kurt_Bremser wrote:

I think this will do it:

retain group 0;
if mod(_n_ - 1,&size.) = 0 then group + 1;

Or my code earlier will do it as well.

--
Paige Miller
JT99
Obsidian | Level 7
Thank you all so much! Your replies are a big help.
JT99
Obsidian | Level 7
Perfect! Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1296 views
  • 6 likes
  • 5 in conversation