BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
fwu811
Fluorite | Level 6

Hi,

 

I would like to do some simple averaging of the existing dataset by certain grouping and bucketing, but I also need to ensure that the value of each bucket increases as buckets always increase. 

 

For example,

data have;
input Col1 Col2;
datalines;
1 2
2 2
3 1
4 1
5 3

6 3

;

Data I want are;

<=2 2

<=4 2

<=6 3

 

Note that for the bucket with Col1 values between 2 and 4, the actual average is 1, which is less than the value (2) of the bucket for Col1 less than 2, so the final value for the 2nd bucket should be replaced with 2. 

Many thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

One possibility is to use a format to create the buckets and then use PROC SUMMARY and finally a data step for correction:


proc format;
  value bucket 
    1-<3='2'
    3-<5='4'
    5-<7='6'
   ;
run;

proc summary nway data=have;
  class col1;
  var col2;
  format col1 bucket.;
  output out=avg(drop=_:) mean=;
run;

data want;
  set avg;
  retain _lastval;
  col2=max(col2,_lastval);
  _lastval=col2;
  col1=input(put(col1,bucket.),best32.);
  drop _:;
run;

I put in a line to replace the value of COL1 with the value displayed with the BUCKET. format, as that may make the data easier to work with afterwards. 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I don't understand, you say "for Col1 values between 2 and 4" but your desired output has <=4, these are not the same. How are the limits for <=2 and <=4 and <=6 chosen, or are they fixed?

--
Paige Miller
s_lassen
Meteorite | Level 14

One possibility is to use a format to create the buckets and then use PROC SUMMARY and finally a data step for correction:


proc format;
  value bucket 
    1-<3='2'
    3-<5='4'
    5-<7='6'
   ;
run;

proc summary nway data=have;
  class col1;
  var col2;
  format col1 bucket.;
  output out=avg(drop=_:) mean=;
run;

data want;
  set avg;
  retain _lastval;
  col2=max(col2,_lastval);
  _lastval=col2;
  col1=input(put(col1,bucket.),best32.);
  drop _:;
run;

I put in a line to replace the value of COL1 with the value displayed with the BUCKET. format, as that may make the data easier to work with afterwards. 

fwu811
Fluorite | Level 6

It works and thanks.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 751 views
  • 0 likes
  • 3 in conversation