How to split a data set equally

Reply
Contributor
Posts: 63

How to split a data set equally

I have a data set of 8600 records and i need to split the data set equally and create a flag for each group

Super Contributor
Posts: 644

Re: How to split a data set equally

Do you need 2 different tables?

Data one two ;

     set have ;

     if mod (_N_, 2) = 1 then

          do ;

               flag = 'ONE' ;

               output one ;

          end ;

     else

          do ;

               flag = 'TWO' ;

               output two ;

          end ;

Run ;

Richard

Contributor
Posts: 63

Re: How to split a data set equally

No i only want the output in a single dataset with flags which are dividing the data.

For example if i have 8600 record i will have 860 in one flag='One'

Super Contributor
Posts: 644

Re: How to split a data set equally

860 is not half of 8600.

You should be able to work out what to do from my code, mostly by leaving out lines of code.

Richard

Contributor
Posts: 63

Re: How to split a data set equally

May be i was not clear.

I want to split the data of 8600 as 860 for each dataset *10 datasets.

Occasional Contributor
Posts: 12

Re: How to split a data set equally

%macro splitdata;

  %do i=1 %to 8600 %by 860;

  data want_&i.;

  set have(firstobs=&i obs=%eval(&i+859));

  flag="&i-%eval(&i+859)";

  run;

  %end;

%mend splitdata;

%splitdata

Regular Contributor
Posts: 151

Re: How to split a data set equally

A simple method if you don't want a random split.  This also avoids hard coding the number of observations.

%let groups=10;

data want;

set have nobs=nobs;

flag=ceil(_n_/(nobs/&groups.));

run;

Respected Advisor
Posts: 4,644

Re: How to split a data set equally

Or simpler:

%let groups=10;

data want;
set have;
flag=mod(_n_-1, &groups.);
run;

PG

PG
Regular Contributor
Posts: 151

Re: How to split a data set equally

Indeed, very good.  You could always make it even simpler and take out the -1.....

Ask a Question
Discussion stats
  • 8 replies
  • 1429 views
  • 2 likes
  • 5 in conversation