Help using Base SAS procedures

Easy way to reverse a proc Format?

Frequent Contributor
Posts: 93

Easy way to reverse a proc Format?


Suppose I have a format:

proc format;
value v
  1='B' 2='A' 3='C';

Is there a way to automatically or easier way to reverse it?a

proc format;
invalue inv
  'B'=1 'A'=2 'C'=3;

Is there any simple way to convert (or reverse?) the informat to a format without having to hard code the second part? Something like put(input(.....))?

Why I need this?

I have some status in text and need to sort in priority by converting to numeric. After sorting by priority, I want to update the columns back to their text field.

Super User
Posts: 5,888

Re: Easy way to reverse a proc Format?

One way is to export the format to a CNTLOUT table. Then rename START to LABEL and LABEL to START (and drop END) columns. You may also need to change the value of FMTNAME and TYPE.

And then just use the resulting table as a CNTLIN table to PROC FORMAT.

If you have overlapping target values in your current format, you need to figure out how to handle these in the "reverse" format.

Data never sleeps
Super Contributor
Posts: 261

Re: Easy way to reverse a proc Format?

Thank you. This helped me today! :-)
Respected Advisor
Posts: 3,852

Re: Easy way to reverse a proc Format?

It sounds like you want the NOTSORTED option on the value statement.  That in conjunction with ORDER=DATA you may be able to achieve what you want with any extra work depending on you need..

For me I like to have a table to lookup the order when I need it.  I do this by creating a data set of the formatted values along with an INDEX.  Then I can use this data set to re-code the values when I get ready to have the order I want.  In this example I use PROC SUMMARY to create the index variable using the LEVELS option.  Then I, index the data set on the "variable" and the index variable.  With this data I can lookup the index(sort order) or lookup the value based on the sort order, using a SET statement with the KEY=option.  Or use the data to create an informat.

proc format;
value v(notsorted) 1='B' 2='A' 3='C';
data v;
   do _n_ = 1 to 30;
      v = rantbl(
proc print;
proc summary nway completetypes;
class v / preloadfmt order=data exclusive;
format v v.;
output out=vi(rename=(_level_=indx) index=(v indx)) / levels;
proc contents;
proc print;

data ordered;
   set v;
   set vi(drop=_freq_) key=v/unique;
proc print;

data cntl;
   set vi;
   retain fmtname 'iv' type 'j';
length start $1.;
   start = strip(vvalue(v));
label = indx;
proc format cntlin=cntl;
   select @$iv;
Posts: 70

Re: Easy way to reverse a proc Format?

Hi Hillind,

If you don`t want complex coding. and write that in simple. you should have to create new variable and apply format to that new variable and sort it. later in the final dataset just drop the new variable like below code..

data test;

input name $ value $;


ccc C

ddd D

aaa A

bbb B



proc print;


proc format;

value $inv 'A' = 1

      'B' = 2

   'C' = 3

   'D' = 4



data test;

set test;

format new $inv.;

new = value;


proc sort data = test;

by name;


proc print;


proc sql;

create table want as

select * from test(drop = new);

select * from want;


this code may help for your requirement..


Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation