Hey folks,
im pretty new to stata and trying to merge different variables into 1 variable for ~20 datafiles with a loop.
I have different Housholds in the datasets which are identified with the variable names q1a, q1b, q1c, paid and hhid. This variables shall be merged into the single variable HH_ID across the 20datasets in my file. In order to do so I tried different commands:
Try1:
local files : dir"C:\Users\User\Desktop\Master\Thesis\Data\Data Cleaning\Required Data Files" files "*.dta*"
foreach file in `files' {
generate HHID = string(q1a) + string(q1b) + string(q1c) + string(paid) + string(hhid)
save `files', replace
}
Stata: unexpected end of file
Try2:
local myfilelist : dir"C:\Users\User\Desktop\Master\Thesis\Data\Data Cleaning\Required Data Files" files "*.dta*"
dir `myfilelist'
foreach datafile of local myfilelist {
generate HH_ID = string(q1a) + string(q1b) + string(q1c) + string(paid) + string(hhid)
save "*.dta*", replace
}
Stata: unexpected end of file
Using only the command " generate HH_ID = string(q1a) + string(q1b) + string(q1c) + string(paid) + string(hhid)" it is possible to merge and create the new variable, so this one works, however the loop does not.
Can someone give me advice how to alter this command to run that loop?
Thank you in advance!
Related Posts with foreach loop variables
Extracting All Strings After a Word within a Variable NameI have a dataset representing a treated unit (GDP), a series of estimated counterfactuals, (cf) and …
Assigning value from one variable to another if condition is metI have a data manipulation question: I have a dataset with the variables "id_1", "year", and "id_2."…
Renaming Matrix Rows With SpacesSuppose I wish to rename the rows of a matrix. Code: clear * cls matrix A = (2,1\3,2) cls loc rn …
How to use xtcointtest with runby? OR How to do cointegration tests for each individuals separately for panel data?Dear all: I'm trying to use xtcointtest to do cointegration tests for each individuals separately in…
Is it possible that log mis-calculates it or create weird ".b" when the workload is too heavy?I use Stata 17 (24 cores). I am sorry that I cannot provide the reproducible example because this p…
Subscribe to:
Post Comments (Atom)
0 Response to foreach loop variables
Post a Comment