Announcement

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

  • Nested locals in some commands

    I constructed two locals, for instance, department and major , to show one's information:
    Code:
    local department ""School of Economics" "Quantitive Economics"" 
    local major ""Macroeconomics" "Monetary Policy""
    With the combination of these two locals, a nested local, info, was established:
    Code:
    local info "`department' `major'"
    I want to save the data in memory using the information contained in local info as file name, so I wrote:
    Code:
    save "`info'"
    But it did not work. What's wrong with my code? Thanks
    Last edited by Summer Xavier; 04 Mar 2020, 03:26.

  • #2
    Code:
    local a1 "foo"
    local a2 "bar"
    
    forvalues i = 1/2 {
        di "`a`i''"
    }
    So the local macro `i' is created in the forvalues loop, containing 1 in the first round and 2 in the second round. So in the first round Stata sees `a`i'', it first evaluates the inner macro `i' to 1, then it sees `a1', and evaluates that to foo.

    Is that what you are looking for?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      The problem is the quotes, you cannot have quotes in a file name.

      I assume that this is part of a loop, and you want to save many datasets. You could make this approach work. However, you will end up with very long file names, which are a pain to use. Typically I would just use consecutive numbers, maybe two sets of consecutive numbers: one for department and one for major. I would then label the data (keeping in mind the limit of 80 characters), and add additional information using notes (see help label and help notes).
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks bro! !
        De facto, I came up with this example for I encountered a data importing problem in which I had a few excel files with several spreadsheets in them. I wanted to write a code to import these spreadsheets and name them with their spreadsheets' file names. for example, an excel file named ExcelFile with SheetA, SheetB and SheetC in it. I want to name the subfile SheetA with the name ExcelFile-SheetA.dta. The example I posted previously was just an abstraction.

        Comment


        • #5
          Your suggestion that recording information by Stata's notes and label and naming the file with number is a very good idea! You really helps me a lot!!

          Comment


          • #6
            Originally posted by Summer Xavier View Post
            [...]I encountered a data importing problem in which I had a few excel files with several spreadsheets in them. I wanted to write a code to import these spreadsheets
            Originally posted by Summer Xavier View Post
            Your suggestion that recording information by Stata's notes and label and naming the file with number is a very good idea!
            For these and related tasks, also see xls2dta (SSC).

            Best
            Daniel

            Comment


            • #7
              Thank you very much!!

              Comment

              Working...
              X