DATA Step, Macro, Functions and more

%include sas program in the same folder

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

%include sas program in the same folder

Currently I have my include statements as below:

%include "M:\Program\Macros\one.sas";

%include "M:\Program\Macros\Macros\two.sas";

%include "M:\Program\Macros\Macros\three.sas";

The program that has these two lines are code is located in "M:\Program."

Is there a way to write these three lines where I do not have to include "M:\Program" and SAS will look in the same folder for example "Macros\one.sas?"  I would like this program to run regardless of the Program folder location.

I've tried %include "Macros\one.sas"; but that doesn't seem to work.

Thank you so much!


Accepted Solutions
Solution
‎07-16-2015 05:10 AM
Super User
Super User
Posts: 7,962

Re: %include sas program in the same folder

Just to add, if you only plan on running in Batch then (think its one dot, been a while since I have used it):

%include ".\Macros\one.sas";

%include ".\Macros\Macros\two.sas";

%include ".\Macros\Macros\three.sas";

Then SAS starts with the path of the program you are running and expands the . for you.

View solution in original post


All Replies
Super User
Posts: 11,343

Re: %include sas program in the same folder

I kind of wonder what the problem is with three lines of code?

One approach could be to put those three lines in one program file and then only include that program in the (presumed) main program.

Another approach would be to establish an AUTOCALL macro library and then have the SAS startup add that location.

(Assumes those files are basically macro definitions only).

Contributor
Posts: 29

Re: %include sas program in the same folder

I apologize for the confusion.

There is another program entitled "main.sas" in folder "M:\Program."  Main.sas has the following lines of code:

%include "M:\Program\Macros\one.sas";

%include "M:\Program\Macros\Macros\two.sas";

%include "M:\Program\Macros\Macros\three.sas";

I was wondering if I can make main.sas a bit smart where I do not have to include "M:\Program."

Super User
Posts: 10,035

Re: %include sas program in the same folder

Yes. You can you can run PIPE at your SAS.

Add the following code into Main.sas.

filename x pipe 'dir  M:\Program\*.sas /s /b';

data _null_;

infile x length=len;

input x $varying200. len;

call execute( cats('%include " ' , x ,' "; ') );

run;

Xia Keshan

PROC Star
Posts: 1,322

Re: %include sas program in the same folder

In what environment are you running the program (e.g. Display Manager SAS interactive, batch, Enterprise Guide, etc?)  Different environments have different ways of dynamically finding the location of the "current" program.

Sometimes instead of dynamically figuring it out, it's easiest to just create a global macro session variable with the root path for the project.

So if you had (slightly different structure):

M:\Project1\Program\main.sas

M:\Project1\Pogram\one.sas

M:\Project1\Program\Macros\macro1.sas

M:\Project1\Program\Macros\macro2.sas

M:\Project1\Output\

M:\Project1\Data

M:\Project2\Program\main.sas

etc.

In main.sas, you could %let root=M:\Project1.  It calls all the other programs as needed.  Once you know &root, you know code is in &root\Program, macros are in &root\Program\macros, output goes to &root\Output, etc etc.

Also checkout the macro autocall facility.  It takes only a small amount of setup, but has benefits of %including macros.  (That said, I %included macros for years....)

--Q.

Contributor
Posts: 29

Re: %include sas program in the same folder

Thank you for your response.

Ultimately, I would like to run this in a batch in windows.

I would like to share this program with other users.  If they were to unpackaged this to their desktop, I don't think this approach would work.

PROC Star
Posts: 1,322

Re: %include sas program in the same folder

For PC SAS interactive and batch, I use a macro like:

%macro ProgramName
  (path=0 /*Boolean, include path or not*/
  )
;

%local return; 

%*SYSIN only exists in batch, SAS_EXECFILENAME only exists in interactive;

%*When batch submit;
%let return = %qsysfunc(getOption(SYSIN)); 
%if %superq(return) ne %str() %then %do;
  %if &path ne 1 %then %let return = %qscan(&return,-1,\/);
%end;

%*When DMS interactive;
%if %superq(return)=%str() %then %do;
  %if &path=1 %then %let return = %qsysfunc(sysget(SAS_EXECFILEPATH));
  %else %let return = %qsysfunc(sysget(SAS_EXECFILENAME));
