<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: String to number conversion not working in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319235#M56972</link>
    <description>&lt;P&gt;You've got a number of small, but understandable errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;SelMonth = {"October_2020"};
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The curly braces {} around your character string have created a one element &lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/lists.shtml#" target="_self"&gt;list&lt;/A&gt;. 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remove the curly braces to get a character string value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;SelMonth = "October_2020";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The next issue is with your next line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;cYear = (Regex Match (char(SelMonth), "\d{4}") );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/regex-match.shtml" target="_self"&gt;Regex Match()&lt;/A&gt; returns a list which is going to cause you problems in your next step. It's also overly complicated. Instead, the &lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/character-functions-2.shtml#ww8107571" target="_self"&gt;Word()&lt;/A&gt; function is your friend: &lt;LI-MESSAGE title="If you learn only one Formula Editor function, Word() is the one" uid="30381" url="https://community.jmp.com/t5/JMP-Blog/If-you-learn-only-one-Formula-Editor-function-Word-is-the-one/m-p/30381#U30381" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;cYear=Word(2, SelMonth, "_");

show(cYear);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;/*:

cYear = "2020";
&lt;/PRE&gt;
&lt;P&gt;On to your next line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num ("cYear");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here you've put your variable name in quotes which makes it a character literal instead of a variable name. Instead, you should have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num (cYear);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, getting back to the Regex Match() function returning a list, that will cause a problem here.&amp;nbsp;The Num() function won't take a list as an argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num (cYear[1]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: If you take my suggestion to use the Word() function this is unnecessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting it all together here's what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Oct 2020 15:14:03 GMT</pubDate>
    <dc:creator>Jeff_Perkinson</dc:creator>
    <dc:date>2020-10-08T15:14:03Z</dc:date>
    <item>
      <title>String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319133#M56965</link>
      <description>&lt;P&gt;Hi, I have a regular expression that extracts the Year from a month_year&amp;nbsp; i.e. "August_2020". However, I haven't been able to find the correct syntax to convert the&amp;nbsp; character Year "cYear" to number so I can do math with it. I will appreciate any pointers. Thanks!!&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 23:40:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319133#M56965</guid>
      <dc:creator>ALopez</dc:creator>
      <dc:date>2023-06-09T23:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319192#M56968</link>
      <description>&lt;P&gt;The variable "SelMonth" is a JMP List( i.e. {} ), therefore the value of variable "cYear" is a list&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;{"2020"}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num (cYear[1]);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2020 14:35:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319192#M56968</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2020-10-08T14:35:52Z</dc:date>
    </item>
    <item>
      <title>Re: String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319193#M56969</link>
      <description>&lt;P&gt;Ah! Yes, the first value of the list! Thanks TX,&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 14:40:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319193#M56969</guid>
      <dc:creator>ALopez</dc:creator>
      <dc:date>2020-10-08T14:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319235#M56972</link>
      <description>&lt;P&gt;You've got a number of small, but understandable errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;SelMonth = {"October_2020"};
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The curly braces {} around your character string have created a one element &lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/lists.shtml#" target="_self"&gt;list&lt;/A&gt;. 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remove the curly braces to get a character string value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;SelMonth = "October_2020";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The next issue is with your next line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;cYear = (Regex Match (char(SelMonth), "\d{4}") );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/regex-match.shtml" target="_self"&gt;Regex Match()&lt;/A&gt; returns a list which is going to cause you problems in your next step. It's also overly complicated. Instead, the &lt;A href="https://www.jmp.com/support/help/en/15.2/#page/jmp/character-functions-2.shtml#ww8107571" target="_self"&gt;Word()&lt;/A&gt; function is your friend: &lt;LI-MESSAGE title="If you learn only one Formula Editor function, Word() is the one" uid="30381" url="https://community.jmp.com/t5/JMP-Blog/If-you-learn-only-one-Formula-Editor-function-Word-is-the-one/m-p/30381#U30381" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;cYear=Word(2, SelMonth, "_");

show(cYear);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;/*:

cYear = "2020";
&lt;/PRE&gt;
&lt;P&gt;On to your next line:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num ("cYear");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here you've put your variable name in quotes which makes it a character literal instead of a variable name. Instead, you should have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num (cYear);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, getting back to the Regex Match() function returning a list, that will cause a problem here.&amp;nbsp;The Num() function won't take a list as an argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;nYear = Num (cYear[1]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: If you take my suggestion to use the Word() function this is unnecessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting it all together here's what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 15:14:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319235#M56972</guid>
      <dc:creator>Jeff_Perkinson</dc:creator>
      <dc:date>2020-10-08T15:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319243#M56974</link>
      <description>Thanks Jeff, I would definitely read more about the Word function. Thank you very much for taking the time to help me.&lt;BR /&gt;Best regards</description>
      <pubDate>Thu, 08 Oct 2020 15:35:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319243#M56974</guid>
      <dc:creator>ALopez</dc:creator>
      <dc:date>2020-10-08T15:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: String to number conversion not working</title>
      <link>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319542#M57000</link>
      <description>&lt;P&gt;Or you can get last 4 letters of your variable and convert it to number which is easier than Regex function&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a = "October_2020";
nYear = Num(Right(a, 4));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2020 06:37:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/String-to-number-conversion-not-working/m-p/319542#M57000</guid>
      <dc:creator>ThuongLe</dc:creator>
      <dc:date>2020-10-09T06:37:26Z</dc:date>
    </item>
  </channel>
</rss>

