Announcement

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

  • Generate var named after the first character of element in loop

    Hi, I have written code to iteratively use a list of dta files. Now I want to generate a var which is named according to the first character of the dta file.

    Here is my current code, where the var is named according to the entire file name:

    Code:
    foreach file in afile bfile cfile {
    gen `file' = x_var
    }
    I want to generate a var in each file, e.g. afile.dta would contain the var: a_var

    I have tried adding a line before the gen line to generate a local which displays the first letter of the element, but I'm not sure what command to use to do so as substr is not allowed (see below). Perhaps a command like word but for single characters?

    Code:
    foreach file in afile bfile cfile {
    local shorthand di : substr("`fluid'", 1, 1)
    gen `file' = `shorthand'_var
    }
    
    substr not allowed
    I'd be very grateful if somebody could advise on the command!

    Many thanks,
    Liz
    Last edited by Liz Broom; 13 Jan 2024, 07:02.

  • #2
    Originally posted by Liz Broom View Post
    I want to generate a var in each file, e.g. afile.dta would contain the var: a_var
    You need to load the dataset first, which I do not see you doing in your code.

    Code:
    foreach file in afile bfile cfile {
        use `file', clear
        gen `file'= `=substr("`file'", 1, 1)'_var
        save `file', replace
    }
    The above assumes that in the file afile.dta, there is a variable named a_var. Thus, you generate a variable named afile in this file that is equal to a_var.
    Last edited by Andrew Musau; 13 Jan 2024, 07:24.

    Comment


    • #3
      Hi Andrew, thanks so much for your reply!

      Sorry, I accidentally mixed things up in the code when I was simplifying it to write here and so I haven't conveyed my question very clearly - I am actually trying to name the var after the first letter, rather than specifying the value of the var I generate. For example, file afile would have a var named a_var which would have a value of 1 for every observation in the code below.

      The code in the original question should have read as follows:

      Code:
      foreach file in afile bfile cfile {
      use `file', clear
      local shorthand di : substr("`file'", 1, 1)
      gen `shorthand'_var = 1
      }
      This generates the error:
      Code:
      ( invalid name
      Here is the code with your correction to the substr comand:

      Code:
      foreach file in afile bfile cfile {
      use `file', clear
      local shorthand di : "`=substr("`file'", 1, 1)'_var"
      gen `shorthand' = 1
      }
      This generates a var named di with the value of 1 for each observation. I want the var name to be x_var, where x is the first character of the file name.

      Apologies again for making such a mess of the question the first time, and thanks again!

      Best,
      Liz
      Last edited by Liz Broom; 13 Jan 2024, 07:28.

      Comment


      • #4
        Code:
        foreach file in afile bfile cfile {
            use `file', clear
            gen `=substr("`file'", 1, 1)'_var=1
            save `file', replace
        }

        Comment


        • #5
          Ah amazing, thank you! A very simple fix

          Comment


          • #6
            Apologies to keep asking on this thread, but is there a way I can save that code as a local so that I can easily use it whilst generating lots of different var, all of which I want to contain the corresponding file character within the middle of the var name. I have tried doing so as follows, but this just generates a var named 'example__example' with the value of 1:

            Code:
            foreach file in afile bfile cfile {
            use `file', clear
            local shorthand "`=substr("`file'", 1, 1)'"
            gen example_`shorthand'_example = 1
            save `file', replace
            }
            Many thanks,
            Liz

            Comment


            • #7
              Drop the double quotes:

              Code:
              foreach file in afile bfile cfile {
                  use `file', clear
                  local shorthand = substr("`file'", 1, 1)
                  gen example_`shorthand'_example = 1
                  save `file', replace
              }

              Comment


              • #8
                Thank you Andrew!

                Comment

                Working...
                X