<?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: Way to convert the Code from SAS Log to a SAS File in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827212#M326747</link>
    <description>&lt;P&gt;If you can re-run the program instead then it is even easier.&amp;nbsp; Just use the MFILE option.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p1dhqw0i5yj2m8n15opapnwteqra.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p1dhqw0i5yj2m8n15opapnwteqra.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="xis-details"&gt;
&lt;DIV id="p1mmm7zhnqk6hsn124uo50c01iwd" class="xis-topicContent"&gt;
&lt;DIV id="p1xl70o3g0i0qdn13qlzo0lmkjy8" class="xis-paragraph"&gt;You can direct MPRINT output to an external file by also using the MFILE option and assigning the fileref MPrint to that file. For more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xis-xrefSee"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p0j3zpinabl49ln13q5f5r1t46mg.htm" target="_blank" rel="noopener"&gt;MFILE System Option&lt;/A&gt;&lt;/SPAN&gt;.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV id="n0lw0ne4ffgr1vn0zmfwxa3hwxsj" class="xis-exampleBlock"&gt;
&lt;H2 class="xis-title"&gt;Example: Directing MPRINT Output to an External File&lt;/H2&gt;
&lt;DIV id="p17dfaabt4e092n1ciqf22e7ga9j" class="xis-example"&gt;
&lt;DIV id="p1c8ggbg1ufgsqn1obnwc5sgaihm" class="xis-paragraph"&gt;Adding these statements before the macro call in the previous program sends the MPRINT output to the file DebugMac when the SAS session ends.&lt;/DIV&gt;
&lt;DIV id="n0voh1kf5m5a2cn16sod0qxsikyg" class="xis-codeBlock"&gt;
&lt;PRE class="xis-code"&gt;options mfile mprint;
filename mprint 'debugmac';&lt;/PRE&gt;
&lt;P&gt;Try this example at home:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename ds2post url 'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas';
%include ds2post;

filename mprint temp;
options mfile mprint;
%ds2post(sashelp.class);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is what was written to the fileref MPRINT.&lt;/P&gt;
&lt;PRE&gt; 310        options nomfile;
 311        data _null_;
 312          infile mprint;
 313          input;
 314          put _infile_;
 315        run;
 
 NOTE: The infile MPRINT is:
       Filename=/saswork/SAS_work9085000092C9_odaws03-usw2.oda.sas.com/#LN00038,
       Owner Name=tom.xxx,Group Name=oda,
       Access Permission=-rw-r--r--,
       Last Modified=04Aug2022:15:26:47,
       File Size (bytes)=3377
 
 *----------------------------------------------------------------------------;
 * Get member label in format of dataset option. ;
 * Get dataset contents information in a format to facilitate code generation.;
 * Column names reflect data statement that uses the value. ;
 *----------------------------------------------------------------------------;
 proc sql noprint;
 select cats('(label=',quote(trim(memlabel),"'"),')') into :memlabel trimmed from dictionary.tables where libname="SASHELP" and memna
 me="CLASS" and not missing(memlabel) ;
 create table _ds2post_ as select varnum , nliteral(name) as name length=66 , substrn(informat,1,findc(informat,' .',-49,'kd')) as in
 f length=32 , case when type='char' then cats(':$',length,'.') when not (lowcase(calculated inf) in ('best','f',' ') and scan(inform
 at,2,'.') = ' ') then ':F.' else ' ' end as input length=8 , case when type='num' and length &amp;lt; 8 then cats(max(3,length)) else ' ' e
 nd as length length=1 , lowcase(format) as format length=49 , lowcase(informat) as informat length=49 , case when missing(label) the
 n ' ' else quote(trim(label),"'") end as label length=300 from dictionary.columns where libname="SASHELP" and memname="CLASS" order 
 by varnum ;
 quit;
 *----------------------------------------------------------------------------;
 * Generate data step code ;
 * - For each statement use value of variable named the same as the statement.;
 * - Only variables that are required in that statement are generated. ;
 * - For LABEL statement use = between name and value instead of space. ;
 * - Wrap statements when lines get too long. ;
 * - Eliminate statements when no variables required that statement. ;
 *----------------------------------------------------------------------------;
 filename _code_ temp;
 data _null_;
 file _code_ column=cc ;
 set _ds2post_ (obs=1) ;
 put "data work.class (label='Student Data');" ;
 put @3 "infile datalines dsd dlm='|' truncover;" ;
 length statement $10 string $370 ;
 do statement='input','length','format','informat','label';
 call missing(any,anysplit);
 put @3 statement @ ;
 do p=1 to nobs ;
 set _ds2post_ point=p nobs=nobs ;
 string=vvaluex(statement);
 if statement='input' or not missing(string) then do;
 any=1;
 string=catx(ifc(statement='label','=',' '),name,string);
 if 72&amp;lt;(cc+length(string)) then do;
 anysplit=1;
 put / @5 @ ;
 end;
 put string @ ;
 end;
 end;
 if anysplit then put / @3 @ ;
 if not any then put @1 10*' ' @1 @ ;
 else put ';' ;
 end;
 put 'datalines4;' ;
 run;
 *----------------------------------------------------------------------------;
 * Generate data lines ;
 *----------------------------------------------------------------------------;
 data _null_;
 file _code_ mod dsd dlm='|';
 if _n_ &amp;gt; 20 then stop;
 set sashelp.class ;
 format _numeric_ best32. _character_ ;
 put (_all_) (+0) ;
 run;
 data _null_;
 file _code_ mod ;
 put ';;;;';
 run;
 *----------------------------------------------------------------------------;
 * Copy generated code to target file name and remove temporary file. ;
 *----------------------------------------------------------------------------;
 data _null_ ;
 infile _code_;
 file log ;
 input;
 put _infile_;
 run;
 filename _code_ ;
 *----------------------------------------------------------------------------;
 * Remove generated metadata. ;
 *----------------------------------------------------------------------------;
 proc delete data=_ds2post_;
 run;
 NOTE: 75 records were read from the infile MPRINT.
       The minimum record length was 4.
       The maximum record length was 671.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Notice the long SQL statement makes a line that is 671 bytes long.&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Thu, 04 Aug 2022 19:31:25 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-04T19:31:25Z</dc:date>
    <item>
      <title>Way to convert the Code from SAS Log to a SAS File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827179#M326729</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are there any ways to convert the executed code from the log to a SAS file (with or with out a indentation)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example: The below is the macro executed code printed in the log. Can it be possible to send this to a SAS file which can be executable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="venkibhu14_0-1659634395405.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74145iA3FE2A3446C5962A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="venkibhu14_0-1659634395405.png" alt="venkibhu14_0-1659634395405.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Venkat.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 17:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827179#M326729</guid>
      <dc:creator>venkibhu14</dc:creator>
      <dc:date>2022-08-04T17:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Way to convert the Code from SAS Log to a SAS File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827208#M326743</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not think there's an easy way.&lt;/P&gt;
