Using LET variable in INCLUDE command in SAS?

I am trying to organize my SAS code by setting a macro called root. Then I want all my %INCLUDE use &root , so I can just define %INCLUDE in terms of root:

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

However, when I try 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 like the &root variable is not expanding into its value in the %INCLUDE . What am I doing wrong?

Thanks!

[Edit] Reply

I lost ';' at the end of the %INCLUDE . = /

+4
source share
3 answers

I have to say that I'm a little confused by your presentation of the error, plus your accepted answer. In my experience, ERROR: Incorrect %INCLUDE statement will appear after the second line, and not earlier. Also, if the problem with macro resolution is a problem, then you will see the following:

 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. 

There is no semicolon in your included code after including%. Perhaps this is due to a problem? When I run% include, then another line that is illegal, for example, starts them in the opposite order, I get this error (still after including% include).

+3
source

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

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

This is really only necessary when the variable name “touches” something, but I would recommend using it ALWAYS - this is good practice. Also, when there is a dot after the variable name, you must put "this" dot, so there will be two periods, for example:

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

EDIT

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

+4
source

I suspect you have another error somewhere before this statement in your code. Try restarting SAS and executing only these two lines of code, and it should work fine, it worked 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. I realized this after @Joe correctly diagnosed the problem.

I created the following folder structure on the desktop:

 \mine\sas\lib 

Then add work.sas with the following content:

 %put blah; 

When I ran the code, the results printed blah just fine, with no error messages. In fact, I used the code in the same way as it has in many versions of SAS and in many operating systems for many years. No problem with your code.

Unlike some other answers, SAS is smart enough to know that the \ character cannot be part of the macro name, so there is no need to explicitly indicate the end of the macro with . .

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

 option mprint mlogic macrogen symbolgen source source2; 

You can disable them like this:

 option nomprint nomlogic nomacrogen nosymbolgen nosource nosource2; 

Just keep in mind that some of them may already be enabled by default in your SAS environment.

In rare cases, you may also encounter a macro problem. This will happen if you use macro functions like %str() %bquote() , etc. If so, this can often be resolved with the %unquote() function around the macro variable causing the problem. From your sample code, this doesn't seem like a problem, but you could simplify your code for the message. If so, then in your situation the code will be changed to look like this:

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

Source: https://habr.com/ru/post/1434899/


All Articles