Hey,
I have a macro that gets all the file names from a folder and stores them as macro variables. I'm trying to take the stored macro variables and put them into a one column data set, this is causing me a lot of trouble. I've tried writing a loop to do this but i keep getting an error. Any help would be much appreciated.
Given that your first macro is working, and given that you are willing to hard-code the number of files instead of automating that part, your loop should be fairly easy:
%macro newsets;
data dirlist;
length filename $ 80;
%local i;
%do i=1 %to 100;
filename = "&&file&i";
output;
%end;
run;
%mend newsets;
Of course, you can always turn the number of files, or the output data set name, into a macro parameter.
Good luck.
%let filecount=100;
*****************import the image filenames********************************************************;
%macro drive(dir,ext);
%let filrf=mydir;
/* Assigns the fileref of mydir to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
/* Returns the number of members in the directory */
%let memcnt=%sysfunc(dnum(&did));
/* Loops through entire directory */
%do i = 1 %to &memcnt;
/* Returns the extension from each file */
%global file&i.;
%let file&i. = %qsysfunc(dread(&did,&i));
%let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);
/* Checks to see if file contains an extension */
%if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then
%do;
/* Checks to see if the extension matches the parameter value */
/* If condition is true prints the full name to the log */
%if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or
(%superq(ext) = and %superq(name) ne) %then
%do;
%put %qsysfunc(dread(&did,&i));
%end;
%end;
%end;
/* Closes the directory */
%let rc=%sysfunc(dclose(&did));
%mend drive;
Here is my code right now. It's making the macro vars variable names and not observations.
%macro newsets();
data outloops;
%do i = 1 %to 100;
files = %substr(&&file&i.,1,10);
%end;
run;
%mend;
%newsets;
Jsendzik,
I faced a similar problem to you and created the below code to solve it. I had been using the pipe statement but wanted to have the dir file location directly inserted so I wouldn't have to write it out and this was the code that ended up working. The link to my question is it also includes some other options to do the same thing. Of note if you want to do anything with the file names you likely will need to use a %scan function, but that is for another time.
%let source = C:\User /*Insert your drive path*/;
%macro read_files (type = ); /*type is the extension of the files you want to keep, ie csv txt*/
filename _dir_ "%bquote(&source.)";
data filenames(keep=memname);
handle=dopen( '_dir_' );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
output filenames;
end;
end;
rc=dclose(handle);
run;
filename _dir_ clear;
data dirlist ;
set filenames;
if substr(memname,length(memname)-2,3) ~="&type" then delete;
file_name = substr(memname,1,length(memname)-4);
keep file_name;
run;
%mend;
Given that your first macro is working, and given that you are willing to hard-code the number of files instead of automating that part, your loop should be fairly easy:
%macro newsets;
data dirlist;
length filename $ 80;
%local i;
%do i=1 %to 100;
filename = "&&file&i";
output;
%end;
run;
%mend newsets;
Of course, you can always turn the number of files, or the output data set name, into a macro parameter.
Good luck.
Thanks!
Usually that's done in the other direction, read the list of files into a dataset and then store into macro variables.
Is there a particular reason you're trying to do it the other way?
Assuming your on windows the following will work, if you're on unix then post back.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.