Announcement

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

  • Merging observations into one variable

    I am working with survey data. Respondents are given the option to respond in different units for some questions. For example, take weight: respondents may choose to either respond in kgs, or lbs. Thus, I have two separate variables that measure the same concept (i.e. variable 1 is weight_kgs, and variable 2 is weight_lbs), but each unique ID will have an observation either recorded in kgs, or in lbs.

    After converting the observations to the same unit (i.e. both will be in kgs), I would like to create one variable that contains both the observations originally recorded as kgs (weight_kgs), as well as those just converted from lbs.

    I tried to merge the variables, but that did not work. Any way to do this on Stata?

  • #2
    If your original variables after conversion were named orig_kg and orig_lb:

    Code:
    generate weight = cond(missing(orig_kg), orig_lb, orig_kg)
    // make sure no instances in which both variables had potentially valid data
    generate problem = !missing(orig_kg) & !missing(orig_lb) & (orig_kg != orig_lb)
    tab problem

    Comment


    • #3
      Welcome to Statalist.

      The short answer is that it is certainly possible to do what you want in Stata. But saying how to do that depends on knowing more about your data and what you have tried than you told us. How is one to know which variable each respondent chose? Is the unchosen one a Stata missing value (".") or is it zero or is it something else, such as -9, which is often popular in survey data? And I cannot begin to guess what command you used to "merge the variables" since Stata's merge command is used to combine observations in different datasets, not values in different variables.

      Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

      The more you help others understand your problem, the more likely others are to be able to help you solve your problem. I really tried to give an answer, but there are too many ways I can guess wrong about your data.

      Comment


      • #4
        Originally posted by Mike Lacy View Post
        If your original variables after conversion were named orig_kg and orig_lb:

        Code:
        generate weight = cond(missing(orig_kg), orig_lb, orig_kg)
        // make sure no instances in which both variables had potentially valid data
        generate problem = !missing(orig_kg) & !missing(orig_lb) & (orig_kg != orig_lb)
        tab problem
        This worked, thank you so much!

        Comment


        • #5
          Originally posted by William Lisowski View Post
          Welcome to Statalist.

          The short answer is that it is certainly possible to do what you want in Stata. But saying how to do that depends on knowing more about your data and what you have tried than you told us. How is one to know which variable each respondent chose? Is the unchosen one a Stata missing value (".") or is it zero or is it something else, such as -9, which is often popular in survey data? And I cannot begin to guess what command you used to "merge the variables" since Stata's merge command is used to combine observations in different datasets, not values in different variables.

          Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem. I really tried to give an answer, but there are too many ways I can guess wrong about your data.
          Thank you for the advice. Fortunately, the other poster's code did the trick but in the future, I'll be sure to provide more detail and include sample data.

          Comment

          Working...
          X