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

As you can see below, I am re-writing a format into a bunch of conditional statements. This is for binning a variable. Who can write a quick macro to convert the format statements below into the conditional ones above? Any suggestions are appreciated.

if 0 <= ost <= 5 then ost_5 = 1;
else if 6 <= ost <= 10 then ost_5 = 2;
else if 11 <= ost <= 15 then ost_5 = 3;
else if 16 <= ost <= 20 then ost_5 = 4;
else if 21 <= ost <= 25 then ost_5 = 5;
26 - 30 = 6
31 - 35 = 7
36 - 40 = 8
41 - 45 = 9
46 - 50 = 10
;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I would imagine that the answer depends on how you are doing the binning and what you will do with the data after it is binned.

 

In this case, the bin width is 5, so you can simply use that to create the new bin values

 

data want;
    set have; 
    if ost=0 then bin=1;
    else bin=floor((ost-1)/5)+1;
run;

So no macros needed here. And unlikely in general you will need a macro. But, please do tell us what the next steps are.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I would imagine that the answer depends on how you are doing the binning and what you will do with the data after it is binned.

 

In this case, the bin width is 5, so you can simply use that to create the new bin values

 

data want;
    set have; 
    if ost=0 then bin=1;
    else bin=floor((ost-1)/5)+1;
run;

So no macros needed here. And unlikely in general you will need a macro. But, please do tell us what the next steps are.

--
Paige Miller
Reeza
Super User

You can create formats from a data set using PROC FORMAT and CTNLIN.

https://support.sas.com/resources/papers/proceedings/proceedings/forum2007/068-2007.pdf

 

Depending on the format, exactly how that data set is created will differ. 

Parsing code will likely take more time.

 


@pink_poodle wrote:

As you can see below, I am re-writing a format into a bunch of conditional statements. This is for binning a variable. Who can write a quick macro to convert the format statements below into the conditional ones above? Any suggestions are appreciated.

if 0 <= ost <= 5 then ost_5 = 1;
else if 6 <= ost <= 10 then ost_5 = 2;
else if 11 <= ost <= 15 then ost_5 = 3;
else if 16 <= ost <= 20 then ost_5 = 4;
else if 21 <= ost <= 25 then ost_5 = 5;
26 - 30 = 6
31 - 35 = 7
36 - 40 = 8
41 - 45 = 9
46 - 50 = 10
;


 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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