Announcement

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

  • #16
    This may help:

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . mata: st_local(index, strofreal(st_varindex("mpg")))
                     <istmt>:  3499  index not found
    r(3499);
    
    . mata: st_local("index", strofreal(st_varindex("mpg")))
    
    . di "`index'"
    3
    If you type "index" Mata understands that the local macro is to be named, literally, index.

    If you type index Mata understands that the name to be used for the macro is itself a scalar called index already defined to Mata. But it can't find such a beast.

    That's my interpretation.

    Note that a literal "varname" won't work unless that really is a name in your dataset.

    Comment


    • #17
      Nick explained the error in syntax, that I would have noticed if I had tested the code. My fault; sorry.

      Comment


      • #18
        Thank you both a lot!

        For everyone that might also wants to import variables based on their variable name without knowing the column index beforehand, I would like to post the entire block of code I used in the end:


        Code:
        local files: dir "${data_raw}" files "*.csv"
        display `files'
        
        * loop through .csv data files
        foreach filename in `files' {
            noi display "looping through filename " "`filename'"
            import delimited "${data_raw}/`filename'", clear
            local indexcols = "" /* in this empty local, we will save the column indices of our selected string variables so we can import them as string */
        
            * Loop through all selected variables for which you want leading zeros to be preserved
            foreach var in var_x var_y var_z {    
                noi display "currently looping through variable: " "`var'"
                capture confirm variable `var'
                display "_rc value is: " _rc
                if _rc == 0 {
                    mata: st_local("index_`var'", strofreal(st_varindex("`var'")))
                    local indexcols = "`indexcols'" + " " + "`index_`var''"
                    noi display "*** The new updated column index is: " "`indexcols'"
                }
            }
            noi display "*** Final column index list for file `filename' is: " "`indexcols'"
            import delimited "${data_raw}/`filename'", stringcols(`indexcols') clear
            * CONTINUE WITH THE REST OF WHATEVER YOU WISH TO DO WITH YOUR DATA
        Why this can be useful:

        Should there be a database amendment in my study and new variables are added, the column index of my variables of interest for which I want to preserve the zeros, may shift. As I work in a regulated clinical trial environment, my code needs to be validated and any changes require approval. For this reason, I want to incorporate as little code that comes with the risk of having to be updated in case of database amendments.

        Thank you all for your thoughts and helpful feedback,

        Best wishes,

        Moniek

        Comment

        Working...
        X