... but I am trying to consolidate it into a single proc
I don't know what this means.
If the format created by the code does what it claims to do, why can't you just assign this format to the desired variables? Why wouldn't that be sufficient?
Not sure how one step is any better than two steps.
Your code could be a lot simpler however.
data ordinal_format;
fmtname="ordinal_fmt";
type="N";
length label $10;
do start=1 to 300;
select(mod(start,10));
when (1) label=cats(start,'st');
when (2) label=cats(start,'nd');
when (3) label=cats(start,'rd');
otherwise label=cats(start,'th');
end;
output;
end;
run;
proc format cntlin=ordinal_format;
run;
@Tom What about 11, 12, 13, 111, 112, ....?
Below a solution approach using a function in a format. If you want to then you can store both user defined functions and formats in a permanent location and you would need to run this bit of the code only ad-hoc if you need to change something.
proc fcmp outlib=sasuser.funcs.nominal;
function nominal(in_num) $;
length out_str temp $10;
temp=left(put(in_num,10.));
if missing(temp) then out_str=' ';
else
if substrn(temp,lengthn(temp)-1) in ('11','12','13') then
do;
out_str=cats(temp,'th');
end;
else
do;
select(mod(in_num,10));
when (1) out_str=cats(temp,'st');
when (2) out_str=cats(temp,'nd');
when (3) out_str=cats(temp,'rd');
otherwise out_str=cats(temp,'th');
end;
end;
return(out_str);
endsub;
run;quit;
options cmplib=sasuser.funcs;
proc format;
value nominal(default=10)
other=[nominal()]
;
run;
data have;
do num_var=1 to 25, 99 to 125, 999 to 1025;
output;
end;
stop;
run;
proc print data=have;
format num_var nominal10.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.