BookmarkSubscribeRSS Feed
Planck
Obsidian | Level 7

Hi,

 

I try to check if a table exist and I can't manage this, can you help?

I call a macro A:

 

%A (
ext_output_table = GG
);

In this macro, there is a loop. For each item I create a temporary table:

 

 

&ext_output_table._tmp

Then (still inside the loop) I call another macro

 

 

	%union(
		input_table = &ext_output_table._tmp.,
		output_table = &ext_output_table.);

 

The union macro is defined like this:

%macro union(
	input_table,
	output_table);
%if NOT(%sysfunc(exist(WORK.&output_table.)))  %then %do;
	data &output_table.; set &input_table.; run;
%end;
%else %do;
	PROC APPEND BASE= &output_table. DATA= &input_table. force;
%end;
PROC DELETE DATA= &input_table.;
%mend union;

The purpose of this is to create the table as equal to the temporary table if it is not existing otherwise just append the current version of the table by adding the rows of the temporary table.

 

In the log I receive the opposite answer than the one I should,  

NOT(%sysfunc(exist(WORK.&output_table.))) IS FALSE

 

For the first item of my loop, it should be true.

Do you know what could go wrong here?

 

Thanks

 

3 REPLIES 3
Kurt_Bremser
Super User

Firstly, you should post your code as is, and not mostly unrelated snippets. It is very hard to make sense of all of this.

This got my attention:

&ext_output_table._tmp

How do you use that text?

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, a mess of snippets and macro code undefined is not a good example.  If you want to append a dataset if it exist, then as always, keep it simple:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="UPDATE"));
  call execute('proc append base=total data=update; run;');
run;

The above will only generate the proc append if a dataset update exists in work.  

Although a second question, why do you not know if a dataset exists or not?  There is only one instance (from a proc freq perhaps) where no output dataset is created, so sounds like you have issue before in yur logic.

Astounding
PROC Star

PROC APPEND does exactly what you would like without any fancy macros:

 

proc append base=&out_table. data=&input_table. force;

run;

 

It's perfectly OK if the BASE= data set does not exist.  PROC APPEND then copies the DATA= data set to the BASE= location.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5083 views
  • 6 likes
  • 4 in conversation