&lt;P&gt;Do you want to do this because you do not have the source code of a compiled macro?&lt;/P&gt;
&lt;P&gt;Be cautious as one execution of the macro might only reveal 10% of its code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, you could send the log to an external file, like here :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfile '/users/myid/mydir/mylog';
proc printto log=myfile;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then import that log-file into a SAS-dataset (every line of the log becomes one record, and the full line is in one variable).&lt;/P&gt;
&lt;P&gt;You can use the&amp;nbsp;$VARYINGw. informat to do that, or just columns-input 1 - 150 (for example).&lt;BR /&gt;&lt;BR /&gt;Then filter out the records (i.e. observations) starting with the string 'MPRINT(TEMP):' and remove that part with substr function.&lt;BR /&gt;&lt;BR /&gt;Then put (file statement and put statements) the records of your new dataset to an external file again.&lt;BR /&gt;That external file should be a valid program that you can %INCLUDE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827208#M326743</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-08-04T19:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Way to convert the Code from SAS Log to a SAS File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827209#M326744</link>
      <description>&lt;P&gt;You can easily get 90% there. Keep the lines that start with MPRINT(.&amp;nbsp; Remove the MPRINT(...): from the start of the lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile 'myfile.log';
  file 'myfile.sas';
  input ;
  if _infile_ ne ' ' and _infile_=:'MPRINT(';
  _infile_=substrn(_infile_,length(scan(_infile_,1,' '))+1);
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But watch out for extremely long statements, like PROC SQL SELECT statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827209#M326744</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-04T19:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Way to convert the Code from SAS Log to a SAS File</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827212#M326747</link>
      <description>&lt;P&gt;If you can re-run the program instead then it is even easier.&amp;nbsp; Just use the MFILE option.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p1dhqw0i5yj2m8n15opapnwteqra.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p1dhqw0i5yj2m8n15opapnwteqra.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="xis-details"&gt;
&lt;DIV id="p1mmm7zhnqk6hsn124uo50c01iwd" class="xis-topicContent"&gt;
&lt;DIV id="p1xl70o3g0i0qdn13qlzo0lmkjy8" class="xis-paragraph"&gt;You can direct MPRINT output to an external file by also using the MFILE option and assigning the fileref MPrint to that file. For more information, see&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xis-xrefSee"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/vdmmlcdc/8.1/mcrolref/p0j3zpinabl49ln13q5f5r1t46mg.htm" target="_blank" rel="noopener"&gt;MFILE System Option&lt;/A&gt;&lt;/SPAN&gt;.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV id="n0lw0ne4ffgr1vn0zmfwxa3hwxsj" class="xis-exampleBlock"&gt;
&lt;H2 class="xis-title"&gt;Example: Directing MPRINT Output to an External File&lt;/H2&gt;
&lt;DIV id="p17dfaabt4e092n1ciqf22e7ga9j" class="xis-example"&gt;
&lt;DIV id="p1c8ggbg1ufgsqn1obnwc5sgaihm" class="xis-paragraph"&gt;Adding these statements before the macro call in the previous program sends the MPRINT output to the file DebugMac when the SAS session ends.&lt;/DIV&gt;
&lt;DIV id="n0voh1kf5m5a2cn16sod0qxsikyg" class="xis-codeBlock"&gt;
&lt;PRE class="xis-code"&gt;options mfile mprint;
filename mprint 'debugmac';&lt;/PRE&gt;
&lt;P&gt;Try this example at home:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename ds2post url 'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas';
%include ds2post;

filename mprint temp;
options mfile mprint;
%ds2post(sashelp.class);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is what was written to the fileref MPRINT.&lt;/P&gt;
&lt;PRE&gt; 310        options nomfile;
 311        data _null_;
 312          infile mprint;
 313          input;
 314          put _infile_;
 315        run;
 
 NOTE: The infile MPRINT is:
       Filename=/saswork/SAS_work9085000092C9_odaws03-usw2.oda.sas.com/#LN00038,
       Owner Name=tom.xxx,Group Name=oda,
       Access Permission=-rw-r--r--,
       Last Modified=04Aug2022:15:26:47,
       File Size (bytes)=3377
 
 *----------------------------------------------------------------------------;
 * Get member label in format of dataset option. ;
 * Get dataset contents information in a format to facilitate code generation.;
 * Column names reflect data statement that uses the value. ;
 *----------------------------------------------------------------------------;
 proc sql noprint;
 select cats('(label=',quote(trim(memlabel),"'"),')') into :memlabel trimmed from dictionary.tables where libname="SASHELP" and memna
 me="CLASS" and not missing(memlabel) ;
 create table _ds2post_ as select varnum , nliteral(name) as name length=66 , substrn(informat,1,findc(informat,' .',-49,'kd')) as in
 f length=32 , case when type='char' then cats(':$',length,'.') when not (lowcase(calculated inf) in ('best','f',' ') and scan(inform
 at,2,'.') = ' ') then ':F.' else ' ' end as input length=8 , case when type='num' and length &amp;lt; 8 then cats(max(3,length)) else ' ' e
 nd as length length=1 , lowcase(format) as format length=49 , lowcase(informat) as informat length=49 , case when missing(label) the
 n ' ' else quote(trim(label),"'") end as label length=300 from dictionary.columns where libname="SASHELP" and memname="CLASS" order 
 by varnum ;
 quit;
 *----------------------------------------------------------------------------;
 * Generate data step code ;
 * - For each statement use value of variable named the same as the statement.;
 * - Only variables that are required in that statement are generated. ;
 * - For LABEL statement use = between name and value instead of space. ;
 * - Wrap statements when lines get too long. ;
 * - Eliminate statements when no variables required that statement. ;
 *----------------------------------------------------------------------------;
 filename _code_ temp;
 data _null_;
 file _code_ column=cc ;
 set _ds2post_ (obs=1) ;
 put "data work.class (label='Student Data');" ;
 put @3 "infile datalines dsd dlm='|' truncover;" ;
 length statement $10 string $370 ;
 do statement='input','length','format','informat','label';
 call missing(any,anysplit);
 put @3 statement @ ;
 do p=1 to nobs ;
 set _ds2post_ point=p nobs=nobs ;
 string=vvaluex(statement);
 if statement='input' or not missing(string) then do;
 any=1;
 string=catx(ifc(statement='label','=',' '),name,string);
 if 72&amp;lt;(cc+length(string)) then do;
 anysplit=1;
 put / @5 @ ;
 end;
 put string @ ;
 end;
 end;
 if anysplit then put / @3 @ ;
 if not any then put @1 10*' ' @1 @ ;
 else put ';' ;
 end;
 put 'datalines4;' ;
 run;
 *----------------------------------------------------------------------------;
 * Generate data lines ;
 *----------------------------------------------------------------------------;
 data _null_;
 file _code_ mod dsd dlm='|';
 if _n_ &amp;gt; 20 then stop;
 set sashelp.class ;
 format _numeric_ best32. _character_ ;
 put (_all_) (+0) ;
 run;
 data _null_;
 file _code_ mod ;
 put ';;;;';
 run;
 *----------------------------------------------------------------------------;
 * Copy generated code to target file name and remove temporary file. ;
 *----------------------------------------------------------------------------;
 data _null_ ;
 infile _code_;
 file log ;
 input;
 put _infile_;
 run;
 filename _code_ ;
 *----------------------------------------------------------------------------;
 * Remove generated metadata. ;
 *----------------------------------------------------------------------------;
 proc delete data=_ds2post_;
 run;
 NOTE: 75 records were read from the infile MPRINT.
       The minimum record length was 4.
       The maximum record length was 671.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Notice the long SQL statement makes a line that is 671 bytes long.&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Way-to-convert-the-Code-from-SAS-Log-to-a-SAS-File/m-p/827212#M326747</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-04T19:31:25Z</dc:date>
    </item>
  </channel>
</rss>

