<?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: Keep leading blanks using fget() for reading external file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/742112#M232064</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; &lt;BR /&gt;I would like to spend more time. But I have only 1 or 2 hours to stay here.&lt;BR /&gt;And Fortunately ,In most posts  OP would post the output he want see,so I can compare it with my code.&lt;BR /&gt;So you still in your original company ? and still in Australia ?</description>
    <pubDate>Tue, 18 May 2021 11:54:27 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-05-18T11:54:27Z</dc:date>
    <item>
      <title>Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741708#M231883</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I'm using fget() to read records from an external file into a SAS variable. Everything works except that leading blanks aren't kept (=string in SAS variable starts with first non-blank character).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help how to keep the leading blanks would be really appreciated. I simply can't make it work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below self contained code sample showcasing the challenge.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Create sample file&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let source_file=%sysfunc(pathname(work))\myscript.sas;
data _null_;
  infile datalines truncover;
  file "&amp;amp;source_file";
  input;
  put _infile_;
  datalines4;
data sample;
  set sashelp.class;
run;
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Syntax reading the sample file that keeps the leading blanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file print;
  infile "&amp;amp;source_file" lrecl=132;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_2-1621158037101.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59485i7C90DC7BD5F3B508/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_2-1621158037101.png" alt="Patrick_2-1621158037101.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Syntax using fget() that doesn't keep the leading blanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* here the code I want to use BUT need to keep leading blanks from source file */
data _null_;
  file print;
  length _filrf $8 _instr $132;
  informat _instr $char132.;
  _filrf='demo';
  _rc=filename(_filrf, cats("&amp;amp;source_file"));                                                                                  
  _fid=fopen(_filrf);   
  if _fid &amp;gt; 0 then                                                                                                                     
    do while(fread(_fid)=0);                                                                                                  
      _rc=fget(_fid, _instr, 132);
      put _instr;                                                                                                                             
    end;                                                                                                                                 
  _rc=fclose(_fid);                                                                                                        
  _rc=filename(_filrf);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1621158004863.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59482iBF5D80B8A1392972/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1621158004863.png" alt="Patrick_1-1621158004863.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The real problem I need to solve&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I've got a set of main programs with %includes to other programs - and the %included programs can have further %includes ("cascading %includes")&lt;/P&gt;
&lt;P&gt;I want to get rid of the includes by creating new main programs that got all the %included code directly in the main script.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've got a working solution using #3 syntax but it's not acceptable to loose all the code indention when generating the new scripts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should there be no solution to keep the leading blanks with my approach then I take accept more than happily also other coding approaches that solve my actual problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution needs to work under Windows and Unix. That's why I went for an approach using SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 09:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741708#M231883</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-16T09:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741714#M231888</link>
      <description>&lt;P&gt;Using informat $char. :&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;%let source_file=%sysfunc(pathname(work))\myscript.sas;
data _null_;
  infile datalines truncover;
  file "&amp;amp;source_file";
  input x $char100.;
  put x $char100.;
  datalines4;
data sample;
  set sashelp.class;
run;
;;;;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 16 May 2021 11:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741714#M231888</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-16T11:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741745#M231915</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably haven't really read the whole question?&lt;/P&gt;
&lt;P&gt;Writing the sample file works and is not the issue/question asked.&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;It's about reading the sample file using data step functions. Because I want to read .sas files with %include statements and create a single .sas files with all the code as result, I have switch the external file whenever I encounter an %include, read the whole %included file, and then switch back to the main programs and continue reading the records after the %include statement. That's why I'm using data step functions also to define and open the external file.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 15:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741745#M231915</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-16T15:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741747#M231917</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think FGET &lt;EM&gt;did&lt;/EM&gt; read the leading blanks. It's just that the PUT statement using &lt;EM&gt;list &lt;/EM&gt;PUT didn't write them. With &lt;EM&gt;formatted&lt;/EM&gt; PUT, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put _instr $132.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;they appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Also, the INFORMAT statement doesn't seem to be necessary.&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 15:48:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741747#M231917</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-16T15:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741787#M231940</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much! Tried so many things but not this very simple one.&lt;/P&gt;
&lt;P&gt;Do you also have an explanation why using an explicit format has this effect?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;True, the informat has no effect. I've only added it to the sample code so no one is going to propose it....&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 22:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741787#M231940</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-16T22:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741812#M231952</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Do you also have an explanation why using an explicit format has this effect?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is one of the differences between &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n0jks6tlw4a1v1n1ssob6gtpxfth.htm" target="_blank" rel="noopener"&gt;&lt;EM&gt;formatted&lt;/EM&gt; output&lt;/A&gt; and &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0jcwhe1ofmb49n1xf1q2kc44b0v.htm" target="_blank" rel="noopener"&gt;&lt;EM&gt;list&lt;/EM&gt; output&lt;/A&gt;. The documentation of the latter says in section "&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0jcwhe1ofmb49n1xf1q2kc44b0v.htm#p1qbuceefg1qt4n1xyi3mna8g931" target="_blank" rel="noopener"&gt;Using List Output&lt;/A&gt;":&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"&lt;SPAN&gt;Character values are left-aligned in the field; leading and trailing blanks are removed. To include blanks (in addition to the blank inserted after each value), use formatted or column output instead of list output."&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 17 May 2021 07:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741812#M231952</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-17T07:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741827#M231958</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; -&amp;nbsp;Learned something &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe I was so far only fully aware of the different styles for reading external data but not for writing it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've ended up using format &amp;amp;varying. to not pad all the records with up to 132 blanks.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file print;
  length _filrf $8 _instr $132;
  informat _instr $char132.;
  _filrf='demo';
  _rc=filename(_filrf, cats("&amp;amp;source_file"));                                                                                  
  _fid=fopen(_filrf);   
  if _fid &amp;gt; 0 then                                                                                                                     
    do while(fread(_fid)=0);                                                                                                  
      _rc=fget(_fid, _instr, 132);
      _l=lengthn(_instr);
      put _instr $varying. _l;                                                                                                                             
    end;                                                                                                                                 
  _rc=fclose(_fid);                                                                                                        
  _rc=filename(_filrf);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 May 2021 11:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741827#M231958</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-17T11:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741848#M231972</link>
      <description>Patrick,&lt;BR /&gt;Sorry. I have no time to go through this question.&lt;BR /&gt;There are some many post everyday at sas communities.So I just pay a few second to read a post .&lt;BR /&gt;&lt;BR /&gt;Best !&lt;BR /&gt;Xia Ke Shan</description>
      <pubDate>Mon, 17 May 2021 12:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/741848#M231972</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-17T12:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/742088#M232053</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then may-be just answer questions where you can spend the necessary time for reading and understanding the problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe I've got sufficient experience to decide what answers get me further but you might send beginners on the wrong path if not really addressing their problem.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 08:54:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/742088#M232053</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-18T08:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: Keep leading blanks using fget() for reading external file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/742112#M232064</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; &lt;BR /&gt;I would like to spend more time. But I have only 1 or 2 hours to stay here.&lt;BR /&gt;And Fortunately ,In most posts  OP would post the output he want see,so I can compare it with my code.&lt;BR /&gt;So you still in your original company ? and still in Australia ?</description>
      <pubDate>Tue, 18 May 2021 11:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-leading-blanks-using-fget-for-reading-external-file/m-p/742112#M232064</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-18T11:54:27Z</dc:date>
    </item>
  </channel>
</rss>

