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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 3 replies
  • 780 views
  • 2 likes
  • 4 in conversation