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