Announcement

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

  • Finding code snippets from Stata's base commands

    Dear Statalist,

    Is there a way to see the programme code from Stata's own base commands?

    In my case, I am interested in seeing how the firstrow option of Stata's import excel command works exactly because I want to learn from it in order to do something similar (I want to be able to modify the column headers of my Excel file after importing and only then should these headers become variable names, so I can't just use the firstrow option directly). But when I type sysdir and then locate the import_excel.ado file in my BASE folder, it contains only very limited reference code, not the full programme...

    Many thanks,
    Felix

  • #2
    what do you mean by saying not the full programme. This is the full programme.ado in my computer for your reference.
    Code:
    program define import_excel
        version 12
    
        if ("`c(excelsupport)'" != "1") {
            dis as err `"import excel is not supported on this platform."'
            exit 198
        }
    
        gettoken filename rest : 0, parse(" ,")
        gettoken comma : rest, parse(" ,")
    
        if (`"`filename'"' != "" & (trim(`"`comma'"') == "," |        ///
            trim(`"`comma'"') == "")) {
            local 0 `"using `0'"'
        }
    
        capture syntax using/, DESCribe
        if _rc {
            capture syntax using/                    ///
                [, SHeet(string)                ///
                CELLRAnge(string)                ///
                FIRSTrow                    ///
              ALLstring                    ///
                case(string)                    ///
                locale(string)                    ///
                clear]
            if _rc {
            syntax [anything(name=extvarlist id="extvarlist" equalok)] ///
                using/                        ///
                [, SHeet(string)                ///
                CELLRAnge(string)                ///
                FIRSTrow                    ///
                ALLstring                    ///
                locale(string)                    ///
                clear]
            }
        }
        mata : import_excel_import_file()
    end
    Last edited by Liu Qiang; 30 May 2019, 02:47.
    2B or not 2B, that's a question!

    Comment


    • #3
      Well, I disagree with post #2. The import_excel.ado is just a "wrapper" that passes the task to the Mata import_excel_import_file() function. That function appears to be compiled Mata code in a Mata library and thus is not viewable, so you won't be able to copy and modify it.

      Offhand - meaning I haven't tried this, I'm just guessing - I would approach this problem by first importing the single row containing the column headers as string variables v1 v2 .... . I would then change the values to what it is you want to use as variable names. I would then save those changed values into local macros. Then I would clear the working dataset and import the rows containing the data (but not the column headers) into Stata variables v1 v2 ... and Stata will do the appropriate conversions. Then I would in a loop use the local macros to rename the variables.

      Comment

      Working...
      X