Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
Level III

## String to number conversion not working

Hi, I have a regular expression that extracts the Year from a month_year  i.e. "August_2020". However, I haven't been able to find the correct syntax to convert the  character Year "cYear" to number so I can do math with it. I will appreciate any pointers. Thanks!!

``````Names Default To Here( 1 );

SelMonth = {"October_2020"};

cYear = (Regex Match (char(SelMonth), "\d{4}") );

//convert character year to number year

nYear = Num ("cYear");  //Scripting Guide p137

//Subtract 1 and get previous year

pYear = (nYear - 1);

//convert previous year to character

cpYear = Char ( pYear);``````
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User

## Re: String to number conversion not working

The variable "SelMonth" is a JMP List( i.e. {} ), therefore the value of variable "cYear" is a list

``{"2020"}``

Assuming there is a reason that "SelMonth" needs to be a List, the simple solution is to reference "cYear" with a subscript to get the value

``nYear = Num (cYear[1]);``
Jim
5 REPLIES 5
Highlighted
Super User

## Re: String to number conversion not working

The variable "SelMonth" is a JMP List( i.e. {} ), therefore the value of variable "cYear" is a list

``{"2020"}``

Assuming there is a reason that "SelMonth" needs to be a List, the simple solution is to reference "cYear" with a subscript to get the value

``nYear = Num (cYear[1]);``
Jim
Highlighted
Level III

## Re: String to number conversion not working

Ah! Yes, the first value of the list! Thanks TX,

Highlighted
Community Manager

## Re: String to number conversion not working

You've got a number of small, but understandable errors.

``````SelMonth = {"October_2020"};
``````

The curly braces {} around your character string have created a one element list. I don't know if that was on purpose or not. If your actual problem (as opposed to this example that you've provided) involves a multi-element list (e.g. {"October_2020", "November_2020"}) then some of what is to follow would need to be adjusted. Let us know.

Remove the curly braces to get a character string value.

``SelMonth = "October_2020";``

The next issue is with your next line:

``cYear = (Regex Match (char(SelMonth), "\d{4}") );``

Regex Match() returns a list which is going to cause you problems in your next step. It's also overly complicated. Instead, the Word() function is your friend: If you learn only one Formula Editor function, Word() is the one.

``````cYear=Word(2, SelMonth, "_");

show(cYear);``````
```/*:

cYear = "2020";
```

``nYear = Num ("cYear");``

Here you've put your variable name in quotes which makes it a character literal instead of a variable name. Instead, you should have:

``nYear = Num (cYear);``

However, getting back to the Regex Match() function returning a list, that will cause a problem here. The Num() function won't take a list as an argument.

If you insist on using Regex Match(), you'll need to specify the element of the list you want to work on using square braces [] as an index.

``nYear = Num (cYear[1]);``

Note: If you take my suggestion to use the Word() function this is unnecessary.

Putting it all together here's what you need:

``````Names Default To Here( 1 );

SelMonth = "October_2020";

cYear = (Regex Match (char(SelMonth), "\d{4}") );

cYear=Word(2, SelMonth, "_");

//convert character year to number year

nYear = Num (cYear);  //Scripting Guide p137

//Subtract 1 and get previous year

pYear = (nYear - 1);

//convert previous year to character

cpYear = Char ( pYear);``````

-Jeff
Highlighted
Level III

## Re: String to number conversion not working

Thanks Jeff, I would definitely read more about the Word function. Thank you very much for taking the time to help me.
Best regards
Highlighted
Level IV

## Re: String to number conversion not working

Or you can get last 4 letters of your variable and convert it to number which is easier than Regex function

``````a = "October_2020";
nYear = Num(Right(a, 4));``````

Article Labels