BookmarkSubscribeRSS Feed
shubham1
Calcite | Level 5

hello

 

I have a library called new which has 50 datasets.one of the dataset name is class

 

dataset class has 5 variables and 10 observations

 

Similarly we have other datasets which have some variables and observations

 

 

Now we have another library called old which also has 50 datasets with same name that we have in library new

 

I want to to append both the library datasets since they have same dataset name and variables

 

once way to acheive is write proc append like this for all the daatsest 

 

proc append base=new.class data=old.class;

run;

 

But what we need to do when we have 200 libraries and each library contain 10 to 15 datasets

 

what is the code that I can write other than proc append to acheive this ?

2 REPLIES 2
Kurt_Bremser
Super User

First, create a dataset that contains the library and dataset names.

Then use call execute off that dataset to dynamically create data steps that create the target dataset:

proc sort data=datasets;
by dsname;
run;

data _null_;
set datasets;
by dsname;
if first.dsname then call execute("data &targetlib.."!!strip(dsname)"!!;set");
call execute(" "!!strip(dsname));
if last.dsname then call execute(";run;");
run;

 

Tom
Super User Tom
Super User

Is the goal to combine the approximately 200 times 15 datasets into 15 datasets?  Or did you have some other goal?  Do you have the list of datasets in a dataset you can use to generate the code you need? 200 is a lot of librefs to actively use.  Most likely once this consolidation is done you will not need to reference the individual datasets anymore.  Note that SAS syntax can actually reference the files used to store datasets directly without first creating a libref for each individual directory.  You just need to use the physical filename in quotes instead of the libref.memname syntax.

 

So first get your list of datasets into a single dataset.  It will probably look like this:

data files ;
  infile cards truncover;
  input filename $256. ;
cards;
/dir1/ds1.sas7bdat
/dir1/ds2.sas7bdat
/dir2/ds1.sas7bdat
/dir3/ds2.sas7bdat
/dir4/ds1.sas7bdat
/dir4/ds2.sas7bdat
;

You can probably use some operating system command, like DIR (windows), or FIND (unix) to get the list for you without typing them into in-line data in your program.

 

Then you probably want to add a variable that indicates the name of the target dataset.  So in the example above there are various combinations of 2 dataset names across 4 directories.  So perhaps you want to create two output datasets using those two distinct memnames.  So sort the data by the target name.

data files;
  set files;
  length memname $32 ;
  memname = scan(filename,-2,'./');
run;

proc sort data=files;
  by memname filename ;
run;

Then you can use that to generate code to combine the dataset.

filename code temp ;
data _null_;
  set files ;
  by memname ;
  file code lrecl=100;
  if first.memname then put 
    'data ' memname ';' 
  / '  set ' @ 
  ;
  put filename :$quote. @ ;
  if last.memname then put
    ';'
  / 'run;'
  ;
run;

Which will make these lines of code in the file CODE.

data ds1 ;
  set "/dir1/ds1.sas7bdat" "/dir2/ds1.sas7bdat" "/dir4/ds1.sas7bdat" ;
run;
data ds2 ;
  set "/dir1/ds2.sas7bdat" "/dir3/ds2.sas7bdat" "/dir4/ds2.sas7bdat" ;
run;

Which you can run by using %INCLUDE statement.

%include code / source2;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 427 views
  • 0 likes
  • 3 in conversation