BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

13 REPLIES 13
Linlin
Lapis Lazuli | Level 10

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

robertrao
Quartz | Level 8

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?

Reeza
Super User

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

robertrao
Quartz | Level 8

Hi,

Yes I did run..

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

Regards

Linlin
Lapis Lazuli | Level 10

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

Reeza
Super User

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. 

robertrao
Quartz | Level 8

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?????

robertrao
Quartz | Level 8

Hi,

Is my understanding of this topic correct?

Regards

Linlin
Lapis Lazuli | Level 10

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;

Reeza
Super User

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.

Linlin
Lapis Lazuli | Level 10

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.

robertrao
Quartz | Level 8

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

Reeza
Super User

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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 13 replies
  • 1426 views
  • 10 likes
  • 3 in conversation