BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
tarheel13
Rhodochrosite | Level 12

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

7 REPLIES 7
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



tarheel13
Rhodochrosite | Level 12

hi, thanks so much. I love how short it is. I think this will work 😀

ballardw
Super User

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.);

tarheel13
Rhodochrosite | Level 12

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. 

ballardw
Super User

@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?

tarheel13
Rhodochrosite | Level 12

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. 

tarheel13_0-1674840129967.png

 

 

tarheel13
Rhodochrosite | Level 12
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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1268 views
  • 3 likes
  • 3 in conversation