DATA Step, Macro, Functions and more

exist function

Posts: 45

exist function



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:




Then (still inside the loop) I call another macro



		input_table = &ext_output_table._tmp.,
		output_table = &ext_output_table.);


The union macro is defined like this:

%macro union(
%if NOT(%sysfunc(exist(WORK.&output_table.)))  %then %do;
	data &output_table.; set &input_table.; run;
%else %do;
	PROC APPEND BASE= &output_table. DATA= &input_table. force;
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?




Super User
Posts: 10,280

Re: exist function

[ Edited ]

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:


How do you use that text?


Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: exist function

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;');

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.

Super User
Posts: 6,785

Re: exist function

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


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



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

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation