From: fabergoogle on
dear all,

I have found this program to import several .txt files but it does
import just the last file from the directory appending it more times
(according to the number of files in the folder).

Can anybody help me and tell me where the code is making the error?

Thank you.

* Set up variables for the location of all the files and the final
output table ;
%let rootPath=C:\\My Documents\Internet\Web_Log_Files\ ;
%let output = work.results ;

* Define macro to read and append multiple files ;
%macro readData(rootPath=,file=,output=) ;
filename x "&rootPath\&file" ;
proc import datafile=x out=work._temp dbms=dlm replace ;
guessingrows=100 ; * This may need to be higher ;
getnames=no ;
datarow=6 ;
run ;
proc append base=&output data=work._temp ; run ;
%mend readData ;

* Delete the final output table ;
proc delete data=&output ; run ;

* Scan the location of files and call macro for each file ;
data _null_ ;
length msg name $ 200 ;
rc = filename('myfiles',"&rootPath") ;
if rc ne 0 then
do ;
put "ERROR: FILENAME myfiles could not be defined" ;
stop ;
end ;
put _all_ ;
did = dopen('myfiles') ;
if did <= 0 then
do ;
msg = sysmsg() ;
put msg ;
stop ;
end ;
memcount = dnum(did);
if memcount > 0 then
do i = 1 to memcount ;
name = dread(did,memcount) ;
put name= ;
call execute('%readData(rootpath=' !! "&rootPath,file=" !!
name !! ",output=&output);") ;
end ;
rc = dclose(did) ;
run ;
From: fabergoogle on
On Apr 15, 3:23 pm, fabergoogle <fabrizio_marr...(a)bradycorp.com>
wrote:
> dear all,
>
> I have found this program to import several .txt files but it does
> import just the last file from the directory appending it more times
> (according to the number of files in the folder).
>
> Can anybody help me and tell me where the code is making the error?
>
> Thank you.
>
> * Set up variables for the location of all the files and the final
> output table ;
>    %let rootPath=C:\\My Documents\Internet\Web_Log_Files\ ;
>    %let output = work.results ;
>
>    * Define macro to read and append multiple files ;
>    %macro readData(rootPath=,file=,output=) ;
>       filename x "&rootPath\&file" ;
>       proc import datafile=x out=work._temp dbms=tab replace ;
>          guessingrows=100 ; * This may need to be higher ;
>          getnames=no ;
>          datarow=6 ;
>       run ;
>       proc append base=&output data=work._temp ; run ;
>    %mend readData ;
>
>    * Delete the final output table ;
>    proc delete data=&output ; run ;
>
>    * Scan the location of files and call macro for each file ;
>    data _null_ ;
>       length msg name $ 200 ;
>       rc = filename('myfiles',"&rootPath") ;
>       if rc ne 0 then
>          do ;
>             put "ERROR: FILENAME myfiles could not be defined" ;
>             stop ;
>          end ;
>             put _all_ ;
>       did = dopen('myfiles') ;
>       if did <= 0 then
>          do ;
>             msg = sysmsg() ;
>             put msg ;
>             stop ;
>          end ;
>       memcount = dnum(did);
>       if memcount > 0 then
>          do i = 1 to memcount ;
>             name = dread(did,memcount) ;
>             put name= ;
>             call execute('%readData(rootpath=' !! "&rootPath,file=" !!
> name !! ",output=&output);") ;
>          end ;
>       rc = dclose(did) ;
>    run ;


Sorry the txt files are tab limited. In the example I forgot to change
the DBMS but I did it in SAS.