DATA Step, Macro, Functions and more

macro to create data sets from txt files in multiple folders.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

macro to create data sets from txt files in multiple folders.

I am using the macro at the link below.

http://support.sas.com/kb/45/805.html

It runs fine as-is, but I need to put an infile statement in it to output SAS data sets for each txt file found.

 I do not seem to be able to get that to run. Below is macro with my infile. I have tried many ways, but must be making some basic mistake. Please help. Thanks.

 

%macro drive(dir,ext);                                                                                                                  
  %local filrf rc did memcnt name i;                                                                                                    
                                                                                                                                        
  /* Assigns a fileref to the directory and opens the directory */                                                           
  %let rc=%sysfunc(filename(filrf,&dir));                                                                                               
  %let did=%sysfunc(dopen(&filrf));                                                                                                     
                                                                                                                                        
  /* Make sure directory can be open */                                                                                                 
  %if &did eq 0 %then %do;                                                                                                              
   %put Directory &dir cannot be open or does not exist;                                                                                
   %return;                                                                                                                             
  %end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
   %do i = 1 %to %sysfunc(dnum(&did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
     %let name=%qsysfunc(dread(&did,&i));

%let x = %sysfunc(tranwrd(&name,.,_));
%let dt = %sysfunc(compress(%superQ(x),%str(%(%))));

     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
      %put &dir\&name;
%put &dt;
*%put &x;
%put &i;

data &dt.&i;
            *set &name;*(keep=path filename txtcount txtc ID);
    *if line ^= "" then do;* change these numbers as needed with do loop;
    *fulldt=trim(left(path))||"\"||trim(left(filename));
    *length line $ 100;
      *informat line $char100.;* change to longer line if needed (was $char155.) *;
infile "&dir\&name" truncover;
      *infile in filevar=fulldt end=done truncover firstobs = 28;
        *do until(done);
      *input line & 1-100;
input line $ 1-100;
     *output;
       *end;
    *end;
*proc print data=&dt.&i(obs=4);title2 "&dt.&i";
run; title2;

      %end;                                                                                                                             
     /* If directory name call macro again */                                                                                           
     %else %if %qscan(&name,2,.) = %then %do;                                                                                          
        %drive(&dir\%unquote(&name),&ext)                                                                                               
      %end;                                                                                                                             
                                                                                                                                  
   %end;                                                                                                                                
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
%mend drive;                                                                                                                            
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%drive(C:\folder,txt) ;


Accepted Solutions
Solution
‎02-01-2018 03:15 PM
Occasional Contributor
Posts: 12

Re: macro to create data sets from txt files in multiple folders.

That was a great help!

Thank you!

View solution in original post


All Replies
Super User
Posts: 6,543

Re: macro to create data sets from txt files in multiple folders.

You're looking at vital information that you didn't share.  For example ...

 

Did the %PUT statements write out anything at all?

 

If so, was it correct?

 

Did you get an error message?  

 

If so, what was it?  Was it a macro error vs. a SAS error? 

 

Did you make any changes to the code before the %DO loop begins? 

 

Do you have a log where you turned on key debugging options:

 

options symbolgen mprint mlogic;

Occasional Contributor
Posts: 12

Re: macro to create data sets from txt files in multiple folders.

Posted in reply to Astounding

The %Put does correctly write all the path/files to the log. No ERRORs were generated.

If I use %Include all the files data is written to the output window, but the format is not usable. Sometimes data lines are one line per row and some times it may be multiple lines of data per row.

The data has some header lines and then the data itself is rows of comma delimited numbers.

Maybe a %symput can be used to write all the path/files to a data set?

 

 

Super User
Posts: 22,874

Re: macro to create data sets from txt files in multiple folders.

Solution
‎02-01-2018 03:15 PM
Occasional Contributor
Posts: 12

Re: macro to create data sets from txt files in multiple folders.

That was a great help!

Thank you!

Super User
Posts: 13,084

Re: macro to create data sets from txt files in multiple folders.

[ Edited ]

Did you run your code with

Options mprint symbolgen mlogic; 

which are basic debugging tools for code involving macros to show you in detail what the macro language is doing?

 

Second:

Do not use the inline comment style such as on this line

*set &name;*(keep=path filename txtcount txtc ID);

inside macros.

Single line comments in the macro language are

%*set &name;   %*(keep=path filename txtcount txtc ID);

 

Or

/*set &name; */   /*(keep=path filename txtcount txtc ID);*/

 

Personally I only use /* */ style comments so I don't have to worry about the differences

AND the editor will comment / uncomment entire lines with the ctrl-/ and shift-ctrl-/ key strokes.

☑ This topic is solved.

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

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