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
;
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.
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.
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
;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: