<?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: Pulling number from complex string and creating new numeric field containing stripped out number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366949#M87328</link>
    <description>&lt;P&gt;I think you are making this overly complex by searching for the wrong string.&amp;nbsp; The payout amount seems to follow the first instance of "amount" : so search for that instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would create a character variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;payout = scan( substr(dataset, index(dataset, '"amount" :' ) + 10), 1);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Jun 2017 13:36:30 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-06-14T13:36:30Z</dc:date>
    <item>
      <title>Pulling number from complex string and creating new numeric field containing stripped out number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366901#M87313</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with 100000 rows and each row contains an extremely long string with lots of data. I'm want to create a new field with the "total payout value". This is the first number after the string "&lt;SPAN&gt;total_payout_value". The problem is the length of the string between the "total_payout_value" and the number can vary and also the number length can vary.&amp;nbsp;Here is a sample from my dataset:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"total_payout_value" : { "asset" : "SBD", "amount" : 37.13 }, "pending_payout_value" : { "asset" :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"total_payout_value" : {"amount" : 37.13, "asset" : "SBD"}, "pending_payout_value" : { "asset" :&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"total_payout_value" : {"amount" : 0, "asset" : "SBD"}, "pending_payout_value" : { "asset" :&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The actual sting is 1000s of characters long but each string will contain the string "&lt;/SPAN&gt;&lt;SPAN&gt;total_payout_value" only once and the first number after this string is what I'm trying to extract. You will also see that sometimes the order of words between "total_payout_value" and the number can change so unfortuntately this length is not fixed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I tried this code but I don't know how to allow for the varying lengths. Really, what I need is is something that find the first number after the string "total_payout_value"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data test1(keep = start end payout );&lt;BR /&gt;set data;&lt;BR /&gt;format payout $8.;&lt;BR /&gt;start = find(dataset,' "total_payout_value" : { "asset" : "SBD", "amount" : ')+54;&lt;BR /&gt;end = find(dataset,', "asset" : "SBD" }, "author"');&lt;BR /&gt;payout = substr(dataset,start,end-start));&lt;BR /&gt;run; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 10:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366901#M87313</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-06-14T10:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling number from complex string and creating new numeric field containing stripped out number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366904#M87314</link>
      <description>&lt;P&gt;What you have there is a JSon file, its a particular type of file structure. &amp;nbsp;Rather than trying to parse it yourself, use one of the methods given in your previous post on the subject:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/ODS-and-Base-Reporting/importing-JSON-data/m-p/346679/highlight/true#M18265" target="_blank"&gt;https://communities.sas.com/t5/ODS-and-Base-Reporting/importing-JSON-data/m-p/346679/highlight/true#M18265&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or one of the many other posts on the subject:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Confusion-errors-in-libname-Json/m-p/319716/highlight/true#M70300" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Confusion-errors-in-libname-Json/m-p/319716/highlight/true#M70300&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Need-to-convert-a-JSON-file-to-SAS-dataset/m-p/306967/highlight/true#M65701" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Need-to-convert-a-JSON-file-to-SAS-dataset/m-p/306967/highlight/true#M65701&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are also several free tools out there, for example:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://csvkit.readthedocs.io/en/1.0.2/" target="_blank"&gt;http://csvkit.readthedocs.io/en/1.0.2/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 11:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366904#M87314</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-14T11:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling number from complex string and creating new numeric field containing stripped out number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366916#M87321</link>
      <description>&lt;P&gt;How to retrieve data using name-value pairs, manually:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let id1=total_payout_value;
%let id2=amount;

data test;
infile cards truncover;
input o_string $100.;
string = o_string;
i = index(string,"&amp;amp;id1");
string = substr(string,i);
string = scan(string,2,'{');
string = scan(string,1,'}');
i = index(string,"&amp;amp;id2");
string = substr(string,i+length("&amp;amp;id2"));
string = scan(string,2,':');
string = scan(string,1,',');
&amp;amp;id2 = input(string,best.);
drop i string;
cards;
"total_payout_value" : { "asset" : "SBD", "amount" : 37.13 }, "pending_payout_value" : { "asset" : 
"total_payout_value" : {"amount" : 37.13, "asset" : "SBD"}, "pending_payout_value" : { "asset" : 
"total_payout_value" : {"amount" : 0, "asset" : "SBD"}, "pending_payout_value" : { "asset" : 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 11:56:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366916#M87321</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-14T11:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling number from complex string and creating new numeric field containing stripped out number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366930#M87322</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what about regex?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile cards truncover;
input string $100.;
cards;
"total_payout_value" : { "asset" : "SBD", "amount" : 37.13 }, "pending_payout_value" : { "asset" : 
"total_payout_value" : {"amount" : 37.13, "asset" : "SBD"}, "pending_payout_value" : { "asset" : 
"total_payout_value" : {"amount" : 0, "asset" : "SBD"}, "pending_payout_value" : { "asset" : 
;
run;

data test1;
   length var $200;
   set test;
   var=prxchange('s/.*total_payout_value\D*(\d*\.?\d*).*/$1/',-1,string);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 12:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366930#M87322</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-06-14T12:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling number from complex string and creating new numeric field containing stripped out number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366949#M87328</link>
      <description>&lt;P&gt;I think you are making this overly complex by searching for the wrong string.&amp;nbsp; The payout amount seems to follow the first instance of "amount" : so search for that instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would create a character variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;payout = scan( substr(dataset, index(dataset, '"amount" :' ) + 10), 1);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 13:36:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-number-from-complex-string-and-creating-new-numeric/m-p/366949#M87328</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-14T13:36:30Z</dc:date>
    </item>
  </channel>
</rss>

