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

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

1 ACCEPTED SOLUTION

Accepted Solutions
5 REPLIES 5
Astounding
PROC Star

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;

MUABer
Fluorite | Level 6

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?

 

 

MUABer
Fluorite | Level 6

That was a great help!

Thank you!

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 797 views
  • 0 likes
  • 4 in conversation