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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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