BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dtchoi86
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

15 REPLIES 15
ballardw
Super User

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).

dtchoi86
Fluorite | Level 6

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."

Ksharp
Super User

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

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
dtchoi86
Fluorite | Level 6

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.

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Ron_MacroMaven
Lapis Lazuli | Level 10

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;

dtchoi86
Fluorite | Level 6

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

SASKiwi
PROC Star

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.

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

jakarman
Barite | Level 11

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 --<-----
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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

Scott Barry

SBBWorks, Inc.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

dtchoi86
Fluorite | Level 6

Thank you all for the very helpful input.

This worked; thank you so much RW9. Smiley Happy

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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