- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think this will do it:
retain group 0;
if mod(_n_ - 1,&size.) = 0 then group + 1;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Wrote a macro for this a while back:
https://gist.github.com/statgeek/49e54641ceaf58bc4fe5dc2062bc89cb
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content