BookmarkSubscribeRSS Feed
teja04
Calcite | Level 5
Hi All,

I have situation, In ALL_DATASETS folder I have three .sas7bdat files (AA, BB, CC) and I have to read all the files and join datasets from that folder. In the future, I may get one additional dataset (DD) or may not. But, the code should support both the scenarios. could someone help me in that.
Example: I have datasets in path " SAS/sas1/sas_sub1/all_datasets".
In that folder, I have
AA.sas7bdat
BB.sas7bdat
CC.sas7bdat
The dataset expecting will be named as DD.sas7bdat.
9 REPLIES 9
teja04
Calcite | Level 5
Hi All,

I have situation, In ALL_DATASETS folder I have three .sas7bdat files (AA, BB, CC) and I have to read all the files and join datasets from that folder. In the future, I may get one additional dataset (DD) or not. But, the code should support both the scenarios. could someone help me in that.
Example: I have datasets in path " SAS/sas1/sas_sub1/all_datasets".
In that folder, I have
AA.sas7bdat
BB.sas7bdat
CC.sas7bdat
The dataset expecting will be named as DD.sas7bdat.
ChrisNZ
Tourmaline | Level 20

Like this?

libname Z '/SAS/sas1/sas_sub1/all_datasets';
data WANT;
  set Z.AA Z.BB Z.CC %sysfunc(ifc(exist(Z.DD),Z.DD,));
run; 

 

Anthony45
Fluorite | Level 6

Defining a SAS libref that points to the "SAS/sas1/sas_sub1/all_datasets" path could be an option. You can then simply refer to the datasets by their names using a libname. More info on the libname statement here

 

Something like this:

 

 

* Define the libname (Note that this statement does not contain an equal sign);
libname my_libname "SAS/sas1/sas_sub1/all_datasets";

* Create a new dataset called 'all_sets' that stacks the 'AA', 'BB', and 'CC' SAS datasets;
data all_sets;
   set my_libname.AA my_libname.BB my_libname.CC;
run;

 

 

PeterClemmensen
Tourmaline | Level 20

You have to be more specific than "Join datasets from that folder". What kind of join?

teja04
Calcite | Level 5
Hi draycut,

I want to append the datasets
ballardw
Super User

The first thing would be to assign a LIBRARY to point to the folder location.

Your example path looks incomplete you should add the bits at the front so that the path starts at a disc or mount point point but generally something like:

 

libname mydata 'SAS/sas1/sas_sub1/all_datasets';

 

Check the LOG for messages. If you do not see a successfully assigned result then the path needs to be modified as mentions.

 

The append can be done with that procedure assuming you have similar variables.

 

Proc append base=mydata.Alldata

                     data=mydata.AA;

run;

Proc append base=mydata.Alldata

                     data=mydata.BB;

run;

Note how the libname is used to address the data sets.

Later to add other sets you just change the name of the set on the DATA= line to add it to the AllData set.

The first time Alldata does not exist so the procedure creates it using the contents of mydata.AA to determine the number and types of variables.

 

If the sets to do not have the same variables this won't work because append will only add matching variables.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

 

%let mystr =AA BB CC;

 


data want;
 set &mystr.;
run;

 

 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

here is another way but you get all SAS datasets so beware

 

libname myin "SAS/sas1/sas_sub1/all_datasets";
proc sql ;		
	select 		
		trim(libname) || '.' || memname into :dataset_vars separated by ' '
	from dictionary.tables
	where upcase(libname)="MYIN"
	and upcase(memname) like '%' 
	;
	
quit;

data myin.want;
	set &dataset_vars;
run;

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
  • 9 replies
  • 4804 views
  • 2 likes
  • 6 in conversation