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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1005 views
  • 0 likes
  • 5 in conversation