DATA Step, Macro, Functions and more

How to write this code clear with less sentences?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How to write this code clear with less sentences?

Hi, Do you know how to write this in a simpler way?

data distanceclass;
set combine;
retain Dist_class 0;
If 0<miles<=0.5 then dist_class=0.5;
If 0.5<miles<=1 then dist_class=1.0;
If 1.0<miles<=1.5 then dist_class=1.5;
If 1.5<miles<=2.0 then dist_class=2.0;
If 2.0<miles<=2.5 then dist_class=2.5;
If 2.5<miles<=3.0 then dist_class=3.0;
If 3.0<miles<=3.5 then dist_class=3.5;
If 3.5<miles<=4.0 then dist_class=4.0;
If 4.0<miles<=4.5 then dist_class=4.5;
If 4.5<miles<=5.0 then dist_class=5.0;
If 5.0<miles<=5.5 then dist_class=5.5;
If 5.5<miles<=6.0 then dist_class=6.0;
If 6.0<miles<=6.5 then dist_class=6.5;
If 6.5<miles<=7.0 then dist_class=7.0;
If 7.0<miles<=7.5 then dist_class=7.5;
If 7.5<miles<=8.0 then dist_class=8.0;
If 8.0<miles<=8.5 then dist_class=8.5;
If 8.5<miles<=9.0 then dist_class=9.0;
If 9.0<miles<=9.5 then dist_class=9.5;
If 9.5<miles<=10.0 then dist_class=10.0;
If 10.0<miles<=10.5 then dist_class=10.5;
If 10.5<miles<=11.0 then dist_class=11.0;
If 11.0<miles<=11.5 then dist_class=11.5;
If 11.5<miles<=12.0 then dist_class=12.0;
If 12.0<miles<=12.5 then dist_class=12.5;
If 12.5<miles<=13.0 then dist_class=13.0;
If 13.0<miles<=13.5 then dist_class=13.5;
If 13.5<miles<=14.0 then dist_class=14.0;
If 14.0<miles<=14.5 then dist_class=14.5;
If 14.5<miles<=15.0 then dist_class=15.0;
If 15.0<miles<=15.5 then dist_class=15.5;
If 15.5<miles<=16.0 then dist_class=16.0;
If 16.0<miles<=16.5 then dist_class=16.5;
If 16.5<miles<=17.0 then dist_class=17.0;
If 17.0<miles<=17.5 then dist_class=17.5;
If 17.5<miles<=18.0 then dist_class=18.0;
If 18.0<miles<=18.5 then dist_class=18.5;
If 18.5<miles<=19.0 then dist_class=19.0;
If 19.0<miles<=19.5 then dist_class=19.5;
If 19.5<miles then dist_class=20.0;
run;

Accepted Solutions
Solution
a month ago
Super User
Posts: 5,516

Re: How to write this code clear with less sentences?

[ Edited ]
Posted in reply to audreyliu201

For most categories, you can get away with:

 

dist_class = ceil(2 * miles) / 2;

 

For the higher values, you also need to add:

 

if dist_class > 20 then dist_class = 20;

View solution in original post


All Replies
Solution
a month ago
Super User
Posts: 5,516

Re: How to write this code clear with less sentences?

[ Edited ]
Posted in reply to audreyliu201

For most categories, you can get away with:

 

dist_class = ceil(2 * miles) / 2;

 

For the higher values, you also need to add:

 

if dist_class > 20 then dist_class = 20;

Occasional Contributor
Posts: 7

Re: How to write this code clear with less sentences?

Posted in reply to Astounding
Thank you!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 148 views
  • 3 likes
  • 2 in conversation