Sunday, February 26, 2012

If you have Stata database file (. DTA) in large numbers and like to process automatically using the do file then we can use the joint command in Stata and windows batch like this (example below is used to eliminate a variable from all of files existing databases)

cd C: \ DATA \ FILES \

The above command is used to open the directory where your database files are stored

! Dir *. DTA / ad / b> C: \ DATA \ FILES \ dtalist.txt, read

The next command is a command "dir" batch windows are run in Stata with the prefix "!" used to list all file names with Stata format "*. DTA" contained in the directory where your database files are located.

The entire file name will then be stored in a list with names dtalist.txt file, list the name of this file will be used as a list of files to be processed by the Stata

file open myfile using C: \ DATA \ FILES \ dtalist.txt, read

Stata will open a list of file names "dtalist.txt" using the command as above, this command just use the status read only

Stata will use "myfile" for the temporary storage of the file list for the next dtalist.txt

file read myfile line

The above command is used to read a list of file names "myfile" on the line

while r (eof) == 0 {

The following command is used to read a list of file names myfile it line by line (in the sense that if eof (end of file) "= 0" is not found then Stata will continue to process orders until end of file (eof))

use `line '

This command is used to using the `line 'lines from the list as the name of the file myfile (note the punctuation' and 'Stata using punctuation as a marker that the word` line' is used as the file name

capture confirm variable nom1

Capture confirm the above command is used to find whether there is a variable "nom1" in the file are in the process

if _rc == 0 {

command "if" above is a command if Stata not found error (in this case if Stata find nom1 variable) then the next command will be activated

drop nom1

Command "drop" to drop nom1 variable and will be removing the nom1 on file in the list of myfile that is currently in the process

save `line ', replace

The above command is used to store the results of the drop nom1 the file is in the process by copying the existing files so that the file has not had a variable nom1 again when in the open

file read myfile line
}
Commands is used to read the next line of the file name from the list of names contained in myfile to the process again

if _rc == 111 {

If from the above command is a command if Stata finds an error (in this case did not find any Stata nom1 variable) then the if command below will be activated

file read myfile line

}
}

Command file read myfile line above is used to read the next line of the file name from the list of names contained in myfile to the process again

combined total in the stacking order can be as below

 cd C: \ DATA \ FILES \

! Dir *. DTA / ad / b> C: \ DATA \ FILES \ dtalist.txt, read

file open myfile using C: \ DATA \ FILES \ dtalist.txt, read

file read myfile line

while r (eof) == 0 {

use `line ', clear

capture confirm variable nom1

if _rc == 0 {

drop nom1

save `line ', replace

file read myfile line

}

if _rc == 111 {

file read myfile line

}

}

References 1

Reference 2

0 comments: