<?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: How to parse character string with arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722241#M223919</link>
    <description>&lt;P&gt;Yes, I think I've done that. I started with a scan function and a single quote as the delimiter, because I want names1 to include all words and spaces between the first pair of single quotes. Then I switch to scan functions using space as the delimiter because the remaining 4 have no spaces. In the 2nd loop, what is the best way to begin scanning at the 6th clause, and then the 11th clause on the 3rd loop, etc? The scan function does not have an option to start in the middle of a column.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Feb 2021 19:53:34 GMT</pubDate>
    <dc:creator>benjamin_2018</dc:creator>
    <dc:date>2021-02-26T19:53:34Z</dc:date>
    <item>
      <title>How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722237#M223917</link>
      <description>&lt;P&gt;Hello. I seek advice on how to parse a character string with multiple "observations" into 5 separate columns. For example, I am trying to parse the following string:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{'Orva Stores': [36.99, 0.0, 'A2NEM58BFPMEIL', 1], 'J.WALKER LLC': [36.99, 0.0, 'A1B3KT3F9BMSN1', 1], 'Justin Smiles': [36.99, 0.0, 'A3SP9XX1M7ZN73', 1], 'Closeout Pro': [36.99, 0.0, 'A3IRLODW57QOBV', 1], 'Flow Dealz': [44.95, 0.0, 'A2GP4414DQTJ29', 1]}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this column there are 5 "observations" that I want to parse into 5 separate columns. The next observation (not shown) has 2 "observations" that I want to parse into 5 columns, and the next has 1, and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been able to parse the first clause, but I need help on how to continue to parse the 2nd clause, then the 3rd, etc. Also, if later observations have fewer than 5 as stated above, I need the loop to stop. In this case &amp;amp;MaxSeller=5 and the column I am parsing is called OfferList.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set work.have;
	Sellers=compress(OfferList,"'.","kads"); *removes punctuation, keeps single quotes, alpha, digits, and decimals;

	/* create the 5 columns */&lt;BR /&gt;	array names[&amp;amp;MaxSeller] $ 64;
	array prices[&amp;amp;MaxSeller];
	array ship[&amp;amp;MaxSeller];
	array sellerid(&amp;amp;MaxSeller) $ 14;
	array prime(&amp;amp;MaxSeller);

	do i = 1 to &amp;amp;MaxSeller;
		names[i]=scan(Sellers,i,"'",);
		_NameLength=length(names[i]);
		_Restofstring=strip(substr(Sellers,_NameLength+3));
		prices[i]=scan(_Restofstring,i," ");
		ship[i]=scan(_Restofstring,i+1," ");
		_ID='/.\w{14}./o';
		_ID_loc=prxmatch(_ID,_Restofstring);
		sellerid[i]=strip(substr(_Restofstring,_ID_Loc+1,14));
		_P='/ [01] /o';
		_P_loc=prxmatch(_P,_Restofstring);
		prime[i]=substr(_Restofstring,_P_loc+1,1);
	end;

	drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you! I use SAS Enterprise Guide 8.1.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 19:34:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722237#M223917</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-26T19:34:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722240#M223918</link>
      <description>&lt;P&gt;Please post the &lt;STRONG&gt;rules and the delimiters&lt;/STRONG&gt; between the observations.&lt;/P&gt;
&lt;P&gt;You can use &lt;STRONG&gt;translate&lt;/STRONG&gt;() function to replace the different delimiters into one specific delimiter then&lt;/P&gt;
&lt;P&gt;use &lt;STRONG&gt;scan&lt;/STRONG&gt;() function in a loop to assign each value to appropriate variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 19:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722240#M223918</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-26T19:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722241#M223919</link>
      <description>&lt;P&gt;Yes, I think I've done that. I started with a scan function and a single quote as the delimiter, because I want names1 to include all words and spaces between the first pair of single quotes. Then I switch to scan functions using space as the delimiter because the remaining 4 have no spaces. In the 2nd loop, what is the best way to begin scanning at the 6th clause, and then the 11th clause on the 3rd loop, etc? The scan function does not have an option to start in the middle of a column.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 19:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722241#M223919</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-26T19:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722247#M223921</link>
      <description>&lt;P&gt;Is it originally JSON/XML file? If so, you could try the libname approach to handling those files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's coming from a DB it's more complicated....&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 20:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722247#M223921</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-26T20:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722248#M223922</link>
      <description>&lt;P&gt;I think it does come from one of those file types, but I am reading from a CSV file. I haven't used LIBNAME with JSON or XML. I can try that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 20:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722248#M223922</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-26T20:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722271#M223931</link>
      <description>&lt;P&gt;I'm not sure you understood what I meant. I can't guess the expected output.&lt;/P&gt;
