I have multiple .dbf files that I would like to import one at a time, change the name of a variable, and save as a .dta file. The folder that contains the .dbf files contains other files as well that I would like Stata to ignore.
Each of the dbf files is named one of the options listed in the local macro mylist, followed by _ward_CTS.dbf. So for example, B_ward_CTS.dbf is one of the files.
My code is the following:
program drop _all
macro drop _all
set more off
cd "/Users/slums-india/cleaning/maps processing/Ward Point
Maps/Output"
clear
local files : dir "/Users/slums-india/cleaning/maps processing/WardPoint
Maps/Output" files "*.dbf"
local mylist B C D E FN FS GS HE HW KE
foreach file of local mylist {
use 'file'_ward_CTS.dbf
/*import database*/
import dbase "'file'_ward_CTS.dbf", clear
/*rename CTS number variable*/
rename cts$V4 cts_number
save "/Users/slums-india/cleaning/sra/temp/'file'_ward_CTS.dta", replace
}
I cannot seem to get this loop to run. The error I get is invalid 'file'.
What am I doing wrong?
You need to delete the first line in the loop, change ' in file and add quotes:
foreach file of local mylist {
/*import database*/
import dbase "`file'_ward_CTS.dbf", clear
/*rename CTS number variable*/
rename cts$V4 cts_number
save "/Users/slums-india/cleaning/sra/temp/`file'_ward_CTS.dta", replace
}
Related
I have a bunch of .txd and .dff files that have different names, let's say, like, img1 img30 greenimg3, etc. all in the same folder. I can compile a list of these names that I want to replace (but not all the files in the folder will be replaced), but how can I replace my list of files with just one file without altering the name of the file? So it remains img1, img30, greenimg3, etcetera but with the replaced file. I hope this makes sense. I would be grateful if someone could write something that I could use, as I don't know how to code myself.
not sure if i understand corretly, but you could do this in python:
import os
folder_path = "./folder"
with open("source.file", "rb") as source_file:
source_file_content = source_file.read()
for file in os.listdir(folder_path):
with open(os.path.join(folder_path, file), "wb") as destination_file:
destination_file.write(source_file_content)
where "./folder" is the path to your folder containing all files you want to overwrite and "source.file" is the path to your file you want to overwrite with.
this script reads and writes the files as binary
I'm importing dta files in a folder and exporting each to a csv file. I'm not sure why but the loop doesn't save the file. Here's the code:
global path "file path"
cd "${path}"
* Geocodes for edd
clear
local files: dir "${path}Data\MeasureData\" files "*_edd.dta"
*do loop to bridge file for EDD and
foreach file in `files' {
use "${path}Data/MeasureData/`file'", clear
rename beafips edd_id
merge m:1 edd_id using Data\TempData\bridge_edd.dta
keep if _merge==3
drop _merge
export delimited "${path}Data\OutgoingData\`file'.csv", replace
}
I keep getting error like this:
file filepath\Data\OutgoingData.csv saved
I was expecting this to be saved as filepath\Data`file'.csv. What did I do wrong?
This is happening because Windows defaults to using backslashes in path directories, but in this context your computer is reading the backslash as an escape character and so isn't interpreting the ` as indicating the beginning of your local files.
This problem won't typically appear on a Mac/Linux machine as they default to using forward slashes in directory paths.
So the solution is to change all the \s to /s in your code. See here for a more detailed write-up of the problem: https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800800310
I am trying to use a simple windows cmd command to move a set of .csv files from one directory to another.
I know that this can be easily achieved through:
MOVE "*.csv" "D:/lorik_home"
The issue comes when I want to only move some specific files with a filename mask, for instance, I want to move all files which are in the .csv extension and start with the name: lorik_files_. I already tried using:
MOVE "lorik_files_*.csv" "D:/lorik_home"
This works when there is only one file of the format: lorik_files_20112233_09_33_33.csv, but when there are two files with the masks such as:
lorik_files_20112233_09_33_33.csv
lorik_files_20112233_10_23_42.csv
I get an error such as:
A duplicate file name exists, or the file cannot be found.
I need a hand for capturing those filename prefix masks, conditioned by .csv extension at the end.
I'm relatively new to stata and I need to append several .dta files together into one dataset.
I have a folder called 2015 and the files look like the following...
jan_2015.dta
feb_2015.dta
mar_2015.dta
... and so on till dec_2015.
I tried the following code:
cd C:\Users\TOSHIBA\Desktop\Lender_List\Compiled\2015
local mylist jan_2015 feb_2015 mar_2015 apr_2015 may_2015 jun_2015 jul_2015 aug_2015 sep_2015 oct_2015 nov_2015 dec_2015
foreach filename of local mylist {
use `var'_2015
append using "jan_2015.dta"
}
but the output from stata said that file _2015.dta not found.
There's no need to loop: append can take a list of files that you can get with fs:
ssc install fs
cd C:\Users\TOSHIBA\Desktop\Lender_List\Compiled\2015
clear
fs *_2015.dta
append using `r(files)'
If you are still eager to loop over files by name for some reason:
cd C:\Users\TOSHIBA\Desktop\Lender_List\Compiled\2015
clear
foreach filename in `=lower("`c(Mons)'")' {
append using "`filename'_2015.dta"
}
The main problem with your code is that the local macro lname is filename, but you dereference something called var, which evaluates to nothing, so Stata cannot find the file named _2015.dta and complains. The second problem is that your loop seems to try to open each month's file and append January's data to it. That does not sound like what you have in mind.
Good evening,
I am currently working on a programm that takes information from a file into a Database, for testing purposes I used to open Testfiles in the classical way via IO:
function reader (file, delimeter)
local f = io.open(file)
for line in f:lines() do
lines[count] = splitty(line, delimeter)
count = count + 1;
end
end
(this part also containes the first part of a splitter)
But in the actual environment, the database programm imediatly moves the file in another directory with a name change to, for example this:
$30$15$2016$09$26$13$27$24$444Z$.Pal.INV.csv
Now I know the directory but I can't really predict the name, so I wanted to know if there might be a way to open files without knowing their name.
(and delete them after reading them)
I had ideas to use a modified link:
local inputFile = "D:\\Directory\\(*all)"
but it failed.
Other aviable information:
The system is until now only planned on Windows PCs.
The directory will always only contain the one file that is to ready, no other files.
You can use the lfs.dir iterator from LuaFileSystem to iterate through the contents of the directory. A small example:
local lfs = require("lfs")
local path = "D:\\Directory\\" -- Your directory path goes here.
for filename in lfs.dir(path) do
print(filename) -- Work with filename, i will just print it
end
If you keep a record of the files you will be able to know which one is the new one. If it is only one file, then it will be easier, you can just check the extension with a string function. From what i remember the iterator includes .. and .. lfs documentation can be found here.
-- directory name and file name should consist of ASCII-7-bit characters only
local dir = [[C:\Temp\New Folder]]
local file = io.popen('dir /b/s/a-d "'..dir..'" 2>nul:'):read"*a":match"%C+"
if not file then
error"No files in this directory"
end
-- print the file name of first file in the directory
print(file) --> C:\Temp\New Folder\New Text Document.txt