Good day,
I ran into following situation where macro variables are defined as global depending if they are from include or not.... Variables defined in macros should not be visible outside of the macro, right. This fails:
%macro if_env;
%let a=100;
%mend if_env;
%if_env;
%put &a.;
WARNING: Apparent symbolic reference A not resolved.
This is as expected. However, I ran into an issue, where the variable bleeds to global space:
I have an include file: C:\TEMP\test.sas, which contains only setting for variables a b c d: (For testing purposes)
%let a=100;
%let B=200;
%let C=300;
%let D=400;
This works also as intended:
%macro if_env;
%include"C:\TEMP\test.sas";
%mend if_env;
%if_env;
%put &B.;
WARNING: Apparent symbolic reference B not resolved.
All good so far. Now, I added if clause 'include if file exists'-condition:
%macro if_env;
%if %sysfunc(fileexist(C:\TEMP\test.sas)) %then
%let C=100
%mend if_env;
%if_env;
%put &C.;
WARNING: Apparent symbolic reference C not resolved.
Last step: Add the actual include to the code:
%macro if_env;
%if %sysfunc(fileexist(C:\TEMP\test.sas)) %then
%include"C:\TEMP\test.sas";
%mend if_env;
%if_env;
%put &A.;
%put &B.;
%put &C.;
%put &D.;
100 200 300 400
Umm, not computing. This should not happen. How could %let-command have different namespace depending if it is in an include or not?
Any idea why this happens as it does? Bug or really fancy feature?
Edit: Very interesting. Based on SAS documentation one should use semicolon at the end of include. Glad to have clarification on the matter. Thanks for the answers.
%include
stores the macro variables to the global environment. Have you tried out @Robert suggestion? – samkart