Help using Base SAS procedures

Easy way to reverse a proc Format?

Reply
Frequent Contributor
Posts: 90

Easy way to reverse a proc Format?

Hi,


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,256

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: 252

Re: Easy way to reverse a proc Format?

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

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';
  
run;
data v;
   do _n_ = 1 to 30;
      v = rantbl(
1234,1/3,1/3);
      output;
     
end;
  
run;
proc print;
  
run;
proc summary nway completetypes;
  
class v / preloadfmt order=data exclusive;
  
format v v.;
  
output out=vi(rename=(_level_=indx) index=(v indx)) / levels;
  
run;
proc contents;
proc print;
  
run;

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

data cntl;
   set vi;
   retain fmtname 'iv' type 'j';
  
length start $1.;
   start = strip(vvalue(v));
  
label = indx;
   run;
proc format cntlin=cntl;
   select @$iv;
   run;
  
Contributor
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 $;

cards;

ccc C

ddd D

aaa A

bbb B

;

run;

proc print;

run;

proc format;

value $inv 'A' = 1

      'B' = 2

   'C' = 3

   'D' = 4

   ;

   run;

data test;

set test;

format new $inv.;

new = value;

run;

proc sort data = test;

by name;

run;

proc print;

run;

proc sql;

create table want as

select * from test(drop = new);

select * from want;

quit;

this code may help for your requirement..

Thanks,

Ask a Question
Discussion stats
  • 4 replies
  • 970 views
  • 2 likes
  • 5 in conversation