<?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: Reading a whole text file in a single row in SAS dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812250#M320460</link>
    <description>How are you creating your JSON file?&lt;BR /&gt;&lt;BR /&gt;There may be a way to still create the file with having the data on different rows. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Mon, 09 May 2022 19:14:13 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-05-09T19:14:13Z</dc:date>
    <item>
      <title>Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812209#M320443</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I am trying to read a text file that has line breaks. I would like to read the whole file in a single row in SAS dataset. Any pointers to how I could achieve this?&lt;/P&gt;
&lt;P&gt;My current code is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.base64;
   length text $32767;
   retain text '';
   infile b64img;
   input;
   text=_infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but the above creates a data set that has a row per line from the source file.&lt;/P&gt;
&lt;P&gt;Many thanks in advance,&lt;/P&gt;
&lt;P&gt;Olli&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 16:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812209#M320443</guid>
      <dc:creator>ojaro</dc:creator>
      <dc:date>2022-05-09T16:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812245#M320457</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272636"&gt;@ojaro&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As long as the whole file will fit into a 32767 byte string, it should be possible, if the file is read as binary stream data (record format = N).&lt;/P&gt;
&lt;P&gt;The line breaks are "outside" the file content and are not read, so a line is followed by the next with no delimiters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename b64img "c:\temp\xif.txt";
data work.base64;
  infile ind recfm=N lrecl=32767;
  input text $32767.;
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>Mon, 09 May 2022 18:45:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812245#M320457</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-05-09T18:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812249#M320459</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The files are likely to be larger than 32767 characters, since they are base64 encoded images.&lt;/P&gt;
&lt;P&gt;I am planning to use the content from the file as an input for another text file I am going to use as a JSON payload for proc http. Do you think I could somehow append the text directly to a another file without the character limit? If this is possible, I somehow need to append the whole file in one (not row by row), since the base64 data must go in the JSON as an attribute value, so must be between quotes and with no line breaks.&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 19:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812249#M320459</guid>
      <dc:creator>ojaro</dc:creator>
      <dc:date>2022-05-09T19:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812250#M320460</link>
      <description>How are you creating your JSON file?&lt;BR /&gt;&lt;BR /&gt;There may be a way to still create the file with having the data on different rows. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 09 May 2022 19:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812250#M320460</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-09T19:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812252#M320462</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code so far, but it does not work, since it will end up adding quotes around every line in the image file. Also, if I move the quotes to previous and next data step, I end up with a line break after between the quotes and the base64 image data, which messes up the processing at server side for the payload.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    file payload;
    text = '{"represent": {"algorithmOptions": ["ROC_STANDARD_REPRESENTATION", "ROC_STANDARD_DETECTION", "ROC_ANALYTICS"], "minQuality": -4.0, "k": -1, "minSize": 20, "falseDetectionRate": 0.02, "image":';
    put text+(-1)@;
run;

data _null_;
	file payload mod;
    length text $32767;
    retain text '';
    infile base64img;
	input;
	put '"' _infile_ '"';
run;

data _null_;
    file payload mod;
    text = '}}';
    put text+(-1)@;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 May 2022 19:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812252#M320462</guid>
      <dc:creator>ojaro</dc:creator>
      <dc:date>2022-05-09T19:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812254#M320463</link>
      <description>&lt;P&gt;Ok, if you're doing a data step to create JSON you can definitely create a single item without spaces/line breaks. Instead of asking to read the file as a single line, what's your actual issue that makes you think you require this.&lt;BR /&gt;&lt;BR /&gt;Instead of infile I would read the file in first and then use a SET statement to access that data set and then write the image data.&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 19:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812254#M320463</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-09T19:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812255#M320464</link>
      <description>&lt;P&gt;Thanks&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, I did try a version like this as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	file payload;
	
	text = '{"request": {"algorithmId": 512, "minQuality": -4.0, "minSize": 20, "falseDetectionRate": 0.02, "k": -1, "image": ';
	put text+(-1)@; 
run;

data work.base64;
   length text $32767;
   retain text '';
   infile b64img;
   input;
   text=_infile_;
run;

data _null_;
	set work.base64;
	file payload mod;
	textf = cats('"',text,'"');
	put textf+(-1)@;
run;

data _null_;
	file payload mod;
	
	text = '}}';
	put text+(-1)@; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, I am not quite sure how I need to modify the following, so that it appends the all lines of base64 data within the quotes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set work.base64;
	file payload mod;
	textf = cats('"',text,'"');
	put textf+(-1)@;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any pointers?&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 19:39:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812255#M320464</guid>
      <dc:creator>ojaro</dc:creator>
      <dc:date>2022-05-09T19:39:21Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812315#M320496</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/272636"&gt;@ojaro&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be possible to change your code into one data step. I cannot find time do make a working example until later today, but the principle would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;First put the leading JSON + double quote to the outfile,&lt;/LI&gt;
&lt;LI&gt;Then read the infile one byte at a time and write it to the outfile until end-of-file is reached,&lt;/LI&gt;
&lt;LI&gt;And then put the finishing double quote + JSON.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 08:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812315#M320496</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-05-10T08:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812339#M320510</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ended up writing some Python to achieve a desired result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp;
declare object py(python);
submit into py;
import json

def writePayload(payload_fn,base64_fn):
    "Output: write_result"
    write_result=-1

    base64_file = open(base64_fn, "r")
    base64_data=base64_file.read().replace("\n", "")
    base64_file.close()

    payload_data='{"represent": {"algorithmOptions": ["ROC_STANDARD_REPRESENTATION", "ROC_STANDARD_DETECTION", "ROC_ANALYTICS"], "minQuality": -4.0, "k": -1, "minSize": 20, "falseDetectionRate": 0.02, "image":"'
    payload_data=payload_data + base64_data
    payload_data=payload_data + '"}}'
    payload_file = open(payload_fn, 'w')
    payload_file.write(payload_data)
    payload_file.close()

    return write_result
endsubmit;
rc = py.publish();
rc = py.call("writePayload",&amp;amp;payload_fn.,&amp;amp;base64_fn.);
write_result = py.results["write_result"];
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if there is a pure SAS way to do this, I would be still interested in knowing how.&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 09:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812339#M320510</guid>
      <dc:creator>ojaro</dc:creator>
      <dc:date>2022-05-10T09:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a whole text file in a single row in SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812428#M320557</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data _null_;
	set work.base64 end=eof;
	file payload mod;
       *quote for start of line;
        if _n_=1 then put('"');
       *data written to file;
        put trim(text)+(-1)@;
       *write end quotes;
       if eof then  put('"');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Something like this, roughly?&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 15:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-whole-text-file-in-a-single-row-in-SAS-dataset/m-p/812428#M320557</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-10T15:23:11Z</dc:date>
    </item>
  </channel>
</rss>

