Announcement

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

  • How to create new variables combining strings from two other columns?

    Hi,

    I am looking to create a new string variable that combines names from two different columns (also strings). This is what it looks like.
    HH_Head1 HH_Head2 New Variable (combined name list)
    Anna . Anna
    John . John
    . Henry Henry
    . Paul Paul
    Sam . Sam
    . Lucy Lucy

    Basically, I want to know how I can create the combined name list by using the names from HH_Head1 and HH_Head2.

    Thanks so much in advance for your help!

    Best, Claudia

  • #2
    sure:
    Code:
    gen new_var=HH_Head1 + HH_Head2
    
    *or
    
    gen new_var2=HH_Head1
    replace new_var2=HH_Head2 if mi(HH_Head1)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      If what you really want is a new variable that is HH_head1 if that is not missing and HH_head2 if that is not missing (and the conditions always mutually exclusive) then you can use:

      Code:
      gen newvar = cond(!mi(HH_Head1) & mi(HH_Head2), HH_Head1, HH_Head2)
      or you can just combine strings with:

      Code:
      g newvar2 = HH_Head1 + HH_Head2
      if the missing values you show (".") are actually just "" .
      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment


      • #4
        The most general workhorse here is the concat() function of egen.

        It allows, beyond the other commands in previous answers,

        * numeric to string conversions on the fly, including using value labels and/or specified display formats

        * different separators, such as spaces or commas.


        Comment


        • #5
          Thanks everyone! Very helpful!

          Comment

          Working...
          X