BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ffurquim
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

5 REPLIES 5
Reeza
Super User

You can always make your own. Does your data have a specific range or is any number possible?

ffurquim
Calcite | Level 5

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.

Reeza
Super User

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;

ffurquim
Calcite | Level 5

Ha, I usually avoid prxmatch like the plague but here it is, good answer!

Reeza
Super User

So do I, ergo the "borrowed code" :smileygrin:

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 5 replies
  • 3028 views
  • 1 like
  • 2 in conversation