Announcement

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

  • Combining codes of different do files

    I am trying to run the codes of two separate do files, combining them into the same do files. Each do file is taking data from different file directories. If I can get both codes to once, I will then combine data and variables before running a regression

    The codes for the two do files I am trying to combine are:

    clear all
    cd "G:\Shared drives\SE475 Capstone, Dennis Evans\State Data - Last 5 Years"
    foreach num of numlist 1 / 51 {
    import delimited `num'.csv, clear
    rename v1 week
    rename v2 interest
    gen state = `num'
    save S`num'.dta, replace
    }


    use S1.dta, clear
    foreach num of numlist 2 / 51 {
    append using S`num'.dta
    }

    save search_data.dta, replace


    use search_data.dta

    gen weekly_date = date(week, "MDY")
    format weekly_date %td

    drop week

    collapse interest, by(state weekly_date)

    sort weekly_date state

    rename state statenum

    browse

    ------------------------------------------------

    clear all
    cd "G:\Shared drives\SE475 Capstone, Dennis Evans\COVID Infection Rate Data"
    foreach num of numlist 1 / 51 {
    import delimited `num'.csv, clear
    gen statenum = `num'
    save Covid`num'.dta, replace
    }


    use Covid1.dta, clear
    foreach num of numlist 2 / 51 {
    append using Covid`num'.dta
    }

    gen weekly_date = date(date, "MDY")
    format weekly_date %td


    qui gen dow = dow(weekly_date)
    qui gen week_no = wofd( weekly_date)
    qui gen temp = date if dow == 0
    by week_no, sort: egen week_var = mode(temp)


    collapse totalcaserateper100k, by(week_var statenum)

    gen weekly_date = date(week_var, "MDY")
    format weekly_date %td
    sort weekly_date statenum
    drop week_var

    order weekly_date, before(totalcaserateper100k)

    save covid_data.dta, replace

    *use covid_data.dta

    *xtset statenum weekly_date

    browse

  • #2
    I don't understand the problem.

    Comment


    • #3
      I'm not sure I understand the problem either, but I have a hunch.

      First, it's not necessary to combine those two do-files into one in order to combine the data sets they create. You can just start a new data file that -use-s one of the data sets and -merge-s it with the other. Voila.

      Except, not voila. And this is where I think O.P. is running into problems. The variable weekly_date is calculated using rather different code in these two do-files. I suspect that as a result of that, there are few or no matches between the two data sets. As I don't quite understand what the variable weekly_date is supposed to be, I can't say which, if either, is correct. But I imagine this is where things are going awry.

      Comment

      Working...
      X