DATA Step, Macro, Functions and more

exist function

Reply
Contributor
Posts: 45

exist function

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

 

Super User
Posts: 6,971

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:

&ext_output_table._tmp

How do you use that text?

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,430

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;');
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: 5,097

Re: exist function

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.

Ask a Question
Discussion stats
  • 3 replies
  • 313 views
  • 6 likes
  • 4 in conversation