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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2220 views
  • 0 likes
  • 3 in conversation