Announcement

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

  • Error merging two files

    Below is a section of my do-file. I am trying to merge two files, HW and KR. The code below runs well up till this line - merge_kr_hw, KR("`kr2003'") HW("`hw2003'")
    local merged2003 `r(merged_file)' returning error message "option kr() required". Can anyone please help resolve the issue?

    // Function to merge KR and HW files
    program define merge_kr_hw, rclass
    syntax, KR(string) HW(string)

    clear
    // Load KR file
    use "`KR'", clear
    describe
    // Check for duplicate keys in KR file
    duplicates report caseid midx
    if r(N) > 0 {
    di as error "Error: Duplicate keys found in `KR'"
    exit 1
    }

    // Load HW file
    di "Merging with HW file: `HW'"
    merge 1:1 caseid midx using "`HW'"

    // Check for duplicate keys in HW file
    duplicates report caseid midx
    if r(N) > 0 {
    di as error "Error: Duplicate keys found in `HW'"
    exit 1
    }

    // Handle merge results
    drop if _merge == 2 | _merge == 3
    drop _merge

    // Save merged file
    tempfile merged
    save `"`merged'"', replace

    return local merged_file `"`merged'"'
    end

    // Ensure KR and HW file paths are defined
    local kr2003 "${path}/NG_2003_DHS_07022024_1257_196132/NGKR4BDT/NGKR4BFL.dta"
    local hw2003 "${path}/NG_2003_DHS_07022024_1257_196132/NGHW4BDT/NGHW4BFL.dta"
    local kr1990 "${path}/NG_1990_DHS_02072024_824_196132/NGKR21DT/NGKR21FL.dta"
    local hw1990 "${path}/NG_1990_DHS_02072024_824_196132/NGHW21DT/NGHW21FL.dta"

    // Merge KR and HW for each year
    merge_kr_hw, KR("`kr2003'") HW("`hw2003'")
    local merged2003 `r(merged_file)'

    merge_kr_hw, KR("`kr1990'") HW("`hw1990'")
    local merged1990 `r(merged_file)'


  • #2
    When you call the program merge_kr_hw, you should specify the options in lowercase, e.g.

    Code:
    merge_kr_hw, kr("`kr2003'") hw("`hw2003'")
    In the syntax command, the capitalization of options is used to tell Stata the minimal amount to which the option can be abbreviated, but the options themselves must be specified in lowercase when the program is called.

    Comment


    • #3
      Thanks, Hemanshu. I just specified the options in lowercase and I got another error message "invalid file specification". Please, what could this mean?

      Comment


      • #4
        That likely means it could not find the file you specified. If you are sure the location and file name is correctly specified. Also if you are executing these statements interactively -- line by line -- remember that the macros may not store their values as you are expecting. Running the code in a batch (from a do-file) will fix that.

        Comment


        • #5
          The two files (hr and kr) are correctly specified and are in the location. No, I am not doing it interactively but executing it in my do-file.

          Comment


          • #6
            Also, within the program, you should again use lowercase for hw and kr whenever you reference those macros.

            Comment


            • #7

              Thanks, Hemanshu. Your comments have been very helpful. All along, I have always used stata interactively so I am currently learning through codes to enhance reproducibility of my work.

              I made the update adjustment and the programme worked. However, I got this error message below again:

              "Duplicates in terms of caseid midx


              Copies Observations Surplus

              1 6029 0

              Error: Duplicate keys found in C:/Users/abc/OneDrive - National University of Ireland, Galway/Desktop/PhD/PhD_Data /01_data/01_raw_data/DHS/SSA/NG_2003_DHS_07022024_1257_196132/NGKR4BDT/NGKR4BFL.dta"

              The path in the error message is the path on my PC to the KR file.

              Comment


              • #8
                You might want to double-check your data and look for the duplicates to see how you want to resolve this. Stata is never wrong about the existence of duplicates.

                Comment


                • #9
                  Thanks so much for your help, I am currently working on this now.

                  Comment

                  Working...
                  X