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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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