BookmarkSubscribeRSS Feed

Sample Dynamic File Scan

Started ‎07-10-2014 by
Modified ‎10-05-2015 by
Views 634

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

            run;

 

            data a_all;

                %if &i = 1 %then %do;

                set a;

                %end;

                %else %do;

                set a_all a;

                %end;

            run;

              

            %end;

        %end;

    %end;

 

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

 

%mend;                                                                               

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

 

Version history
Last update:
‎10-05-2015 03:45 PM
Updated by:

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags