Hello,
I'm wondering if it is possible to write specific lines of a SAS program to a file from that same SAS program and, if so, how to go about that.
Thanks in advance.
Hi,
Your request is pretty old but this might help some other reader.
Did you mean something like this, where the content of the macro is saved in another file?
%macro demo (memname=);
%put Message in the log;*won't appear in the new file;
title 'This is a demo';
proc print data=sashelp.&memname.;
run;
%mend demo;
options mfile mprint ;
filename mprint "&xxproject./pgms/new.sas ";
%demo(memname=class);
It's the latter of those two options. I want to save a macro definition to a file that I can later %INCLUDE into other SAS programs:
%macro prnt(var,sum); proc print data=srhigh; var &var; sum ∑ run; %mend prnt;
*code to save lines 1-16 to my_macro.sas to C:\Users\me\desktop\;
I think you might theoretically be able to do what you are asking (by reading in the sas program via a datastep and creating another sas program as the output) . But I dont think you should.
If you want a macro to be available to other SAS Programs you should save the macro in an autocall library.
I'm looking for a bit more explanation.
In particular, the words "saved to a file from that same SAS program" don't have an obvious meaning to me.
Please see my reply to Reeza above for further explanation.
So, let me see if this is what you want. You want a program to browse through your code, and pick out the SAS macro(s), and then save them one-by-one into files on your hard disk or network somewhere, so each can be used via %include in other programs. Is that right?
I'm not aware of any tool that does this.
Have you tried writing your program to read in the SAS log (save to a named file) and parse the text? You would need to set "OPTIONS MLOGIC SYMBOLGEN" to capture verbose macro output to log.
Hi,
Your request is pretty old but this might help some other reader.
Did you mean something like this, where the content of the macro is saved in another file?
%macro demo (memname=);
%put Message in the log;*won't appear in the new file;
title 'This is a demo';
proc print data=sashelp.&memname.;
run;
%mend demo;
options mfile mprint ;
filename mprint "&xxproject./pgms/new.sas ";
%demo(memname=class);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.