<?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: Process JSON with infile statment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313415#M68103</link>
    <description>&lt;P&gt;OK. This could give you a start .&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-sas"&gt;data x;
infile '/folders/myfolders/json.txt' lrecl=320000 dsd dlm='{},';
input x : $400. @@;
if not missing(x);
run;
data want;
 set x;
 name=dequote(strip(scan(x,1,':','mq')));
 value=dequote(strip(scan(x,-1,':','mq')));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 22 Nov 2016 11:17:00 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-11-22T11:17:00Z</dc:date>
    <item>
      <title>Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313400#M68093</link>
      <description>&lt;P&gt;Hi there&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if someone could help me, I have been using SAS for a good few years but are coming out of my comfort zone on this one. I want to process data from a JSON file that I get from&amp;nbsp;a API call using a filename statement as per the example below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems to work until I try and read in the "comment" field, which I have commented out below. Basically this field only exists for some records which I think is causing the problem as when uncommented, it appears that only records where this is populated come through.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I dont have proc dc2 and I have dabbled with groovey but I dont think I have the extra jar packages and if possible would prefer to go though this approach as, to my shame, have little knowledge of Javascipt.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My code example below&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename api url "https://api.mydata.com/api/responses.json"

DATA example;
INFILE api LRECL = 1000000 SCANOVER encoding="utf-8" truncover dsd dlm="}},{";
INPUT
@'"id":' Response_ID : best32.
@'"time":' Date: $23. 
@'"ip_address":' ip_address: $16.
@'"identity":' identity: $32.
@'"page":' page: $150.
@'"user_agent":' user_agent: $150.
@'"answer":' Main: $100.
/* @'"comment":' comment: $100.*/
@'"SessionID":' session_id: best32.
@'"Language":' Language: $5.
@'"Locale":' Locale: $2.
@'"Channel":' Channel: $20.
@@;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="array-key-number"&gt;Example of the data I am reading in&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="array-key-number"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;{
"id": 89702492,
"time": "2016-10-06 14:26:25 UTC",
"anon_visitor_id": "xxxxxxxxxxxxxxxxxxxxxx",
"ip_address": "111.111.111.111",
"identity": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"page": "https://secure.hello.com",
"referrer": null,
"user_agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit",
"nudge_id": 111111,
"nudge_name": "ATTRIBUTION - Hello",
"answered_questions":
{
312102:
{
"question_id": 312102,
"question_title": "what is the time&amp;nbsp;",
"question_type": "radio",
"answer": "Other please say",
"selected_option_id": 890309,
"comment": "I dont know"
}
&amp;nbsp;
}
,
&amp;nbsp;
"properties":
{
"SessionID": "4444444444",
"Language": "fr",
"Locale": "Be",
"Channel": "Affiliate"
}
&amp;nbsp;
}
,&lt;/PRE&gt;&lt;P&gt;&lt;SPAN class="array-key-number"&gt;Thanks so much for your help&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="array-key-number"&gt;Higgy&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="punctuation brace"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 22 Nov 2016 10:08:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313400#M68093</guid>
      <dc:creator>higgoboss</dc:creator>
      <dc:date>2016-11-22T10:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313405#M68096</link>
      <description>&lt;P&gt;If the "comment" variable is not present at all, then yes your import will not work. &amp;nbsp;Here is another solution presented by KSharp. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/Reading-json-file-into-SAS-using-SAS-enterprise-guide/td-p/205044" target="_blank"&gt;https://communities.sas.com/t5/SAS-Enterprise-Guide/Reading-json-file-into-SAS-using-SAS-enterprise-guide/td-p/205044&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The principal being to import all the data as parameter/response and then transpose it up. &amp;nbsp;Means a little more work in datastep getting the output exactly as you like, but would be more flexible on adding nor parameters or dropping them.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 10:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313405#M68096</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-22T10:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313413#M68101</link>
      <description>&lt;P&gt;You have JSON Objectives that would mess up the result.&lt;/P&gt;
&lt;P&gt;What kind of output do you like to see ? post the result dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 10:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313413#M68101</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-22T10:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313414#M68102</link>
      <description>&lt;P&gt;Hi there&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ksharps answer seems to have potential, however in the first datastep&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;"user_agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/11.1.1111.111 Safari/111.11",&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Comes out as&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;like Gecko) Chrome/11.1.1111.11 Safari/111.11"&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 11:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313414#M68102</guid>
      <dc:creator>higgoboss</dc:creator>
      <dc:date>2016-11-22T11:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313415#M68103</link>
      <description>&lt;P&gt;OK. This could give you a start .&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-sas"&gt;data x;
infile '/folders/myfolders/json.txt' lrecl=320000 dsd dlm='{},';
input x : $400. @@;
if not missing(x);
run;
data want;
 set x;
 name=dequote(strip(scan(x,1,':','mq')));
 value=dequote(strip(scan(x,-1,':','mq')));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Nov 2016 11:17:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313415#M68103</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-22T11:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313423#M68105</link>
      <description>&lt;P&gt;Thanks Ksharp - but it doesnt seem to work for&amp;nbsp;user agent field which it puts accross two rows. The original data has a commas after&amp;nbsp;KHTML which I think makes it go to the next row&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;[&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;"id":1111111&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;"time":"2016-11-21 20:41:18 UTC"&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;"user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;like Gecko) Chrome/11.1.1111.11 Safari/111.11"&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;"nudge_id":164851&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 22 Nov 2016 11:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313423#M68105</guid>
      <dc:creator>higgoboss</dc:creator>
      <dc:date>2016-11-22T11:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313431#M68109</link>
      <description>&lt;P&gt;OK. Try this one .&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-sas"&gt;data x;
infile '/folders/myfolders/json.txt' lrecl=320000 dsd dlm='{},:[]';
input x : $400. @@;
run;
data want;
 merge x x(firstobs=2 rename=(x=value));
 if lowcase(x) in ("id" "time" "ip_address" "identity" "page" 
"user_agent" "answer" "comment" "sessionid"
 "language" "locale" "channel");
run;




proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Nov 2016 11:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313431#M68109</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-22T11:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313452#M68116</link>
      <description>&lt;P&gt;Thanks so much Sir - that seems to work and is a good little work around !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wish I had posted on this page earlier - would have saved me hours of time&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Higgy&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 13:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313452#M68116</guid>
      <dc:creator>higgoboss</dc:creator>
      <dc:date>2016-11-22T13:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Process JSON with infile statment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313454#M68117</link>
      <description>&lt;P&gt;&lt;FONT size="3"&gt;Higgy,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;If previous answers did not provide what you need, you may want to check this blog if you have SAS 9.4,&lt;/FONT&gt;&lt;/P&gt;
&lt;H1 class="entry-title"&gt;&lt;FONT size="3"&gt;&lt;A href="http://blogs.sas.com/content/sasdummy/2015/09/28/parse-json-from-sas/" target="_self"&gt;Using SAS DS2 to parse JSON&lt;/A&gt;&lt;/FONT&gt;&lt;/H1&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;Hope this helps,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;Ahmed&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 13:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Process-JSON-with-infile-statment/m-p/313454#M68117</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2016-11-22T13:07:02Z</dc:date>
    </item>
  </channel>
</rss>

