DATA Step, Macro, Functions and more

Export executed code to external file

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Export executed code to external file

Hello,

 

We have a SAS code executed regulary in our data management department. Programs are pretty large (20000 lines) with lots of parametres. Every time we save logs to external files and I wonder if it is possible also to save interpreted code (all variables evaluated) or the base code once executed to external text file too? 

 

Thanks in advance for your ansvers. 


Accepted Solutions
Solution
‎06-19-2017 08:06 AM
Frequent Contributor
Posts: 129

Re: Export executed code to external file

[ Edited ]
Posted in reply to Astounding

Hi,

 

the MFILE MPRINT functionality is explained >here<

 

Edit: maybe turning on these options is sufficient for you:

options mprint mprintnest source source2;

options nomprint nomprintnest nosource nosource2;

%macro test(initial);
proc print data=sashelp.class;where substr(name,1,1) eq "&initial.";run;

%mend test;

%test(A);
%test(J);

options mprint mprintnest source source2;

%macro test(initial);
proc print data=sashelp.class;where substr(name,1,1) eq "&initial.";run;

%mend test;

%test(A);
%test(J);
________________________

- Cheers -

View solution in original post


All Replies
Super User
Posts: 3,254

Re: Export executed code to external file

Posted in reply to gabriel_k

Please explain what you mean by interpreted code. Do you mean the code generated by SAS macros or something else? If you are referring to the generated SAS macro code then using the SAS options MPRINT will add this to the SAS log and if you want the resolved SAS macro variable values use SYMBOLGEN.

 

The SAS "base" code is simply what is stored in your SAS programs, unless you are generating your code by some other means. If this is the case please explain.

Contributor
Posts: 21

Re: Export executed code to external file

I mean the code generated by SAS macros you are wright, and it would be great to have it appart of the log that it is possible to execute this code directly in case I need. 

Super User
Super User
Posts: 7,955

Re: Export executed code to external file

Posted in reply to gabriel_k

In what sense?  You have a program that exports data, therefore that program is stored somewhere as a file, hence you already have this?  Or are you using some macro or something to generate code, or are you using proc export or something.  Some examples might help.  

Contributor
Posts: 21

Re: Export executed code to external file

There is some macro code, sas base code, it exports and imorts data.. does lots of stuff, the question is how to keep track of final code executed and try to have it in the file to rerun the later in case of problems or something.
Super User
Super User
Posts: 7,955

Re: Export executed code to external file

Posted in reply to gabriel_k

As @Oligolas provided (note I would add symbolgen in also):

options mlogic mprint symbolgen source source2;

 

Before the run should put all the required information in the log.  However the log itself will not be "executable" like your requirement.  I think that would require quite some re-work, having the code you already have, instead of running, creating a text file output which is included at the end of the program.  But the question is, why can you not re-run the code you have as apposed to the log?  If your code isn't replicable then there is something fundamentally wrong with thr process.  The code should run the same with the same inputs and create the same output each time.

Super User
Posts: 5,505

Re: Export executed code to external file

Posted in reply to gabriel_k

You can save the generated code to a file of your choice using the MFILE option.  (I believe you need to turn on MPRINT as well to make MFILE effective.)

Solution
‎06-19-2017 08:06 AM
Frequent Contributor
Posts: 129

Re: Export executed code to external file

[ Edited ]
Posted in reply to Astounding

Hi,

 

the MFILE MPRINT functionality is explained >here<

 

Edit: maybe turning on these options is sufficient for you:

options mprint mprintnest source source2;

options nomprint nomprintnest nosource nosource2;

%macro test(initial);
proc print data=sashelp.class;where substr(name,1,1) eq "&initial.";run;

%mend test;

%test(A);
%test(J);

options mprint mprintnest source source2;

%macro test(initial);
proc print data=sashelp.class;where substr(name,1,1) eq "&initial.";run;

%mend test;

%test(A);
%test(J);
________________________

- Cheers -

Contributor
Posts: 21

Re: Export executed code to external file

[ Edited ]
filename mprint "...\mysasfile1.sas";
options mprint mfile;
run; 
options mprint mprintnest source source2;
%macro test(initial);
proc print data=sashelp.class;
where substr(name,1,1) eq "&initial.";
run;
%mend test;
%test(A);
%test(J);

Trying to run your code with the parts mentioned in the article, everything goes smooth no errors ... but code is not printed to the file. What could it be?

Super User
Super User
Posts: 7,955

Re: Export executed code to external file

Posted in reply to gabriel_k

Works fine for me:

filename mprint "...\mysasfile1.sas";
options mprint mfile mprintnest source source2;
%macro test(initial);
  proc print data=sashelp.class;
    where substr(name,1,1) eq "&initial.";
  run;
%mend test;
%test(A);
%test(J);

Make sure your path in the mprint statement exists, you have access to it, and it can overwrite any file there.  You should get a log message if it doesnt work.

Contributor
Posts: 21

Re: Export executed code to external file

Posted in reply to gabriel_k

Probably there was some problem with server I've tried it later and it works fine, that is exectly what I was looking for. thanks a lotSmiley Happy 

Contributor
Posts: 21

Re: Export executed code to external file

Is it possible to make SAS interpret the variables also?

 

For instance if I have a DATA step the macro variables inside are not interpreted in generated code. 

Super User
Super User
Posts: 7,955

Re: Export executed code to external file

Posted in reply to gabriel_k

Not sure I follow you.  Could you provide a small example to illustrate your problem.

Contributor
Posts: 21

Re: Export executed code to external file

Never mind, its fine, I'll run first the code that defins variables. 

Contributor
Posts: 21

Re: Export executed code to external file

Hello, 

 

I have found this article and would like to do exectly they are doing.

http://www.lexjansen.com/pharmasug/2002/proceed/TechTech/tt14.pdf

 

 I have contacted autors but don't have an answer so far. May be you have seen an example of a similar program on the internet?  

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 226 views
  • 2 likes
  • 5 in conversation