Announcement

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

  • Generate unique Identifier from 2 variables

    Hello,

    I encountered the following problem:
    Out of 2 existing variables( a date and a variable consisting of numbers and letters) stored as str6 (format: %9s) and long (format: %tdDDNNCCYY) I need to create an identifier by combining these 2 variables.

    egen ident =concat(date var2)

    yields this error code: factor variables and time-series variables not allowed.

    Thanks a lot in advance!

  • #2
    Originally posted by CAPM View Post
    egen ident =concat(date var2)

    yields this error code: factor variables and time-series variables not allowed.
    No it doesn't.

    .ÿversionÿ13.1

    .ÿ
    .ÿclearÿ*

    .ÿsetÿmoreÿoff

    .ÿ
    .ÿinputÿstr6ÿvar2ÿlongÿdate

    ÿÿÿÿÿÿÿÿÿÿvar2ÿÿÿÿÿÿÿÿÿÿdate
    ÿÿ1.ÿ"abc123"ÿ19992
    ÿÿ2.ÿ"DEF456"ÿ19993
    ÿÿ3.ÿend

    .ÿ
    .ÿformatÿvar2ÿ%9s

    .ÿformatÿdateÿ%tdDDNNCCYY

    .ÿ
    .ÿegenÿidentÿ=concat(dateÿvar2)ÿ

    .ÿlist,ÿnoobs

    ÿÿ+---------------------------------+
    ÿÿ|ÿÿÿvar2ÿÿÿÿÿÿÿdateÿÿÿÿÿÿÿÿÿidentÿ|
    ÿÿ|---------------------------------|
    ÿÿ|ÿabc123ÿÿÿ26092014ÿÿÿ19992abc123ÿ|
    ÿÿ|ÿDEF456ÿÿÿ27092014ÿÿÿ19993DEF456ÿ|
    ÿÿ+---------------------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    Now, why don't you follow suit?

    Comment


    • #3
      I don't believe that! Your problem can't be reproduced:

      Code:
       
      . clear
      
      . set obs 1
      obs was 0, now 1
      
      . gen long date = mdy(10,7,2014)
      
      . gen var2 = "answer"
      
      . egen ident = concat(date var2)
      That works.

      This post is reminiscent of http://www.stata-forum.de/stata-synt...ugen-t872.html where you used quite a different syntax, which was illegal, which was why you got an error message.

      Please read FAQ Advice Section 5 on use of real names being preferred; Section 8 on cross-posting (you should tell us about it); and Section 12 on reporting exactly what you typed and exactly what happened.

      Comment


      • #4
        Your cross-post (I didn't know about it until Nick's post above) mentions matching, but maybe you mean merging? If so, then you can do that with the two variables as-is.
        Code:
        merge 1:1 var2 date using einem_anderen, keep(match)
        Provided, of course, that the two variables exist separately in the other dataset.
        Last edited by Joseph Coveney; 07 Oct 2014, 06:28.

        Comment


        • #5
          First of all thanks for your answer. Indeed I am a Stata beginner and am sry for the inconveniences. I will read the FAQs asap.

          To your answer: What I need is this:

          ÿ+---------------------------------+
          ÿÿ|ÿÿÿvar2ÿÿÿÿÿÿÿdateÿÿÿÿÿÿÿÿÿidentÿ|
          ÿÿ|---------------------------------|
          ÿÿ|ÿabc123ÿÿÿ26092014ÿÿÿ26092014abc123ÿ|
          ÿÿ|ÿDEF456ÿÿÿ27092014ÿÿÿ27092014DEF456ÿ|
          ÿÿ+---------------------------------+


          As can be seen in your example the date is displayed as a number instead:

          ÿ+---------------------------------+
          ÿÿ|ÿÿÿvar2ÿÿÿÿÿÿÿdateÿÿÿÿÿÿÿÿÿidentÿ|
          ÿÿ|---------------------------------|
          ÿÿ|ÿabc123ÿÿÿ26092014ÿÿÿ19992abc123ÿ|
          ÿÿ|ÿDEF456ÿÿÿ27092014ÿÿÿ19993DEF456ÿ|
          ÿÿ+---------------------------------+


          Is there a solution for this problem?

          Thanks and best regards!

          Comment


          • #6
            The format() option is provided for such problems. See the help for egen.

            Code:
             
            egen ident = concat(date var2), format(%tdDDNNCCYY)

            Comment


            • #7
              Thank you very much Nick. This makes my day!

              Comment


              • #8
                CAPM:
                please, make our day, too and, as Nick pointed out, re-register using your real names (as per FAQ). Thank you.

                Kind regards,
                Carlo
                Kind regards,
                Carlo
                (StataNow 18.5)

                Comment

                Working...
                X