I am trying to check if the dataset is already loaded to cas using this code:
%let data= 'MyDataSet';
proc cas;
table.tableExists result =r / name="&data" caslib="MyCasLib";
print(r);
if r=0 then do;
print("table is not loaded");
%let loaded = 0;
end;
else ;
print("Table already loaded");
quit;
The code works if I hard code the table name, but not if I try to use a macro value, and enter "MyDataSet" in stead. From the log, I can see that the macro variable is not resolved when I try to use the macro variable &data
And your next mistake is this
%let loaded = 0;
Macro statements are resolved while the code is fetched for interpretation/compilation, so this statement is resolved before PROC CAS runs, and &loaded will be zero in all cases.
You have to use the CASL functions SYMPUT or SYMPUTX to conditionally set a macro variable.
Everything after the %LET up to the semicolon (except leading and trailing blanks) is stored in the macro variable, so your statement resolves to
table.tableExists result =r / name="'MyDataSet'" caslib="MyCasLib";
which is not correct syntax. Always keep in mind that the macro processor is a text generating/replacing engine, and you do not need quotes around strings, unless you explicitly want them later (which is usually not the case).
And your next mistake is this
%let loaded = 0;
Macro statements are resolved while the code is fetched for interpretation/compilation, so this statement is resolved before PROC CAS runs, and &loaded will be zero in all cases.
You have to use the CASL functions SYMPUT or SYMPUTX to conditionally set a macro variable.
Thansks, I did not know about cas spesific symput statements and %let beeing resolved beforehand. Got it working using the casl symput
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.