Announcement

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

  • Collapsing different variables in one variable

    I have different variables, resulting from generating new variables with regexs and regexm. Since these numbers I was looking for have different formats, I created redundant values.
    Ref3 Ref4 Ref5 Ref6
    Ref. 2589 Ref. 258
    Ref. 6265 Ref. 626
    Ref. 3403 Ref. 340 Ref. 34

    My goal is to create a new variable where only the longest string or the longest number in one row appears. Any help kindly appreciated!
    RefNew
    Ref. 2589
    Ref. 6265
    Ref. 3403
    Last edited by Vincenz Jahn; 05 Jun 2017, 10:42.

  • #2
    There's probably a half dozen ways to do this. My first crack at it would be

    Code:
    gen RefNew =""
    forvalues i = 3/6 {
    replace RefNew = Ref`i' if strlen(Ref`i')>strlen(Ref`i')
    }
    I am assuming you have a larger dataset than the one you presented, so it's not always the case that Ref3 is the longest variable.

    Comment


    • #3
      I think David meant
      Code:
        replace RefNew = Ref`i' if strlen(Ref`i')>strlen(RefNew)
      and it can be made slightly tighter with
      Code:
      gen RefNew = Ref3
      forvalues i = 4/6 {
        replace RefNew = Ref`i' if strlen(Ref`i')>strlen(RefNew)
        }

      Comment


      • #4
        Also posted at http://www.statalist.org/forums/foru...y-of-variables with a different answer given.

        Comment


        • #5
          Thank you for your help! Works perfectly

          Comment

          Working...
          X