BookmarkSubscribeRSS Feed
hellind
Quartz | Level 8

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.

4 REPLIES 4
LinusH
Tourmaline | Level 20

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
djrisks
Barite | Level 11
Thank you. This helped me today! 🙂
data_null__
Jade | Level 19

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;
  
yaswanthj
Fluorite | Level 6

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,

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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