Announcement

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

  • Generate variable with the name of another variable

    Hello. In a dataset like this:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(x1 x2 z)
    10 20 20
    34 14 34
    end

    I need to create a variable this_is that indicates which variable, x1 or x2, matches in value with z

    In this case, the result should be:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(x1 x2 z) str2 this_is
    10 20 20 "x2"
    34 14 34 "x1"
    end

    How can I do it? Thanks


  • #2
    what do you want the new variable to be is x1=z AND x2=z?

    Comment


    • #3
      Sorry if I didn't make myself understood. In this example, in the first row since z = x2 then the result of this_is is "x2"; in the second row, since z = x1 then the result of this_is is "x1".

      Comment


      • #4
        I do understand that but apparently you did not understand what I meant: what if x1=10 and x2=10 and z=10 - then what should your new variable be?

        Comment


        • #5
          Oh, I understand. For what I need, either of the two results works: x1 or x2. If you could include the instruction that the result be the first variable (from left to right), in this case x1, that would be great. Thanks a lot

          Comment


          • #6
            try the following (this could be compressed to one line using the cond() function):
            Code:
            gen str this_is=""
            replace this_is="x2" if x2==z
            replace this_is="x1" if x1==z

            Comment


            • #7

              Code:
              gen this_is = cond(z == x1, "x1", "x2")
              or paranoid code

              Code:
              gen this_is = cond(z == x1, "x1", cond(z == x2, "x2", "???????"))

              Comment


              • #8
                Well, I was wrong in assuming that it would be understood that what I need is for general cases, not for specifically these two variables x1 and x2. Basically I would like to know if there is a command, or a more general trick, that generates values based on the names of other variables. And now, yes, I apologize for not making myself understood correctly.

                Comment


                • #9
                  A more abstract formulation is this:

                  initialise a string variable with empty

                  loop over some other variables:

                  replace string variable with variable name if some condition is true

                  As Rich Goldstein flagged, a key detail is what we want to happen if there are multiple finds, e.g. to record first or last instance found or all instances found.

                  A recent paper in the Stata Journal discusses row-wise examples.

                  Comment


                  • #10
                    Thanks to both

                    Comment

                    Working...
                    X