Announcement

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

  • Generate a variable containing the values of other variables

    Hi, I know this may seem simple but I just can't figure it out.

    I have 3 variables: Time, serial, code

    Time expresses years, serial is a identifier variable and code is a variable which ranges between 1 and 50.

    I want to generate a new variable (lets call it "nv") which contains the values of this 3 variables in this order: Time|serial|code

    So for example if I have the following:

    Time: 1999
    serial: 143
    code: 4

    My new variable should look like this:

    nv: 19991434



    Thanks for your help.

  • #2
    Juan Esteban:
    see -concat()- function under -egen-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Carlo Lazzaro is correct. My contribution (apart from writing this beast originally) is to suggest that

      Code:
      egen wanted = concat(Time serial code), p(" ")
      is a little more demanding of storage, but produces a more civilised result. Use punctuation other than spaces if more congenial.

      If you squeeze together results like that, one day it will bite you when "19991434" could be "1999 143 4" or "1999 14 34" but you have to struggle to work out which. Naturally in your present situation you may know that will not happen, but one day you will be pleased to remember this.

      Comment


      • #4
        Thank you so much for your help, both contributions are valuable to my work.

        Comment


        • #5
          Nick's code is more elegant than mine.
          But Nick's advice about keeping note (say, via a comment on a -.do- file) is even more relevant: it might well be the result of a (hopefully natural) ageing process (and related fears), but I'm more keen to use -.do- file to support my memory about what I've done months ago than I was in the past.
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment

          Working...
          X