%end;

&return
%mend;

To return program name with path, call like %put %programname(path=1) ;    Something like that could be useful.

Regular Contributor
Posts: 227

Re: %include sas program in the same folder

Here are a couple of pages with information about your big topic: setting up for running in batch.

http://www.sascommunity.org/wiki/Batch_processing_under_Windows

My own naming conventions for folders differ from yours

I have a SAS-Projects folder

under which are projects, such as Project-A, Project-B, Project-Z

one of the 'project' folders is named SAS-site, which is my tool-box;

and it has a sub-folder named 'macros'

Your Q comes up in every class I have taught about macro usage:

"How can I get SAS to find (i.e. search for) my macros?"

thus the by-guess-and-by-golly solution that you have

%include '...\MyMacroA.sas';

%include '...\MyMacroB.sas';

the key issues are explained on this page

SASautos Companion Reusing Macros - sasCommunity

the option SASAUTOS contains the search path (i.e. library, a set of folders)

for the autocall (auto-search) macro facility.

The default value is the fileref sasautos.

use these statement in your autoexec.sas

to add folders to the autocall search list

filename project '.'; *here: project folder;

filename sitemacr '<???>';*there: tool-box containing macros in files;

options mautosource

  sasautos = (Project SiteMacr SASautos);

Now you have the macro autocall facility working for you;

no need to %include your macros.

SAS searches those folders, finds a file with the name of the macro, %includes it,

and the compiled code is saved in catalog work.sasmacro.

... and see the location of the macro with this option

    options mautocomploc;

Contributor
Posts: 29

Re: %include sas program in the same folder

Posted in reply to Ron_Fehd_macro_maven

Wow this is quite a bit of great information.  Thank you for all your help!

Super User
Posts: 3,254

Re: %include sas program in the same folder

I find it a lot easier just to do something like this:

%let MyProgramFolder = M:\Program;

%include "&MyProgramFolder\Macros\one.sas";

%include "&MyProgramFolder\Macros\Macros\two.sas";

%include "&MyProgramFolder\Macros\Macros\three.sas";

However I think using AUTOCALL macros as already discussed is a better approach.

Super Contributor
Super Contributor
Posts: 3,174

Re: %include sas program in the same folder

The SAS AUTOCALL method also permits externalized SASAUTOS environment (concatenation, various OS platform environments considered) change for testing, albeit without making any SAS program code change.  The alternative would be to edit the SAS program directly and change the %LET statement that identifies the SAS program source code reference.

Scott Barry

SBBWorks, Inc.

Trusted Advisor
Posts: 3,214

Re: %include sas program in the same folder

The autocall is for sas-macros not for sas-code.
What you could do is allocating a filename. You can use concatenated locations for that. It a PDS approach sbb will recognize.

  Filename mycode  "M:\Program\Macros" ;

Than you still can decide to use the fileref  "mycode" to be used in a sasautos approach for sas-macro-s or use

   %include nycode(main) ;      

Assuming mian.sas (default suffix .sas) is your code to be included. When developing working with macro-s also very handy to recompile those.

With a different sufffix  enclose the name in quites like "main.sasold"         

---->-- ja karman --<-----
Super Contributor
Super Contributor
Posts: 3,174

Re: %include sas program in the same folder

Thank you for the correction, Jaap -- agreed that SASAUTOS has limited applicability here.  I stand corrected.

Scott Barry

SBBWorks, Inc.

Solution
‎07-16-2015 05:10 AM
Super User
Super User
Posts: 7,962

Re: %include sas program in the same folder

Just to add, if you only plan on running in Batch then (think its one dot, been a while since I have used it):

%include ".\Macros\one.sas";

%include ".\Macros\Macros\two.sas";

%include ".\Macros\Macros\three.sas";

Then SAS starts with the path of the program you are running and expands the . for you.

Contributor
Posts: 29

Re: %include sas program in the same folder

Thank you all for the very helpful input.

This worked; thank you so much RW9. Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 15 replies
  • 2554 views
  • 17 likes
  • 10 in conversation