Hi all,
this may be a very simple question but I have not found an answer yet. Is there a format that can put cardinal numbers into ordinals? Turning, e.g., 1 to 1st, 2 to 2nd, etc?
It's not that bad, this is borrowed from a 2006 post from David Cassell and
data ordinal_format;
length label $8.;
do number=1 to 300;
numstring = put(number, 16. -l);
if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
else ending = 'th';
ordstring =catt(numstring, ending);
start=number;
label=ordstring;
fmtname="ordinal_fmt";
type="N";
output;
end;
keep start label fmtname type;
run;
proc format cntlin=ordinal_format;
run;
data want;
do i =1 to 100;
j=i;
output;
end;
format j ordinal_fmt.;
run;
You can always make your own. Does your data have a specific range or is any number possible?
Thanks Reeza...I was hoping to avoid creating my own due to the large range and the fact that the range could grow over time, so a SAS delivered one was preferable.
Anyway, the range is currently 1 (1st) to 127 (127th), although most values of interest cap out at around 22.
It's not that bad, this is borrowed from a 2006 post from David Cassell and
data ordinal_format;
length label $8.;
do number=1 to 300;
numstring = put(number, 16. -l);
if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
else ending = 'th';
ordstring =catt(numstring, ending);
start=number;
label=ordstring;
fmtname="ordinal_fmt";
type="N";
output;
end;
keep start label fmtname type;
run;
proc format cntlin=ordinal_format;
run;
data want;
do i =1 to 100;
j=i;
output;
end;
format j ordinal_fmt.;
run;
Ha, I usually avoid prxmatch like the plague but here it is, good answer!
So do I, ergo the "borrowed code" :smileygrin:
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.