We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Sample Dynamic File Scan

by Regular Learner Anind_Sharma on ‎07-10-2014 03:00 AM - edited on ‎10-05-2015 03:45 PM by Community Manager (176 Views)

%let filepath=C://;* Filepath to pick up the files;


%macro extract(dir=, ext=);


    %let filrf=mydir;

    %let rc=%sysfunc(filename(filrf,&dir));

    %let did=%sysfunc(dopen(&filrf));

    %let memcnt=%sysfunc(dnum(&did));


    %do i = 1 %to &memcnt;

        %let memname = %qsysfunc(dread(&did, &i));

        %let fid = %sysfunc(mopen(&did, &memname));


        %let extname=%qscan(%qsysfunc(dread(&did,&i)),-1,.);    /* Returns the extension from each file */


        /* Returns the file name portion */

        %let name1=%qscan(%qsysfunc(dread(&did,&i)),1,.);       /* filename with yymmdd */

        %let name2=%substr(&name1,1,(%length(&name1)-6));       /* filename without yymmdd */


        /* Checks to see if file contains an extension */

        %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&ext) %then %do;

                                                                                %if (%qupcase(&extname) eq %qupcase(&ext)) and ("&name1" ne "") %then %do;             


            /* if condition TRUE then execute SAS statement......*/


            %put user;

            /* export filenames in dataset */

            data a;

            format file $20. file2 $20.;

            no = &i.;

            file = "&name1.";

            file2 = "&memname.";



            data a_all;

                %if &i = 1 %then %do;

                set a;


                %else %do;

                set a_all a;








    %let rc=%sysfunc(dclose(&did));    



%extract(dir=&filepath, ext=/file extension/)


Your turn
Sign In!

Want to write an article? Sign in with your profile.

Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.