Announcement

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

  • Combine String Observations

    Hello Stata-Users,

    I'm very new to stata and I've looked for this problem but seem to find only the combination of string variables not observations.

    My data set has an ID(which is type:double) and then performancetypes(which is type: str6) which can take several forms and I want to combine them into one.


    input double grantid str6 performancetype
    1345976 "Abs"
    1345976 "Rel"
    1345976 "Time"
    1345977 "Abs"
    1345977 "Rel"
    1345977 "Time"
    1345978 "Rel"
    1345978 "Time"
    1345979 "Rel"




    Desired Output:

    ID Performancetype
    1345976 "AbsRelTime"
    1345977 "AbsRelTime"
    1345978 "RelTime"
    1345979 "Rel"

    I don't know if this is only possible with collapse command, however if this would work otherwise that`d be great. Because there are several more variables in the data set which should not get lost.

    I have tried the following command:
    replace performancetype="AbsRelTime" if grantid==grantid & performancetype=="Abs" & performancetype=="Rel" & performancetype=="Time"

    But first I don't think this is a clean way to do it, cause i would have to do this with each combination possibility and the error code type mismatch r(109) occurs.



    Your help would be very much appreciated.

  • #2
    Perhaps this accomplishes what you want. I've added an extra variable to show that this techniques does not lose other variables.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double grantid str6 performancetype float other
    1345976 "Abs"  23
    1345976 "Rel"  34
    1345976 "Time" 45
    1345977 "Abs"  56
    1345977 "Rel"  67
    1345977 "Time" 78
    1345978 "Rel"  89
    1345978 "Time" 90
    1345979 "Rel"  12
    end
    
    generate seq = _n
    generate pt str16 = ""
    bysort grantid (seq): replace pt = pt[_n-1]+performancetype
    bysort grantid (seq): replace pt = pt[_N]
    drop seq
    list, sepby(grantid) abbreviate(16)
    Code:
    . list, sepby(grantid) abbreviate(16)
    
         +------------------------------------------------+
         | grantid   performancetype   other           pt |
         |------------------------------------------------|
      1. | 1345976               Abs      23   AbsRelTime |
      2. | 1345976               Rel      34   AbsRelTime |
      3. | 1345976              Time      45   AbsRelTime |
         |------------------------------------------------|
      4. | 1345977               Abs      56   AbsRelTime |
      5. | 1345977               Rel      67   AbsRelTime |
      6. | 1345977              Time      78   AbsRelTime |
         |------------------------------------------------|
      7. | 1345978               Rel      89      RelTime |
      8. | 1345978              Time      90      RelTime |
         |------------------------------------------------|
      9. | 1345979               Rel      12          Rel |
         +------------------------------------------------+

    Comment


    • #3
      William Lisowski, Thank you so much!
      It worked perfectly!

      Comment

      Working...
      X