&lt;P&gt;Maybe next code will give a hint to what I meant:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let str = {'Orva Stores': [36.99, 0.0, 'A2NEM58BFPMEIL', 1], 'J.WALKER LLC': [36.99, 0.0, 'A1B3KT3F9BMSN1', 1], 'Justin Smiles': [36.99, 0.0, 'A3SP9XX1M7ZN73', 1], 'Closeout Pro': [36.99, 0.0, 'A3IRLODW57QOBV', 1], 'Flow Dealz': [44.95, 0.0, 'A2GP4414DQTJ29', 1]};
data a;
  str = "&amp;amp;str";
  str1 = compress(str,'{[}]');
  length strx $40;
  do i=1 to 10;
     strx = scan(str1,i,':'); &lt;BR /&gt;     if strx ne ' ' then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What are the expected variables and observations from this given string?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 22:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722271#M223931</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-26T22:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722275#M223933</link>
      <description>&lt;P&gt;I would start like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
txt = "{'Orva Stores': [36.99, 0.0, 'A2NEM58BFPMEIL', 1], 'J.WALKER LLC': [36.99, 0.0, 'A1B3KT3F9BMSN1', 1], 'Justin Smiles': [36.99, 0.0, 'A3SP9XX1M7ZN73', 1], 'Closeout Pro': [36.99, 0.0, 'A3IRLODW57QOBV', 1], 'Flow Dealz': [44.95, 0.0, 'A2GP4414DQTJ29', 1]}";
id = prxParse("/'([^']+)': \[([^\]]+)\]/");
length name $24 values $64;
start = 1;
stop = length(txt);
call prxnext(id, start, stop, txt, pos, len);
do while (pos &amp;gt; 0);
    name = prxPosn(id, 1, txt);
    values = prxPosn(id, 2, txt);
    /* Parse values here */
    output;
    call prxnext(id, start, stop, txt, pos, len);
    end;
keep name values;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1614380062931.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55251iDD590EA2B42F985D/image-size/large?v=v2&amp;amp;px=999" role="button" title="PGStats_0-1614380062931.png" alt="PGStats_0-1614380062931.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;.... and then use SCAN or the same technique again on variable &lt;EM&gt;values&lt;/EM&gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 22:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722275#M223933</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-02-26T22:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722276#M223934</link>
      <description>I see. Yes sorry I realize I did not post the expected output. The variables I want to create are the variables created in the array statements.&lt;BR /&gt;names[i] should be the string between the first pair of quotes.&lt;BR /&gt;prices[i] should be the first number in the brackets&lt;BR /&gt;ship[i] should be the 2nd number in the brackets.&lt;BR /&gt;sellerid[i] should be the quoted string within the brackets.&lt;BR /&gt;prime[i] should be the last number in the brackets.&lt;BR /&gt;After that I want to go back through the loop to begin again with names[i], etc.&lt;BR /&gt;&lt;BR /&gt;I will look into your solution next week. Thank you.</description>
      <pubDate>Fri, 26 Feb 2021 22:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722276#M223934</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-26T22:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722277#M223935</link>
      <description>THANK YOU! I will try this solution next week. I’m not familiar with prxnext but I’ll give it a shot!!</description>
      <pubDate>Fri, 26 Feb 2021 23:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722277#M223935</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-26T23:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722301#M223943</link>
      <description>&lt;P&gt;Next is a tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let str = {'Orva Stores': [36.99, 0.0, 'A2NEM58BFPMEIL', 1], 'J.WALKER LLC': [36.99, 0.0, 'A1B3KT3F9BMSN1', 1], 'Justin Smiles': [36.99, 0.0, 'A3SP9XX1M7ZN73', 1], 'Closeout Pro': [36.99, 0.0, 'A3IRLODW57QOBV', 1], 'Flow Dealz': [44.95, 0.0, 'A2GP4414DQTJ29', 1]};
data a;
  str = "&amp;amp;str";
  str1 = compress(str,"'{[}");
  str1 = translate(str1,',',':');
  length strx $60 name $15 ;
  do i=1 to 10;
     strx = scan(str1,i,']'); 
    if strx ne ' ' then do; 
     name = scan(strx,1,',');
     price = scan(strx,2,',');
     ship  = scan(strx,3,',');
     sellerid = scan(strx,4,',');
     prime = scan(strx,5,',');
     output;
  end; end;
  keep name price ship sellerid prime strx;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Feb 2021 02:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722301#M223943</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-27T02:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse character string with arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722305#M223944</link>
      <description>&lt;P&gt;Thank you very much!!!&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2021 03:15:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-parse-character-string-with-arrays/m-p/722305#M223944</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2021-02-27T03:15:34Z</dc:date>
    </item>
  </channel>
</rss>

