BookmarkSubscribeRSS Feed
BhavukZutshi
Fluorite | Level 6

I am having a car sales data file. Wanted to berak the data set where it can give me groups on price ranges. 

Having price ranges between 1 to 50 . Want to create 4 groups of data 1-10, 10-20,20-30 and so on. 

 

How to do this via simple code. 

3 REPLIES 3
mohamed_zaki
Barite | Level 11

You can use PROC FORMAT

like

proc format;
value rangefmt 
1 -<10 = '01-10'
10 -< 20 = '10-20'
20 -< 30 = '20-30'
30 -< 40 = '30-40'
40 - 50 = '40-50'
;

data want;
set have;
code=put(price, rangefmt.);
run;

or you can use noraml IF/ELSE statments

like


data want;
set have;
if 1 <= price < 10 then code='01-10';
else if 10 <= price < 20 then code='10-20';
else if 20 <= price < 30 then code='20-30';
else if 30 <= price < 40 then code='30-40';
else if 40 <= price <= 50 then code='40-50';
run;
BhavukZutshi
Fluorite | Level 6

The above solution stands good but what actually I'm looking for is to realign the whole table on basis of price where if price is less than 10 then that entire set should group as one set , then another set on price range of 10-20 and so on. The above solution just creates another column named as code and assign each row a vlaue on identification of price. 

mohamed_zaki
Barite | Level 11

I am not sure what you want exactly. How your table looks is another issue and can be done by different ways.

 

For example you can use PROC REPORT

proc report data=have SPANROWS ;
column code price;
define code / order 'Groups';
define price/ display 'Price';
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1366 views
  • 0 likes
  • 2 in conversation