Announcement

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

  • Copy values from one variable to another

    Hello,

    I was wondering how to copy values from one variable to another. I have two variables of age - one is the complement of the other. The reason is that when I merged two databases the names of the variables were different. For example, the variable "age1" has ages from 2013-2015, and the variable "age2" has ages from 2015-2020. I created a new variable called "age" and I wanted to copy the ages from "age1" to "age" and "age2" to "age". Do you think this is possible? If so, how?

    Thank you so much for your help!

    Valeria

  • #2
    Of note, the ages from one variable (i.e., age1) are the missing values of the other variable (i.e.,age2)

    Comment


    • #3
      Code:
      gen age = age1
      replace age = age2 if missing(age)
      Your description is a little vague. In the future, when asking for help with code, show example data so there is no confusion about what you are working with. And do that by using the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Comment


      • #4
        Thank you!! This is very helpful. I was not aware of the -dataex- command.

        Comment


        • #5
          Code:
          gen age = max(age1, age2)
          would also seem to match your description.

          Comment

          Working...
          X