<?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: SAS macro for reading input file and write the contents to output file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968025#M376488</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;To get familiar using SAS macro with the reading of complex files containing special characters % and &amp;amp; and write it to the output file.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't.&lt;/P&gt;
&lt;P&gt;Use the macro to generate the SAS code that can read the file.&amp;nbsp; The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_file(infile);
%local loc outfile;
%if 0=%length(&amp;amp;infile) %then %put ERROR: No filename provided.;
%else %if %sysfunc(fileexist(&amp;amp;infile)) %then %do;
  %let loc=%sysfunc(findc(&amp;amp;infile,.,-%length(&amp;amp;infile)));
  %if ^(&amp;amp;loc) %then %let outfile=&amp;amp;infile._out;
  %else %let outfile=%qsubstr(&amp;amp;infile,1,&amp;amp;loc-1)_out.%qsubstr(&amp;amp;infile,&amp;amp;loc+1);

data _null_;
  infile "&amp;amp;infile";
  file "&amp;amp;outfile";
  input;
  put _infile_;
run;

%end;
%else %put ERROR: &amp;amp;=infile is not found.;

%mend check_file;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Jun 2025 13:54:09 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-06-03T13:54:09Z</dc:date>
    <item>
      <title>SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968010#M376479</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; It is required to read input file (can be a log file or text file or other) which contains blanks lines, lines with % and &amp;amp; characters, lines without these % and &amp;amp; characters. Need to&lt;/P&gt;
&lt;P&gt;1. Check that the input file exists or not&lt;/P&gt;
&lt;P&gt;2. if input file exists then read it and write the contents to the output file.&lt;/P&gt;
&lt;P&gt;3. Output file name need to be inputfile_out.extension&lt;/P&gt;
&lt;P&gt;Please, help. As of now, I don't have any sample input file and hence not able to attach it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 08:15:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968010#M376479</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2025-06-03T08:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968012#M376480</link>
      <description>&lt;P&gt;Please provide an example, also what you have tried so far.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 09:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968012#M376480</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-06-03T09:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968015#M376482</link>
      <description>&lt;P&gt;I have tried the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_file(infile);
       %if %sysfunc(fileexist(&amp;amp;infile)) %then %do;
            %let filrf = myfile;
            %let rc = %sysfunc(filename(filrf, &amp;amp;infile));
             %let infid = %sysfunc(fopen(&amp;amp;filrf, i));

             %if &amp;amp;infid  &amp;gt; 0 %then %do;
                   %do %while(%sysfunc(fread(&amp;amp;infid) = 0);
                          %let rc = sysfunc(fget(&amp;amp;infid, mystring, 32767));
                           %if %sysfunc(indexc(%superq(mystring), %^&amp;amp;)) &amp;gt; 0 %then %do;
                               %let mystring1 = %sysfunc(tranwrd(%superq(mystring), %, ~));
                               %let mystring1 = %sysfunc(tranwrd(superq(mystring1), &amp;amp;,^));
                               %put The contents of the file are %superq(mystring1);
                      %end;
                       %else %do;
                           %put The contents of the file are %superq(mystring);
                       %end;
                     %end;
                   %end;
                    %let rc = %sysfunc(fclose(&amp;amp;infid));
                  %end;
                   %else %do;
                        %put Could not open the file &amp;amp;infile;
                        return;
                    %end;
                    %end;
               %else %do;
                   %put The file &amp;amp;infile does not exist; 
                %end;
%mend check_file;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here, I have tried to replace % with ~ and &amp;amp; with ^ as these characters when found in the input file are getting evaluated and giving errors. But, if lines containing % and ^ characters could be written as it is to the output file then it will be very helpful. Also, I have not tried yet the code for writing to the output file, instead I am writing to the log. But, need to write to the output file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the above code, I was getting some warnings like when the line is %macro namex(name=, number=); WARNING: Apparent symbolic reference NUMBER not resolved.&lt;/P&gt;
&lt;P&gt;When the line is like this: %do n= 1 %to &amp;amp;number; then WARNING: Apparent symbolic reference NAME not resolved. WARNING: Apparent symbolic reference N not resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 11:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968015#M376482</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2025-06-03T11:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968021#M376484</link>
      <description>&lt;P&gt;Why do you have to read the file in and then write it out to a different file? Why can't you simply perform a copy (or copy+rename) in your operating system? You can even call these operating system commands from SAS.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 12:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968021#M376484</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-06-03T12:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968023#M376486</link>
      <description>&lt;P&gt;To get familiar using SAS macro with the reading of complex files containing special characters % and &amp;amp; and write it to the output file.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 13:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968023#M376486</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2025-06-03T13:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968024#M376487</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;To get familiar using SAS macro with the reading of complex files containing special characters % and &amp;amp; and write it to the output file.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok, but for this task using operating system commands will work and will be much easier.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 13:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968024#M376487</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-06-03T13:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968025#M376488</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;To get familiar using SAS macro with the reading of complex files containing special characters % and &amp;amp; and write it to the output file.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't.&lt;/P&gt;
&lt;P&gt;Use the macro to generate the SAS code that can read the file.&amp;nbsp; The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_file(infile);
%local loc outfile;
%if 0=%length(&amp;amp;infile) %then %put ERROR: No filename provided.;
%else %if %sysfunc(fileexist(&amp;amp;infile)) %then %do;
  %let loc=%sysfunc(findc(&amp;amp;infile,.,-%length(&amp;amp;infile)));
  %if ^(&amp;amp;loc) %then %let outfile=&amp;amp;infile._out;
  %else %let outfile=%qsubstr(&amp;amp;infile,1,&amp;amp;loc-1)_out.%qsubstr(&amp;amp;infile,&amp;amp;loc+1);

data _null_;
  infile "&amp;amp;infile";
  file "&amp;amp;outfile";
  input;
  put _infile_;
run;

%end;
%else %put ERROR: &amp;amp;=infile is not found.;

%mend check_file;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Jun 2025 13:54:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968025#M376488</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-06-03T13:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968026#M376489</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;To get familiar using SAS macro with the reading of complex files containing special characters % and &amp;amp; and write it to the output file.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't.&lt;/P&gt;
&lt;P&gt;Use the macro to generate the SAS code that can read the file.&amp;nbsp; The macro language is for generating code. It is not really intended (or well suited) for manipulating data. That is what the actual SAS language is for.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I also agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Part of learning how to use the SAS macro language is learning when NOT to use the SAS macro language.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 14:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968026#M376489</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-06-03T14:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for reading input file and write the contents to output file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968032#M376491</link>
      <description>&lt;P&gt;Thank you very much Tom, it is very helpful. Also, thank you very much for the valuable advise.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jun 2025 14:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-reading-input-file-and-write-the-contents-to/m-p/968032#M376491</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2025-06-03T14:58:18Z</dc:date>
    </item>
  </channel>
</rss>

