Hi all,
I have multiple wide and long datasets stored in a unique folder. I am trying to write a code to merge them all. I have datasets with 1 line per participant (unique id = identano) and datasets with multiple lines per participant (still identano for each participant but visitnum for the number of visit baseline visit 1 etc.). I would like STATA to automatically recognize a wide or long dataset and create my unique working dataset automatically.
I wrote this code but it is still not working, someone here to help me? Thanks so much! I imagine there are many ways to make this code easier and shorter +++ as well. Thanks a lot to you all +++
Best,
Pierre
local dir "xx"
cd "`dir'"
local files: dir "`dir'" files "*.dta"
local base_datasets ""
local visit_datasets ""
foreach file of local files {
use "`file'", clear
capture confirm variable visitnum
if !_rc {
local visit_datasets "`visit_datasets' `file'"
}
else {
local base_datasets "`base_datasets' `file'"
}
}
foreach base_file of local base_datasets {
if `base_merged' == 0 {
use "`base_file'", clear
local base_merged = 1
}
else {
merge 1:1 identano using "`base_file'", assert(match using) nogenerate
}
}
save base_temp.dta, replace
local visit_merged = 0
foreach visit_file of local visit_datasets {
use "`visit_file'", clear
reshape wide *, i(identano) j(visitnum)
if `visit_merged' == 0 {
save visit_temp.dta, replace
local visit_merged = 1
}
else {
merge 1:1 identano using visit_temp.dta, assert(match using) nogenerate
save visit_temp.dta, replace
}
}
use base_temp.dta, clear
merge 1:1 identano using visit_temp.dta, assert(match using) nogenerate
save dataset_final.dta, replace
erase base_temp.dta
erase visit_temp.dta
I have multiple wide and long datasets stored in a unique folder. I am trying to write a code to merge them all. I have datasets with 1 line per participant (unique id = identano) and datasets with multiple lines per participant (still identano for each participant but visitnum for the number of visit baseline visit 1 etc.). I would like STATA to automatically recognize a wide or long dataset and create my unique working dataset automatically.
I wrote this code but it is still not working, someone here to help me? Thanks so much! I imagine there are many ways to make this code easier and shorter +++ as well. Thanks a lot to you all +++
Best,
Pierre
local dir "xx"
cd "`dir'"
local files: dir "`dir'" files "*.dta"
local base_datasets ""
local visit_datasets ""
foreach file of local files {
use "`file'", clear
capture confirm variable visitnum
if !_rc {
local visit_datasets "`visit_datasets' `file'"
}
else {
local base_datasets "`base_datasets' `file'"
}
}
foreach base_file of local base_datasets {
if `base_merged' == 0 {
use "`base_file'", clear
local base_merged = 1
}
else {
merge 1:1 identano using "`base_file'", assert(match using) nogenerate
}
}
save base_temp.dta, replace
local visit_merged = 0
foreach visit_file of local visit_datasets {
use "`visit_file'", clear
reshape wide *, i(identano) j(visitnum)
if `visit_merged' == 0 {
save visit_temp.dta, replace
local visit_merged = 1
}
else {
merge 1:1 identano using visit_temp.dta, assert(match using) nogenerate
save visit_temp.dta, replace
}
}
use base_temp.dta, clear
merge 1:1 identano using visit_temp.dta, assert(match using) nogenerate
save dataset_final.dta, replace
erase base_temp.dta
erase visit_temp.dta
Comment