BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
blaubner
Calcite | Level 5

Be kind. First time posting...

 

I used...

 

data fakeset;
length fref $8 fname $200;
did = filename(fref,'Fake\Path');
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname;
run;

 

...to generate 'fakeset' in my work folder with files from the Fake\Path directory. 

 

All of the files in Fake\Path are in .sas7bdat format. 'fakeset' looks like this...

 

fname

fake1.sas7bdat

fake2.sas7bdat

fake3.sas7bdat

 

How do I import each file in Fake\Path using the fname I generated in 'fakeset'? I'd like all of the files imported, then set into one new data set in my work folder. 

I'm going to continue searching for a soltution as I know this has been done before (I'm sure). Thanks for the help

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

To be technically precise, you cannot import files that have names ending with .sas7bdat, because they are already SAS data sets.

 

So all you need is a LIBNAME statement to make the files available to SAS.

 

libname mylib 'Fake\Path';

From there, you can combine all of them together using the SET statement in a SAS data step.

 

data want;
    set mylib.fake1 mylib.fake2 mylib.fake3;
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

To be technically precise, you cannot import files that have names ending with .sas7bdat, because they are already SAS data sets.

 

So all you need is a LIBNAME statement to make the files available to SAS.

 

libname mylib 'Fake\Path';

From there, you can combine all of them together using the SET statement in a SAS data step.

 

data want;
    set mylib.fake1 mylib.fake2 mylib.fake3;
run;
--
Paige Miller
blaubner
Calcite | Level 5

Sorry, I wasn't specific enough.

I don't want to designate a library and hard code the actual file names. I want a procedure to be automated. When the program is executed, it would...

 

1. collect the filenames from a directory and put them into work.fakeset under the variable 'fname' (I've achieved this)

2. The program then brings in all the files from the directory using each 'fname' into one new data set called work.alldata

 

Does that make sense?

Kurt_Bremser
Super User

@blaubner wrote:

Sorry, I wasn't specific enough.

I don't want to designate a library and hard code the actual file names. I want a procedure to be automated. When the program is executed, it would...

 

1. collect the filenames from a directory and put them into work.fakeset under the variable 'fname' (I've achieved this)

2. The program then brings in all the files from the directory using each 'fname' into one new data set called work.alldata

 

Does that make sense?


All you need to do is

  • define a LIBNAME for the directory
  • use all dataset names (without the .sas7bdat extension) in a SET statement

The list of names can be retrieved from the dataset you created from the directory.

blaubner
Calcite | Level 5

Sorry I'm not following. If I add a libname to my path, what is the next procedure to read in only the files with a filename from work.fakeset? I don't want to hard code the names myself. I want SAS to do this procedure automatically. I'm assuming a macro needs to be established? I'm not familiar with macros yet.

 

I don't want to do this...

 

data fakeset;
length fref $8 fname $200;
did = filename(fref,'Fake\Path');
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname;
run;

libname want 'Fake\Path';

data notwhatiwant;
set want.fake1 want.fake2 want.fake3;
run;

  

PaigeMiller
Diamond | Level 26

@blaubner wrote:

Sorry, I wasn't specific enough.

I don't want to designate a library and hard code the actual file names. I want a procedure to be automated. When the program is executed, it would...

 

1. collect the filenames from a directory and put them into work.fakeset under the variable 'fname' (I've achieved this)

2. The program then brings in all the files from the directory using each 'fname' into one new data set called work.alldata

 

Does that make sense?


So, let's assume you know the folder name but you don't know the names of the SAS files in the folder ... is that a valid assumption?

 

If so

 

libname mylib 'Fake\Path';
proc sql noprint;
     select distinct cats('mylib.',memname) into :tablenames separated by ' '
        from sashelp.vtable where libname='mylib';
quit;
data want;
     set &tablenames;
quit;

 

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2659 views
  • 2 likes
  • 3 in conversation