Announcement

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

  • Local macros not showing up in file name

    Hi all,

    I have a forval loop that is trying to take advantage of local macros for naming certain files. (Please note that there is code above this that allows for the .dta file to enter into this loop properly; I am showing only this portion to hone in on the problem I'm having--see my example output in the second code box below):
    Code:
    forval i = 1/`numvars' { import delimited "$dofile/output_data/`fp_`i''", delim("|") clear gen file = "`fp_`i''" dis file capture drop st yr bch gen st = substr(file, -21, 2) gen yr = substr(file, -18, 4) gen bch = substr(file, -13, 1) local state st dis `state' local year yr dis `year' local batch bch dis `batch' save "$temp/fp_`i'_`state'_`year'_`batch'", replace
    So, when I use a file called andersaa_VT_1909_1_trained.txt, my end goal is to have a file named fp_1_1901_1_trained.txt, where the number after fp will be different for each time through the loop.

    What I get for my output upon running the above:
    Code:
    andersaa_VT_1909_1_trained.txt VT 1909 1 file C:/Users/andersaa/Documents/datatemp/fp_1_st_yr_bch.dta saved
    All of the displaying of my local variables works as expected, but then when the final file is saved, it uses "st" "yr" and "bch" instead of the actual values for those things ("VT", "1909", and "1", respectively). Since I'm running this and everything above it every time, I can't figure out why my local macros wouldn't be working in this case. Any help is greatly appreciated!

    Best,

    Sarah
    Last edited by Sarah Atherton; 26 May 2015, 09:03.

  • #2
    Look at your second line!

    Code:
    `fp_`i''
    Given local macro i set to 1, Stata will look for local macro fp_1 and (presumably) fail to find it. (Just as elementary algebra, expressions are evaluated inside outwards.)

    And so forth, every time around the loop.

    My guess is that you meant to type

    Code:
    fp_`i'
    The same error occurs later; I stopped at the first example.

    Fussy advice, but well meant, and codified in the FAQ:

    1. We ask for full real names here.

    2. Using code delimiters makes code easier to read, and everyone benefits.
    Last edited by Nick Cox; 26 May 2015, 08:35.

    Comment


    • #3
      Sarah A, check for the slashes in the locals. See the following illustration:

      Code:
      local folder "C:\data\"
      local index "1"
      display "`folder'`index'"
      
      mata st_local("path",pathjoin(st_local("folder"),st_local("index")))
      display "`path'"
      First output is: C:\data`index'
      Second output is: C:\data\1

      Best, Sergiy Radyakin

      Comment


      • #4
        I think Sarah is using Stata variables where she should be using macros. Consider the following (where the first three commands imitate the forval loop for purposes of this example)
        Code:
        . global temp C:\Users\andersaa\Documents\datatemp
        
        . local i 1
        
        . local file andersaa_VT_1909_1_trained.txt
        
        . local state = substr("`file'", -21, 2)
        
        . local year  = substr("`file'", -18, 4)
        
        . local batch = substr("`file'", -13, 1)
        
        . display "$temp/fp_`i'_`state'_`year'_`batch'"
        C:\Users\andersaa\Documents\datatemp/fp_1_VT_1909_1
        
        .
        In Sarah's code, the macro state is set to the character string st (rather than to the state abbreviation found in the variable st) and the command
        Code:
        dis `state'
        becomes
        Code:
        dis st
        which displays the value of the variable st in the first observation.

        Comment


        • #5
          William, that indeed was my problem! Thank you!!

          Thanks to Nick and Sergiy as well for the feedback/advice.

          Comment

          Working...
          X