Announcement

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

  • issue with reshaping wide to long

    Hello Statlist community,
    I have the following data that I would like to reshape to long:
    Findid sex_98 age_98 qwealth_98 sex_06 age_06 qwealth_06
    12345 Male 32 70 Male 40 50
    I would like to reshape it so that it appears as follows
    Findid year sex age qwealth
    12345 1998 Male 32 70
    12345 2006 Male 40 50

    when I use the following code: reshape long sex age crempstp mnthwgAllJob yrschl crstablp crsectrp qwealth, i(Findid) j(year)
    I get an error message saying that: "variable year contains all missing values"

    I thought there was an issue with Stata trying to identify the year variable so created one, and when I tried reshaping the data from wide to long, I also got an error message saying that the data is already in long format even though it's not.
    Do you have any idea what's wring and if so how I can fix it?

    Thank you.

  • #2
    Code:
    reshape long sex age crempstp mnthwgAllJob yrschl crstablp crsectrp qwealth, i(Findid) j(work) string 
    gen year = cond(work == "_98", 1998, 2006)

    Comment


    • #3
      When you write -reshape long sex age, i(findid) j(year)- [I shortened the variable list to simplify the presentation but the same problem arises with all of your variables.] Stata looks to find variables named sex# and age#, where # is any number. But you have no such variables. You have sex_98 and sex_06, but _98 and _06 are not numbers. So you have a few ways around this.

      You can rename all your variables to eliminate the underscores ( _ ) that separate their "stem" from the year number and then use your -reshape- command the way you originally wrote it.

      Or, you can do it as
      Code:
      reshape long sex age, i(findid) j(year) string
      destring year, ignore("_"), replace
      Added: Crossed with #2 which shows yet another way.

      Comment


      • #4
        Thank you a lot, it worked perfectly.

        Comment

        Working...
        X