DATA Step, Macro, Functions and more

How to append datasets when the dataset names are in series

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

How to append datasets when the dataset names are in series

Hi all,

 

My requirement is to append the summary datasets named data_var1, data_var2, data_var3.....

here var1, var2, var3 are unique set of variables . I need to append all the datasets into a single.

I tried using proc append, following is the code. There are warnings saying that the variables dont exist in base dataset, for this I have used force option. But the output dataset is empty.

 

%macro Append_summary(in_ds,count,var);
   %do i=1 %to &&&count.;
      proc append base= summary_stats data=&in_ds._&&&var&i force; run;
   %end;
%mend Append_summary;

%Append_summary(summary,var_count,var_name);

var_count and var_name are global macro variables.




Please help me resolve this.

 

Thanks


Accepted Solutions
Solution
‎12-14-2017 12:35 PM
Super User
Super User
Posts: 9,840

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

If that is indeed your data, and they are the same:

data want;
  set data_var:;
run;

Note the colon, means any dataset with the prefix of data_var.

 

Secondly why do you have: data_var1, data_var2, data_var3

In the first place.  This seems to be a common issue, when doing a procedure over a variable or group, the tendancy is to push out a dataset for each loop.  SAS is built on something called by group processing.  Get your data into one dataset, with a variable as the group (i.e. what you would split the data out to), then use by in your statement.  Doing it this way will make your code smaller, more robust, and produce the one dataset output you want at the end.  For instance:

proc means data=have;
  by mygroup;
  ...;
  output out=want;
run;

This will produce one dataset, by the mygroup variable - you can apply this logic to anything in SAS, sometimes a change to your data is needed.

View solution in original post


All Replies
Super User
Posts: 10,574

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

Post the complete log of the macro, please.

And inspect your data, there may be nothing in there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎12-14-2017 12:35 PM
Super User
Super User
Posts: 9,840

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

If that is indeed your data, and they are the same:

data want;
  set data_var:;
run;

Note the colon, means any dataset with the prefix of data_var.

 

Secondly why do you have: data_var1, data_var2, data_var3

In the first place.  This seems to be a common issue, when doing a procedure over a variable or group, the tendancy is to push out a dataset for each loop.  SAS is built on something called by group processing.  Get your data into one dataset, with a variable as the group (i.e. what you would split the data out to), then use by in your statement.  Doing it this way will make your code smaller, more robust, and produce the one dataset output you want at the end.  For instance:

proc means data=have;
  by mygroup;
  ...;
  output out=want;
run;

This will produce one dataset, by the mygroup variable - you can apply this logic to anything in SAS, sometimes a change to your data is needed.

Contributor
Posts: 49

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

@UshaLatha .. if your id data set and data is like this , please try the below code

 

data data_var1;
input a b c;
cards;
1 2 3
4 5 6
;
data data_var2;
input d e f;
cards;
7 8 9
10 11 12
;

data data_var3;
input g h i;
cards;
13 14 15
;

 

data append_var;
set data_var:;
run;

Trusted Advisor
Posts: 1,848

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

Is summary is a name of library ?

If positive then you missed a dot:

%macro Append_summary(in_ds,count,var);
   %do i=1 %to &&&count.;
      proc append base= summary_stats data=&in_ds.._&&&var&i force; run;
   %end;
%mend Append_summary;

 

Super User
Posts: 13,941

Re: How to append datasets when the dataset names are in series

Posted in reply to UshaLatha

The documentation on Proc Append is pretty clear that you cannot add new variables in the append.

 

You either have to make your base data set will all of the variables you will ever have before appending any data or use data step code with multiple data sets on the set statement.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 142 views
  • 0 likes
  • 6 in conversation