Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop appending datasets with local and substr

    Hello,

    I am using DHS datasets of multiple countries and survey years. I have to append men's data to women's data in each individual country and year. I am hoping to automate this process to facilitate future work. The problem is there is no pattern in the file names. The files follow this general format CCTTYYDD.dta in which CC is the country label, TT is the survey type, YY is the survey wave, and DD is indicating a state file. For example, BUMR61FL.DTA is Burundi, male, 2001, Stata survey file. The TT and DD indicators are the same but the CC and YY are not consistent.

    Here are some examples of file names: CMIR44dt, CMMR44dt, CMMR61DT, CMMR71DT, KEMR42DT, KEIR52DT


    To append multiple datasets I have been trying to use multiple local macros and substr to save some general directories to then pull the women's and men's data sets that are matching in everything but the TT. The code is as follows:


    cd "C:\Users\Jolanta\Data\STATA data"

    local files : dir . files "*.dta"

    foreach file of local files {


    local country = substr("`file'", 1, 2)
    local round = substr("`file'", 5, 2)

    local women_file : dir . files "``country'*IR*``round'??.dta"
    local men_file : dir . files "``country'*MR*``round'??.dta"

    if "`women_file'" != "" & "`men_file'" != "" {

    use "`men_file'", clear
    append using "`women_file'"
    save "`country'CR``round'.dta", replace
    }
    }



    When I use this code the do-file runs with no errors but no files are saved. I assume this is because data is not actually finding files that fit the name indicated. I am new to Stata so any help would be appreciated!

  • #2
    add

    di "`men_file'" before use "`men_file'" to see what's its using

    Comment

    Working...
    X