Does anyone have a good way to convert Roman numerals to numbers? I have to do I-IX so I would like them to be 1-9 but I was just doing it with a format but what if it was more than 9?
Build yourself an informat:
data input;
FMTNAME="myRoman";
type="i";
do _N_ = 1 to 9999;
start=put(_N_,roman32.);
label=put(_N_,best12.);
output;
end;
HLO="OU";
label=".";
output;
run;
proc format CNTLIN=input;
run;
data test;
do _N_ = 10000, 1 to 9999;
TEXTTXT=put(_N_,roman32.);
TESTNUM=input(TEXTTXT,myRoman.);
output;
end;
run;
Bart
Build yourself an informat:
data input;
FMTNAME="myRoman";
type="i";
do _N_ = 1 to 9999;
start=put(_N_,roman32.);
label=put(_N_,best12.);
output;
end;
HLO="OU";
label=".";
output;
run;
proc format CNTLIN=input;
run;
data test;
do _N_ = 10000, 1 to 9999;
TEXTTXT=put(_N_,roman32.);
TESTNUM=input(TEXTTXT,myRoman.);
output;
end;
run;
Bart
hi, thanks so much. I love how short it is. I think this will work 😀
What is the range of Roman numerals that you have to deal with?
I would make a custom informat for the conversion. The example below assumes that your Roman numerals are all upper case.
data forinformat; fmtname='Romantonumber'; type='I';/* this means informat is made*/ /* make the length of start the longest number of characters your roman numeral has */ length start $ 10 ; /* set the largets numeric value you expect example uses 500 */ do label=1 to 500; /* use the Roman format to turn numeric value into Roman numeral, use width of format, 10 in example to match length of start */ start = strip(put(label,Roman10.)); output; end; run; /* use the data set to create the informat*/ proc format cntlin=forinformat; run; /* use INPUT with values to create number*/ data example; input Roman $; number = input(roman,romantonumber.); datalines; I II V VI CCC ;
If your Roman values are not upper case then use Input(upcase(roman),romantonumber.);
They're actually at the end of inclusion/exclusion criteria. I see I-IX right now. It has 3 levels. For example, 4BI is 4 b i) and it could have ii) iii) etc. I wanted to build something flexible so it could work for a variety of different number of Roman numerals.
@tarheel13 wrote:
They're actually at the end of inclusion/exclusion criteria. I see I-IX right now. It has 3 levels. For example, 4BI is 4 b i) and it could have ii) iii) etc. I wanted to build something flexible so it could work for a variety of different number of Roman numerals.
So you are going to have to provide a lot more rules and examples about when an "i" is a Roman numeral and when it is any other letter i . When you say 4B I have to assume that you have other values like 4A 4C (100) 4D (500).
Extract you roman numeral then use one of the informats suggested and replace with the numeric value. TRANWRD might be possible. But not even going to guess without concrete examples.
Please reread your original post in this thread and look for where there is a piece that the roman numerals are part of a longer text variable.
Does anyone have a good way to convert Roman numerals to numbers? I have to do I-IX so I would like them to be 1-9 but I was just doing it with a format but what if it was more than 9?
I did not mention that it was part of a text variable. I was mostly looking for a flexible way to convert the Roman numerals rather than doing it one by one. I like to make something dynamic so it can handle all possible cases or as many as possible. I have written a macro that will work now using your informat. This is the way I am handling it.
data example;
format ietestcd_std $200.;
input ietestcd_std &;
datalines;
Exclusion 4bi
Exclusion 2oi
Exclusion 2oix
Exclusion 4c
;
run;
Here's some example data. I have to change these to EX04B1, EX02O1, EX02O9, EX04C. It doesn't seem likely that it would go past 9 but I don't know what's possible in a protocol. And depending which variable I use in the raw data, it can be all caps or like what I have presented. I guess for your solution, it will make more sense to use the version of the variable that's in all caps.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.