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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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