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:

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3988 views
  • 1 like
  • 2 in conversation