Announcement

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

  • Create string var of numeric values by two groups without collapse

    Using the data below, how can I create a variable without collapsing (or during a collapse) that produces a string variable of all values of -categ- within a given id-time grouping?



    Code:
    input byte(id time categ)
    1 1  4
    1 1  7
    1 2  3
    1 2  4
    2 1 13
    2 1  3
    2 1  5
    2 2  4
    2 2  1
    end

  • #2
    Code:
    gen wanted = string(categ)
    by id time (categ), sort: replace wanted = wanted[_n-1]  + " " + wanted ///
        if _n > 1
    by id time: replace wanted = wanted[_N]
    Because you refer both to avoiding -collapse- and using -collapse- it is unclear to me whether you want to end up with just one observation per id-time group, or retain the original data structure, with the same resulting value populating all of the observations in the id-time group. The code above gets you the latter. If you prefer the former, you can get there by further doing:
    Code:
    drop categ
    by id time: keep if _n == 1

    Comment


    • #3
      Code:
      search pr0071, entry
      to find a link to a 2020 paper discussing Clyde’s approach in more detail.

      Comment


      • #4
        Thanks both. My ultimate goal is to end up with one observation per time-id group, so the first approach works perfectly.

        Comment

        Working...
        X