Announcement

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

  • Generating Treatment Variable Code With Non-Existent Variable

    Hello,

    I am trying to recreate a treatment variable with the following code:

    Code:
    ***the critical code***
    gen treat= groups1 !=""
    replace treat= . if date10 != .
    *gen treat=1 if groups1!=""
    label def treat_control 1 "Treatment" 0 "Control"
    So, basically the code does not work for me. When I run the code, it does create the variable "treat" but without any values. Besides, I would like to know what the meaning behind:
    Code:
    gen treat= groups1 !=""
    is, since variable "groups1" does not exist. This syntax appears multiple times. There is no error message, and the code runs without any problems. Besides, the line starting with *g seems also quite odd to me, since they are notes, however, I don't really understand what is meant with them. Is this abbreviated Stata language?

    Thank you for your help in advance.

  • #2
    Lines beginning with * are comments. The code following may have been part of the program previously but has been removed. This is often called being “commented out”.

    Comment


    • #3
      Besides, I would like to know what the meaning behind Code:

      gen treat_control = groups1 !=""
      is, since variable "group1" does not exist.
      -gen treat_control = groups1 !=""- tells Stata to create a new variable, named treat_control. The content of that variable is determined, in each observation of the data set, by evaluating the expression on the right side of the = character. Now, that expression is -groups1 != ""-. This is a Stata logical expression. Stata evaluates it by looking at the value of the variable groups1 (which, evidently, is supposed to be a string variable), and asks whether or not its value is missing (denoted by the null string ""). If it is not missing, then the expression groups1 != "" is true in that observation, if it is missing then the expression is false in that observation. Stata does not have a boolean data type, and instead, values of logical expressions are represented as numbers. In evaluating logical expressions, Stata will return 1 when the expression is true and 0 when it is false. In the reverse process of interpreting numerical expressions as logical expressions (which can arise in somewhat different contexts), 0 is taken to mean false, and anything other than zero (including missing values), true.

      So treat_control is set to1 in every observation where groups1 has a non-missing value, and 0 in every observation where groups1 has a missing value.

      Now, as you point out, the data set to which this code is being applied has no variable groups1. So if it were actually run, Stata would reply with an error message pointing out that fact. This is almost certainly the reason that these commands were, as Nick noted, commented out. But now you know what they would have done if they were not commented out and a string variable group1 existed in the data set.

      Comment


      • #4
        Hello Nic, hello Clyde,

        I have solved the problem. Thank you.
        Last edited by Penelope Smart; 23 Oct 2021, 09:23.

        Comment

        Working...
        X