BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
davidnamh
Calcite | Level 5

Hi,

I have a following data step which breaks entire observation by 10%  (eg. entire observation is 1896, and break the data set by 10% (1806 *10% = 189.6 ~= 190 )  )

Is there any way to group using macro so I don't need to change the parameters manually when the number of observation changes ? Thank you

DATA want

  SET have;

  if _n_<= 190 then grp=1;

else if 190 <_n_<=380 then grp=2;

else if 380 <_n_<=570 then grp=3;

else if 570 <_n_<=760 then grp=4;

else if 760 <_n_<=950 then grp=5;

else if 950 <_n_<=1140 then grp =6;

else if 1140 <_n_<=1330 then grp=7;

else if 1330 <_n_<=1520 then grp=8;

else if 1520 <_n_<=1710 then grp=9;

  grp=10;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Why not use proc rank ?

data class;
 set sashelp.class;
 n+1;
 run;
 proc rank data=class out=want groups=5;
 var n;
 ranks group;
 run;

Xia Keshan

View solution in original post

4 REPLIES 4
Chrishi
Calcite | Level 5

Hi davidnamh,

Hope this code will be helpful.

data bug;

set have;

count+1;

run;

proc sql;

create table want as select *, ceil(count/max(count)*0.10)) as grp from bug;

quit;

user24feb
Barite | Level 11

Hi!

I Think Proc Rank would be really an option:

Proc Rank Data=In_File Out=Result Groups=10;

  Var <Variable>;

  Ranks <Group_Name>;

Run;

Peter_C
Rhodochrosite | Level 12

No macro No macro required!

The SET statement has an option NOBS= which names a variable into which the data step compiler will place that number of "obs in the dataset".

You'll need to rewrite your code just a little (it could be a lot shorter)

data want ;

set have nobs= nobs ;

*grp = int( _n_/nobs) +1;

  grp = int((_n_-1)/nobs)+1 ;

run ;

Ksharp
Super User

Why not use proc rank ?

data class;
 set sashelp.class;
 n+1;
 run;
 proc rank data=class out=want groups=5;
 var n;
 ranks group;
 run;

Xia Keshan

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 4 replies
  • 1352 views
  • 0 likes
  • 5 in conversation