DATA Step, Macro, Functions and more

Stored Macro vars into a dataset

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Stored Macro vars into a dataset

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.


Accepted Solutions
Solution
‎01-14-2014 11:34 AM
Super User
Posts: 5,082

Re: Stored Macro vars into a dataset

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


All Replies
Contributor
Posts: 35

Re: Stored Macro vars into a dataset

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

Frequent Contributor
Posts: 83

Re: Stored Macro vars into a dataset

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;

Solution
‎01-14-2014 11:34 AM
Super User
Posts: 5,082

Re: Stored Macro vars into a dataset

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.

Contributor
Posts: 35

Re: Stored Macro vars into a dataset

Thanks!

Super User
Posts: 17,828

Re: Stored Macro vars into a dataset

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 295 views
  • 0 likes
  • 4 in conversation