SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
FP12
Obsidian | Level 7

Hi,

 

I have this macro, which I run in a loop:

%macro union(
	input_table,
	output_table,
	loop);

%if &loop. = 1 %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;

It works well. But I am sure there is better practice to avoid using loop value.

Is there a way for the Proc APPEND to detect if the output_table exists, and if not to create it naturally with the input table?

 

Thanks

2 REPLIES 2
Kurt_Bremser
Super User

You can use proc append with a non-existing base table without problems:

data have;
x1 = 1;
run;

proc append
  data=have
  base=out
;
run;

Log:

24         data have;
25         x1 = 1;
26         run;

NOTE: The data set WORK.HAVE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

27         
28         proc append
29           data=have
30           base=out
31         ;
32         run;

NOTE: Appending WORK.HAVE to WORK.OUT.
NOTE: BASE data set does not exist. DATA file is being copied to BASE file.
NOTE: There were 1 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.OUT has 1 observations and 1 variables.
NOTE: PROCEDURE APPEND used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The question is, why you have so many related datasets that need appending, such that you need a macro to do this.  I would suggest you look into By Group processing, which is a core SAS feature used to do operations on groups of data, as I suspect your running procedures of blocks of data individually yourself, and then setting them back together again at the end.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2 replies
  • 2890 views
  • 0 likes
  • 3 in conversation