Announcement

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

  • Deleting the last variable while importing excel file into STATA .dta format

    Dear STATA users,

    I'm importing the excel file with multiple worksheets. In each worksheet the number of variables vary. The last variable doesn't have a name but it has the values(grades at some university), something like 20/28, 23/28, 21/28, 25/28 and so on. I tried to drop the variables if it contains the "/" in it. But some error occurs.

    The code is this:

    cd "C:\Users\yerbolat.assylbekov\Documents\IR\IR Projects\Attendance Records\Datasets\Raw\Fall 2013"
    set more off
    clear
    import excel using "Attendance Fall 2013_", describe
    forvalues sheet=1/`=r(N_worksheet)' {
    local sheetname=r(worksheet_`sheet')
    import excel using "Attendance Fall 2013_", sheet("`sheetname'") allstring clear
    gen course = B[1]
    order course, first
    drop in 1/4
    *drop if A=="Student ID"
    ds, has(type string)
    foreach var of varlist `r(varlist)' {
    count if regexm(`var',"/")
    if r(N)>0 {
    drop `var'
    }
    save "`sheetname'", replace
    }
    }



    It shows "worksheet . not found".

    Could you please help me with that?

  • #2
    Is it always the last variable? If so, you want something like

    Code:
    local nvars = c(k)
    unab allvars : *
    local lastvar : word `nvars' of `allvars'
    drop `lastvar'
    Alternatively, look at the cellrange() option in import excel.

    For the import and saving part, also see xls2dta (SSC).

    Best
    Daniel
    Last edited by daniel klein; 25 May 2017, 02:43.

    Comment


    • #3
      Thank you, Daniel.

      It's always the last variable, but the number of variables in each sheet vary. Also, in some sheets I need to drop last 2 variables. Is that possible to do somehow?
      The last 2 variables have characters "/" and "%". Maybe somehow it's possible to say STATA to drop variable if it contains these two characters. So, it's 1 excel file with multiple worksheets in it.

      Sincerely,
      Yerbolat

      Comment

      Working...
      X