<?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: Parsing JSON string stored in parameter column in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596428#M8012</link>
    <description>I don't understand your point.  Didn't you have to calculate the count for each observation in order to find the max over all the observations?&lt;BR /&gt;</description>
    <pubDate>Tue, 15 Oct 2019 04:13:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-15T04:13:32Z</dc:date>
    <item>
      <title>Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/594978#M8005</link>
      <description>&lt;P&gt;I am trying to parse below json string from parameters column in table 1:-&lt;/P&gt;&lt;P&gt;"{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""3.25*"",""type"":""Single Rate"",""needVerification"":""true""},&lt;BR /&gt;{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""2.87*"",""type"":""Joint Rate"",""needVerification"":""true""},{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":"""",""type"":""Joint Dev"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""40000"",""type"":""Benefit Maximize"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""11"",""type"":""Termination "",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""22"",""type"":""Eligibility Age"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""UNL"",""type"":""Term Of Insurance"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""7501"",""type"":""Benefit Amount"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""DEFAULT"",""notes"":null,""intergroupValue"":"""",""type"":""Delinquent Payment"",""needVerification"":""false""}"&lt;/P&gt;&lt;P&gt;Tried Coding as below:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; max(count(parameters,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'}'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;))+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;into&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; :maxelements &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; table1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; parsed_vars $ &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;150&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; new_var1-new_var&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;maxelements);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;maxelements;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;parsed_vars(i) =scan(parameters, i, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"}"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;i+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;In the result am getting alternate row of data in new_var1 then new_var3, new_var5 all&amp;nbsp;even ones are blank which is a problem. So I am getting only 5 of 9 rows.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 08:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/594978#M8005</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-09T08:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/595020#M8006</link>
      <description>&lt;P&gt;It seems that this JSON is not too complicated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;options noquotelenmax;
data have;
x='{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""3.25*"",""type"":""Single Rate"",""needVerification"":""true""},
{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""2.87*"",""type"":""Joint Rate"",""needVerification"":""true""}';
run;
data temp;
 set have;
 do i=1 to countw(x,',{}');
   temp=scan(x,i,',{}');output;
 end;
 drop x;
run;
data want;
 set temp;
 name=scan(temp,1,':');
 value=scan(temp,-1,':');
run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Oct 2019 12:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/595020#M8006</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-09T12:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596271#M8009</link>
      <description>&lt;P&gt;One additional query to this I will need to capture all these tokens as an array so I can search the ones I need. Also these lines are just part of more than 15K row of data.&lt;/P&gt;&lt;P&gt;Suppose I have id=101, parameter{&amp;lt;datalines shared earlier&amp;gt;} to parse now if I get count of words and&amp;nbsp;want to create array where size is dynamically captured based on the tokens&amp;nbsp;present in each&amp;nbsp;dataline. Each row will have different&amp;nbsp;count of sub rows in parameter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was able to get&amp;nbsp;count as a table column in proc sql :-&lt;/P&gt;&lt;P&gt;(count(parameters,'}')*12 as ratecnt&lt;/P&gt;&lt;P&gt;But problem is I want to define array defined on ratecnt for&amp;nbsp;each row of data. But only if I take max of above and capture using into macro variable it allows me to create dynamic array.&lt;/P&gt;&lt;P&gt;Please help me define proc sql or sas dynamic array defined based on ratcnt field captured in every row of data.&lt;/P&gt;&lt;P&gt;Below code doesn't run but on these lines.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;array parsed_vars $ 150 new_var1-new_var%eval(ratecnt);&lt;BR /&gt;i=1;&lt;BR /&gt;do i=1 to ratecnt;&lt;BR /&gt;parsed_vars(i) =scan(parameters, i, "}");&lt;BR /&gt;i+1;*/&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 13:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596271#M8009</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-14T13:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596276#M8010</link>
      <description>&lt;P&gt;An array of variables in a SAS data step always refers to the same list of variables on every observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So just need to know the MAX() of the number needed for each observation across the whole dataset.&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select max(ratecnt) into :array_length trimmed 
from have 
;
quit;

data want;
   set have;
   array parsed_vars [&amp;amp;array_length] $150 ;
   ....
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first step will create the macro variable ARRAY_LENGTH with a value like 45.&lt;/P&gt;
&lt;P&gt;Then in the data step the ARRAY statement will create 45 variables named PARSED_VARS1 to PARSED_VARS150, each of which is defined as character of length 150.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 14:10:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596276#M8010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-14T14:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596406#M8011</link>
      <description>This i thought of but then for each array i will need to calculate number of actual populated elements for do while loop because max works for overall table for eg comes 229 but some rows have as small as 98 name/ value elements.&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Oct 2019 02:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596406#M8011</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-15T02:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596428#M8012</link>
      <description>I don't understand your point.  Didn't you have to calculate the count for each observation in order to find the max over all the observations?&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Oct 2019 04:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596428#M8012</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-15T04:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596457#M8013</link>
      <description>Ratecnt defined in above post was giving count for each row. But array was to be defined for each row based on Number of observations each row has. How to use ratecnt defined in proc sql for each row in do while. If i take max in proc sql n pass to macro it is constant for all rows when each row has different number of obs. The *into* clause uses only 1 value from whole table for assigning.&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Oct 2019 10:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596457#M8013</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-15T10:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596473#M8014</link>
      <description>I removed array only and added another step for parsing thanks for your solution</description>
      <pubDate>Tue, 15 Oct 2019 11:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596473#M8014</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-15T11:20:52Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596504#M8015</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292567"&gt;@Priyanka2019&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Ratecnt defined in above post was giving count for each row. But array was to be defined for each row based on Number of observations each row has. How to use ratecnt defined in proc sql for each row in do while. If i take max in proc sql n pass to macro it is constant for all rows when each row has different number of obs. The *into* clause uses only 1 value from whole table for assigning.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That doesn't make any sense.&amp;nbsp; You cannot define an array differently on different observations. Anymore than you could change the name of a variable on different observations.&amp;nbsp; You can choose how many of the variables in the array you actually USE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array X [2000] ;
do i=1 to ratecnt;
  x[i]= ....
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2019 12:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596504#M8015</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-15T12:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596580#M8016</link>
      <description>Tom this makes sense system shouldnt allow it i didnt think through, ratecnt wasnt working in do while loop but will just recheck this once and confirm back. Have one more query regarding survey parsing is there where Request response:value1,obs2, obs 3 etc, ans:value5 List, ques no - 1.2 , Cu response =bcx, repeated for multiple qs are there. Was wondering same above logic will work without array n using wordcount and scan. Any Thoughts...many curly braces in between for a single q n ans in obs as json</description>
      <pubDate>Tue, 15 Oct 2019 16:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596580#M8016</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-15T16:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596581#M8017</link>
      <description>Also some obs are strings in this</description>
      <pubDate>Tue, 15 Oct 2019 16:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596581#M8017</guid>
      <dc:creator>Priyanka2019</dc:creator>
      <dc:date>2019-10-15T16:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing JSON string stored in parameter column</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596589#M8018</link>
      <description>&lt;P&gt;If the JSON is too complex you might have trouble parsing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way that might work is to parse it in parts.&amp;nbsp; Use one delimiter to scan across and then apply the same method to the result but using the second delimiter there.&amp;nbsp; &amp;nbsp;For quotes you probably need to look at the Q modifier on the SCAN() and COUNTW() functions. You might want to use the DEQUOTE() function to remove them once you are down to a single value.&amp;nbsp; Note that DEQUOTE() does nothing to the value if it is not enclosed in quotes, so need to check first, just apply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the nesting of [ ] and { } in the JSON is at all complex then it will probably make using the simple SCAN() function not enough.&amp;nbsp; You will either need to make a much more complex logic to parse it.&amp;nbsp; Or just figure out how to write the strings to a file and read it using the JSON libname engine instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename sample temp;
data _null_;
 file sample;
 put '['
"{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""3.25*"",""type"":""Single Rate"",""needVerification"":""true""},
{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""2.87*"",""type"":""Joint Rate"",""needVerification"":""true""},{""creditUnionValue"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":"""",""type"":""Joint Dev"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""40000"",""type"":""Benefit Maximize"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""11"",""type"":""Termination "",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""22"",""type"":""Eligibility Age"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""UNL"",""type"":""Term Of Insurance"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""READONLY"",""notes"":null,""intergroupValue"":""7501"",""type"":""Benefit Amount"",""needVerification"":""false""},{""crediting"":null,""intergroupVisibility"":""DEFAULT"",""notes"":null,""intergroupValue"":"""",""type"":""Delinquent Payment"",""needVerification"":""false""}"
']'
;
run;


libname sample json;

proc print data=sample.alldata (obs=10);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;    Obs    P    P1                      V    Value

       1    1    creditUnionValue        1    null
       2    1    intergroupVisibility    1    READONLY
       3    1    notes                   1    null
       4    1    intergroupValue         1    3.25*
       5    1    type                    1    Single Rate
       6    1    needVerification        1    true
       7    1    creditUnionValue        1    null
       8    1    intergroupVisibility    1    READONLY
       9    1    notes                   1    null
      10    1    intergroupValue         1    2.87*
&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2019 17:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Parsing-JSON-string-stored-in-parameter-column/m-p/596589#M8018</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-15T17:09:10Z</dc:date>
    </item>
  </channel>
</rss>

