SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ullsokk
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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

Kurt_Bremser
Super User

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.

Ullsokk
Pyrite | Level 9

Thansks, I did not know about cas spesific symput statements and %let beeing resolved beforehand. Got it working using the casl symput 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1784 views
  • 1 like
  • 2 in conversation