Announcement

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

  • Creating a new variable with all possible combinations of two existing variables

    Hi,

    I would like to create a new variable that will have a unique values depending on the combination of two of my variables (i.e. I want a new variable at the intersection of my two existing variables). More specifically, I want to combine my Gender variable that is equal to 1 if male and 2 if female and I my Year of Labour Market entry variable that takes values from 1978 to 2011 into a new variable so I can then use this new variable for standard error clustering. I wanted to ask whether there is a quick way of telling Stata to do this rather than typing all possible combinations of the two variables and hence avoiding the risk of making a mistake.

    Thanks in advance.

  • #2
    If I read you correctly, then maybe something like
    Code:
    version 13.1
    
    clear *
    set more off
    
    set obs `=2011 - 1978 + 1'
    generate int year = 1978 - 1 + _n
    expand = 2
    bysort year: generate byte sex = _n
    
    exit

    Comment


    • #3
      Joseph's code creates a data set with two variables representing all combinations of year (1978 through 2011) and sex. If Christina actually already has a data set containing the year and sex variables and wants to get a new variable that distinctly identifies each combination of them, then it would be
      Code:
      egen newvar = group(year sex)

      Comment


      • #4
        thanks a lot to both. I already have the year and sex variables in my data set so I will go with Clyde's suggestion and keep Joseph's suggestion in mind for the future.
        Thanks again.

        Comment

        Working...
        X