BookmarkSubscribeRSS Feed
venkatard
Calcite | Level 5

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

9 REPLIES 9
RichardinOz
Quartz | Level 8

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

venkatard
Calcite | Level 5

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'

RichardinOz
Quartz | Level 8

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

venkatard
Calcite | Level 5

May be i was not clear.

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

Xianhua_zeng
Fluorite | Level 6

%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

Keith
Obsidian | Level 7

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;

PGStats
Opal | Level 21

Or simpler:

%let groups=10;

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

PG

PG
Keith
Obsidian | Level 7

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

naiksairam22
Calcite | Level 5
Dear Friends,

I have 40lac data in sas need separate data in two data set equally kindly suggest ho can I do it.


Thanks
Sai

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 9 replies
  • 19348 views
  • 2 likes
  • 6 in conversation