Announcement

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

  • Looping over many dta files

    Dear Statausers,

    I'm trying to loop using foreach over 4 dta files (missions_all missions_catholic missions_protestant missions_bfbs) to shorten my code.

    This is the longer version of what I want to do:

    Code:
    clear all
    
    global healthinfancy "/Users/perezp/Desktop/PhD/Research ideas/health infancy/Data"
    cd "$healthinfancy"
    
    use All_Data.dta, clear
    drop if longitude==.
    
    geonear id_geo latitude longitude using missions_all, neighbors(_ID _CY _CX) nearcount(1)
    gen lndist_all=ln(km_to_nid)
    rename km_to_nid dist_all
    drop nid
    
    geonear id_geo latitude longitude using missions_BFBS, neighbors(_ID _CY _CX) nearcount(1)
    gen lndist_bfbs=ln(km_to_nid)
    rename km_to_nid dist_bfbs
    drop nid
    
    geonear id_geo latitude longitude using missions_catholic, neighbors(_ID _CY _CX) nearcount(1)
    gen lndist_catholic=ln(km_to_nid)
    rename km_to_nid dist_catholic
    drop nid
    
    geonear id_geo latitude longitude using missions_protestant, neighbors(_ID _CY _CX) nearcount(1)
    gen lndist_protestant=ln(km_to_nid)
    rename km_to_nid dist_protestant
    drop nid
    This is the shorten version I want to do but it doesn't work:

    Code:
    clear all
    
    global healthinfancy "/Users/perezp/Desktop/PhD/Research ideas/health infancy/Data"
    cd "$healthinfancy"
    
    use All_Data.dta, clear
    drop if longitude==.
    local filelist: dir . files "missions_*"
    
    foreach file of local filelist {
    
    geonear id_geo latitude longitude using `"`file'"', neighbors(_ID _CY _CX) nearcount(1)
    gen lndist_`"`file"'=ln(km_to_nid)
    rename km_to_nid dist_`"`file'"'
    drop nid
    }
    The error is the following: "missions_all.dta invalid name

    May I get some help with this? Thanks in advance!

    Daniel.
    Last edited by Daniel Perez Parra; 12 May 2023, 11:07.

  • #2
    You're trying to name the variable your file name. Variables can't be named emerald.dragon, you'd need to use underscores

    Comment


    • #3
      Originally posted by Jared Greathouse View Post
      You're trying to name the variable your file name. Variables can't be named emerald.dragon, you'd need to use underscores
      Thank you Jared, how could I do that in my loop? I mean naming it lndist_missions_all, for instance, the name of the files include .dta and that's why it doesn't work I guess.
      Last edited by Daniel Perez Parra; 12 May 2023, 14:49.

      Comment


      • #4
        You can remove the suffix of a filename stored in a local macro file with:
        Code:
        . local file foo.dta
        
        . mata: st_local("varname",pathrmsuffix(st_local("file")))
        
        . di "`varname'"
        foo
        The inner st_local() retrieves the content of the local named file, pathrmsuffix() removes the suffix, and the outer st_local() stores the result in a local macro named varname
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          You can remove the suffix of a filename stored in a local macro file with:
          Code:
          . local file foo.dta
          
          . mata: st_local("varname",pathrmsuffix(st_local("file")))
          
          . di "`varname'"
          foo
          The inner st_local() retrieves the content of the local named file, pathrmsuffix() removes the suffix, and the outer st_local() stores the result in a local macro named varname
          Thank you Maarten. I've tried to introduce your code into mine but it should have done something wrong when combining it with the loop:
          error: lndist_missions_all.dta invalid name
          Code:
          local filelist: dir . files "missions_*"
          
          mata: st_local("mission",pathrmsuffix(st_local("filelist")))
          
          foreach file of local mission {
              geonear id_geo latitude longitude using `file', neighbors(_ID _CY _CX) nearcount(1)
              gen lndist_`file'=ln(km_to_nid)
              rename km_to_nid dist_`file'
              drop nid
          }

          Comment


          • #6
            Code:
            local filelist: dir . files "missions_*"
            foreach file of local mission {
                mata: st_local("varname",pathrmsuffix(st_local("file")))
                geonear id_geo latitude longitude using `file', neighbors(_ID _CY _CX) nearcount(1)
                gen lndist_`varname'=ln(km_to_nid)
                rename km_to_nid dist_`varname'
                drop nid
            }
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Originally posted by Maarten Buis View Post
              Code:
              local filelist: dir . files "missions_*"
              foreach file of local mission {
              mata: st_local("varname",pathrmsuffix(st_local("file")))
              geonear id_geo latitude longitude using `file', neighbors(_ID _CY _CX) nearcount(1)
              gen lndist_`varname'=ln(km_to_nid)
              rename km_to_nid dist_`varname'
              drop nid
              }
              Ir works! Thank you Maarten.

              Just for the record, in the foreach line it would be -filelist- instead of -mission-

              Comment

              Working...
              X