BookmarkSubscribeRSS Feed
3 REPLIES 3
PaigeMiller
Diamond | Level 26

... 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?

--
Paige Miller
Tom
Super User Tom
Super User

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;
Patrick
Opal | Level 21

@Tom What about 11, 12, 13, 111, 112, ....?

 

@gleebglorb 

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;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1052 views
  • 2 likes
  • 4 in conversation