Announcement

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

  • Program and Syntax command

    Hi all, I am new to the forum, and I appreciate your kind help in advance.

    I am currently in the process of replicating a paper. Here is the code:

    capture program drop generate_labeled_var
    program generate_labeled_var
    syntax, yvar(string)

    capture gen T_`yvar' = T
    local label: variable label `yvar'
    label var T_`yvar' "`label'"
    end


    foreach yvar in eq_index_mh_all {
    generate_labeled_var, yvar(`yvar')
    }

    Could someone help explain what the code is doing? it defines a function generate_label_var and use syntax to fit in. but what is yvar(string) doing? and how to understand T_`yvar' = T ?

    Thanks a lot

  • #2
    Whoever did this is, to put it politely, either 1) a very silly person or 2) engaged in some heavy data management tasks that aren't apparent from the code. I write ado code, so I know what's going on here, I just don't understand why someone would do this.

    Essentially, all they're doing is taking the current label of a T_[variable] and slapping a T_ in front of its variable label.

    Comment


    • #3
      I don't think the explanation in #2 is correct. I do agree, however, that this program looks rather amateurish, and given what it does, it really isn't clear why one would bother writing a program to do it in the first place.

      The program assumes, without verifying, that there is already a variable named T in the data set. (If there is no such variable, the program will throw an error message and abort execution.) It also assumes without verifying that the string that you pass in option -yvar()- when you call the program is in fact the name of another already existing variable. (Again, if that is not the case, the program will throw an error message and abort execution.) Assuming these conditions are, in fact, met, it will create a new variable whose name is T_ followed by the name of the variable that was passed to it in option yvar(), containing the same values as that yvar() variable has (but recast as a float if the original was something else) and then give that new variable the same label that the variable passed in option yvar() had.

      The program is amateurish because it can break as a result of several unchecked assumptions, it uses an option to catch what could more effectively have been passed in a varlist, and risks losing precision in the newly created variable if the original was a long or double. Moreover the function it carries out is pretty trivial and I don't see why anyone would write a program for that. Also amateurish is the -foreach- loop which iterates over just a single variable! The exact same thing could be done much better with just:
      Code:
      clonevar T_eq_index_mh_all = T
      label var T_eq_index_mh_all `"`:var label eq_index_mh_all'"'
      Last edited by Clyde Schechter; 16 Dec 2022, 23:27.

      Comment


      • #4
        I agree strongly with Clyde Schechter

        A side-note: if you want to clone existing variable T but also want give the new variable the same variable label as some existing variable, say frog you could do this, which would be less typing.

        Code:
        clonevar T_wanted = frog 
        replace T_wanted = T 
        as clonevar copies the variable label of its argument and if you want the values of T you could then replace as shown.

        Somewhat also in Clyde's style, I flag also ways in which this could go wrong. If frog is float, but T is double you could lose precision, and the problem could be (much) worse with other storage types. All said, Clyde's code is safer.

        Comment


        • #5
          Nick Cox Clyde Schechter Jared Greathouse Thanks you all for your generous input and time. Yes, this is one of the hot AER papers published this year. Again I appreciate you all taking the time to answer my questions.

          Comment


          • #6
            I think we need a 3) to Jared's 1) and 2), perhaps: a good economist but not quite so skilled at Stata.

            Comment

            Working...
            X