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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
Jsendzik
Fluorite | Level 6

%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;

overmar
Obsidian | Level 7

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;

Astounding
PROC Star

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.

Jsendzik
Fluorite | Level 6

Thanks!

Reeza
Super User

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.

24820 - Creating a Directory Listing Using SAS for Windows

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 1168 views
  • 0 likes
  • 4 in conversation