Announcement

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

  • Append and recode multiple datasets at once

    Hi,
    I am fairly new to stata and am trying to figure out a way where I can recode variables across multiple datasets and have the new recoded variables be saved into 1 aggregate data file. For context, I have 14 files all named: file_1.dta , file_2.dta, .... file_14.dta
    For this example, I have a variable MCREV06 where the 06 is the given year



    Code:

    cd "/Users/octavio/Desktop/MEPS"
    clear
    foreach i in file_1.dta file_2.dta file_3.dta file_4.dta file_5.dta file_7.dta file_8.dta file_9.dta file_10.dta file_11.dta file_12.dta file_13.dta file_14.dta {
    use file_`i'.dta
    keep MCREV`num'
    gen EVERMEDICARE=0
    replace EVERMEDICARE=1 if MCREV`num'==1
    append using file_`i'
    save aggregatedatafile

    }


  • #2
    Looks like you're on the right track. Here are some suggested changes:

    Code:
    cd "/Users/octavio/Desktop/MEPS"
    clear
    foreach i of numlist 1/14 {
    use file_`i'.dta
    keep MCREV`num'
    gen EVERMEDICARE=0
    replace EVERMEDICARE=1 if MCREV`num'==1
    capture append using aggregatedatafile
    save aggregatedatafile, replace
    }

    Comment


    • #3
      Phil, Thank you! This worked like a charm.

      Comment

      Working...
      X