4
votes

I'm trying to organize my SAS code by defining a macro variable called root. Then, I want to be able to have all of my %INCLUDE statements use the &root value so I can just define the %INCLUDE in terms of the root:

%LET root = C:\Documents and Settings\me\Desktop\mine\SAS;
%include "&root\lib\work.sas"

However, when trying to run this under SAS 9.2 I get the following error from the log:

1    %LET root = C:\Documents and Settings\me\Desktop\mine\SAS;
ERROR: Incorrect %INCLUDE statement will not be executed. There is a syntax error.
2    %include "&root\lib\work.sas"

So it looks likes the &root variable isn't being expanded into its value in the %INCLUDE statement. What am I doing wrong?

Thanks!

[Edit] Answer

I was missing the ';' at the end of the %INCLUDE statement. =/

3
Thanks again to everyone! I'm really happy to see such an active and helpful SAS community here. The problem was, as @Joe pointed out, the missing semi-colon at the end of the INCLUDE (yep, SAS newb here).Matt Klein

3 Answers

4
votes

In that situation you probably need to use the separator, that marks the end of a macro variable name, which is ".", so your code will look like:

%LET root = C:\Documents and Settings\me\Desktop\mine\SAS;
%include "&root.\lib\work.sas"

This is actually needed only when variable name "touches" something, but I would recommend using it ALWAYS - it's a good practise. Also when there is a dot after the variable name you should put "that" dot, so there will be two dots, like:

%LET root = C:\Documents and Settings\me\Desktop\mine\SAS;
%LET fname = work;
%include "&root.\lib\&fname..sas"

EDIT

As @Joe correctly states in other answer, the real problem in that situation is a lack of semicolon after %INCLUDE statement. Placing it after the path should solve the problem.

3
votes

I have to say i'm a bit confused by your presentation of the error, plus your accepted answer. In my experience, the ERROR: Incorrect %INCLUDE statement would show up after the second line, not before. Plus, if the macro variable resolution is the issue, then the following is what you'd see:

903  %LET root = C:\temp;
904
905  %include "&roots\test.sas";
WARNING: Apparent symbolic reference ROOTS not resolved.
WARNING: Physical file does not exist, C:\Users\xxxx\&roots\test.sas.
ERROR: Cannot open %INCLUDE file &roots\test.sas.

In your included code, there is no semicolon after the %include. Is that possibly related to the problem? When I run the %include then another line that is illegal, for example running them in the opposite order, I do get that error (still after the %include, though).

2
votes

I suspect you have another bug somewhere prior to this statement in your code. Try restarting SAS and executing just those two lines of code and it should work fine, it worked just fine for me:

%let root = c:\documents and settings\robert.penridge\desktop\mine\sas;
%include "&root\lib\work.sas";

EDIT: This worked for me because I actually added a semicolon. Realised this after @Joe's correct diagnosis of the problem.

I created the following folder structure on the desktop:

\mine\sas\lib

And then put work.sas in there with the following contents:

%put blah;

When I ran the code the results printed blah just fine with no error messages. In fact I have used code just like this across many versions of SAS and across many operating systems for many years. There's no issue with your code.

Contrary to what some of the other answers suggest here, SAS is smart enough to know that the \ character can not be part of a macro name so there's no need to explicitly state the end of the macro with the . character.

Finally, if you are trying to debug macro code you can also turn on the following options:

option mprint mlogic macrogen symbolgen source source2;

You can turn them back off like so:

option nomprint nomlogic nomacrogen nosymbolgen nosource nosource2;

Just be aware that some of these may already be on by default in your SAS environment.

In rare circumstances you may also be running into a macro quoting issue. This will happen if you have been using macro functions like %str() %bquote() etc... If this is the case then it can often be resolved by using the %unquote() function around the macro variable that is causing issues. From your code sample it doesn't look like this is the issue but you might have simplified your code for the post. If this were the case then in your situation the code would be changed to look like this:

%let root = c:\documents and settings\robert.penridge\desktop\mine\sas;
%include "%unquote(&root)\lib\work.sas";