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.
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.
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.
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,
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!
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.
Ready to level-up your skills? Choose your own adventure.