Help using Base SAS procedures

Understanding the code for Appending several datasets

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Understanding the code for Appending several datasets

Hi,

I have a couple of questions in the following link...

http://support.sas.com/kb/48810

proc contents data=abc._all_ out=abccont(keep=memname) noprint;        /*what does the memname refer to*/

run;

ALSO

%macro combdsets;

%do i=1 %to &count;

proc append base=new data=abc.&&name&i force;        /*after running this macro step "NEW"  dataset is created which has all the datasets???*/

run;

%end;

%mend combdsets;

Thanks


Accepted Solutions
Solution
‎04-03-2013 04:33 PM
Super Contributor
Posts: 1,636

Re: Understanding the code for Appending several datasets

Hi,

try the code below.

libname abc "c:\temp"; /* change the directory if necessary. there should not be any other datasets in this dir. */
data abc.data1 abc.data2 abc.data3;
   set sashelp.class;
run;
proc contents data=abc._all_  out=abccont(keep=memname) noprint;run; /* memname is dataset name */
proc sort data=abccont nodupkey;
by memname;
run;

data _null_;
  set abccont end=last;
  call symputx(cats('name',_n_),memname);
  if last then call symputx('count',_n_);
run;
%put _user_; /* allow you to see the macro variables in log file */
%macro combdsets;
%do i=1 %to &count;
proc append base=new data=abc.&&name&i force;       /*after running this macro step "NEW"  dataset is created which has three datasets(data1,data2,data3) */
run;
%end;
%mend combdsets;
%combdsets

View solution in original post


All Replies
Solution
‎04-03-2013 04:33 PM
Super Contributor
Posts: 1,636

Re: Understanding the code for Appending several datasets

Hi,

try the code below.

libname abc "c:\temp"; /* change the directory if necessary. there should not be any other datasets in this dir. */
data abc.data1 abc.data2 abc.data3;
   set sashelp.class;
run;
proc contents data=abc._all_  out=abccont(keep=memname) noprint;run; /* memname is dataset name */
proc sort data=abccont nodupkey;
by memname;
run;

data _null_;
  set abccont end=last;
  call symputx(cats('name',_n_),memname);
  if last then call symputx('count',_n_);
run;
%put _user_; /* allow you to see the macro variables in log file */
%macro combdsets;
%do i=1 %to &count;
proc append base=new data=abc.&&name&i force;       /*after running this macro step "NEW"  dataset is created which has three datasets(data1,data2,data3) */
run;
%end;
%mend combdsets;
%combdsets

Super Contributor
Posts: 1,040

Re: Understanding the code for Appending several datasets

Hi Linlin..

In the below abcount is the dataset name...i still have questions about memname...

Could you explain a little bit

proc contents data=abc._all_  out=abccont(keep=memname) noprint;run; /* memname is dataset name */

proc sort data=abccont nodupkey;

by memname;

run;

Also if I am sure that there are no duplicates of datasets can I ignore the PROC CONTENTS and the PROC SORT?

Super User
Posts: 17,868

Re: Understanding the code for Appending several datasets

Run it without the keep= and then with the keep = to see what happens.

Super Contributor
Posts: 1,040

Re: Understanding the code for Appending several datasets

Hi,

Yes I did run..

So MEMNAME is a variable produced with the CONTENTS PRocedure which contains the names of the datasets??????

Regards

Super Contributor
Posts: 1,636

Re: Understanding the code for Appending several datasets

you have to use proc sort.

below is the output without proc sort:

libname abc "c:\temp"; /* change the directory if necessary. there should not be any other datasets in this dir. */

data abc.data1 abc.data2 abc.data3;

   set sashelp.class;

run;

proc contents data=abc._all_  out=abccont(keep=memname name) noprint;run; /* memname is dataset name */

proc print data=abccont;run;

                           Obs    MEMNAME    NAME

                               1     DATA1     Age

                               2     DATA1     Height

                               3     DATA1     Name

                               4     DATA1     Sex

                               5     DATA1     Weight

                               6     DATA2     Age

                               7     DATA2     Height

                               8     DATA2     Name

                               9     DATA2     Sex

                              10     DATA2     Weight

                              11     DATA3     Age

                              12     DATA3     Height

                              13     DATA3     Name

                              14     DATA3     Sex

                              15     DATA3     Weight

Super User
Posts: 17,868

Re: Understanding the code for Appending several datasets

That's what I get for not running the code Smiley Wink

This isn't the type of macro you need to append monthly datasets though. what you need then is something like the following:

%macro append_monthly(new_data=);

proc append base=old_data data=&new_data;

run;

%mend;

Then you call it each month with the new file. 

Super Contributor
Posts: 1,040

Re: Understanding the code for Appending several datasets

Hi,,

Until I append all the datasets present currently in the library  to one single dataset I need to go with

%macro combdsets;

%do i=1 %to &count;

proc append base=new data=abc.&&name&i force;

run;

%end;

%mend combdsets;

After I do that every month fresh data is appended by :

%macro append_monthly(new_data=);

proc append base=old_data data=&new_data;

run;

%mend;

IS IT RIGHT?????

Super Contributor
Posts: 1,040

Re: Understanding the code for Appending several datasets

Hi,

Is my understanding of this topic correct?

Regards

Super Contributor
Posts: 1,636

Re: Understanding the code for Appending several datasets

I think so.

make sure your "old_data" dataset is the same as your "new" dataset (the cumulative one you created by running your macro);

%macro combdsets;

%do i=1 %to &count;

proc append base=new data=abc.&&name&i force;

run;

%end;

%mend combdsets;

After I do that every month fresh data is appended by :

%macro append_monthly(new_data=);

proc append base=old_data data=&new_data;

run;

%mend;

Super User
Posts: 17,868

Re: Understanding the code for Appending several datasets

Read the doc or try it.

Base SAS(R) 9.2 Procedures Guide

Overview: APPEND Procedure

The APPEND procedure adds the observations from one SAS data set to the end of another SAS data set.

Super Contributor
Posts: 1,636

Re: Understanding the code for Appending several datasets

abccont is the dataset name just created. memname is a variable of dataset abccont, the values of memname are all the dataset names in the library.

Super Contributor
Posts: 1,040

Re: Understanding the code for Appending several datasets

One last question:

Is it compulsary that I run the contents and the SOrt????

What is the PROC CONTENTS role here in the code?????

All the datasets I have now  are got after running the same Macro

Regards

Super User
Posts: 17,868

Re: Understanding the code for Appending several datasets

Proc contents creates the list of datasets to append from the library. 

Test what you need in your circumstances by deleting all intermediary files created and then running sections and see what happens.

IMO the proc sort isn't needed, because SAS won't allow duplicate names in a library, but the remaining steps are.

☑ This topic is SOLVED.

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

Discussion stats
  • 13 replies
  • 391 views
  • 10 likes
  • 3 in conversation