cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
ALopez
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
txnelson
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

View solution in original post

5 REPLIES 5
txnelson
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
ALopez
Level III

Re: String to number conversion not working

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

Jeff_Perkinson
Community Manager 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";

On to your next line:

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
ALopez
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
ThuongLe
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));

 

Thuong Le