Announcement

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

  • Concatenating string observations

    Assume the following sample dataset:
    var1 var2 var3
    A 1 A
    B 0 B
    C 1 BC
    D 1 D
    E 0 E
    F 0 EF
    G 1 EFG
    I would like to get the concatenation seen in var3. The rule is simple: if var2 of the previous observation equals 0, then add the character of var1 before the string and output it concatenated. Or from a different perspective: if var2 is 0, then save the character of var1, and add it before the string in the next line. I've managed to come up with a solution, but to me it seems to be clumsy (therefore not necessarily robust), so I'd much appreciate your thoughts!

  • #2
    Code:
    clear 
    input str1 var1    var2 str3 var3
    A    1    A
    B    0    B
    C    1    BC
    D    1    D
    E    0    E
    F    0    EF
    G    1    EFG
    end 
    
    gen wanted = var1 
    replace wanted = wanted[_n-1] + wanted if var2[_n-1] == 0 
    
    list , sep(0)
    
         +-----------------------------+
         | var1   var2   var3   wanted |
         |-----------------------------|
      1. |    A      1      A        A |
      2. |    B      0      B        B |
      3. |    C      1     BC       BC |
      4. |    D      1      D        D |
      5. |    E      0      E        E |
      6. |    F      0     EF       EF |
      7. |    G      1    EFG      EFG |
         +-----------------------------+

    Comment


    • #3
      Nick Cox Nice, thanks! I could only solve it in three lines, but yours is way better!
      Last edited by Kazi Bacsi; 19 Sep 2026, 17:46.

      Comment


      • #4
        Kazi Bacsi I have a two line solution, if you show me yours I will show you mine.

        Comment


        • #5
          Dirk Enzmann I had the following, but I was thinking too much about the concept behind the data, so a "fresh eye" like Nick could simplify it.

          Code:
          gen var3 = var1 if !var2
          replace var3 = var3[_n - 1] + var3 if !missing(var3)
          replace var3 = cond(var2[_n - 1], "", var3[_n - 1]) + var1 if missing(var3)
          Last edited by Kazi Bacsi; 19 Sep 2026, 17:46.

          Comment


          • #6
            Very similar to the solution by Nick Cox:
            Code:
            clear
            input str1 var1 var2
            "A" 1
            "B" 0
            "C" 1
            "D" 1
            "E" 0
            "F" 0
            "G" 1
            end
            
            gen var3 = ""
            replace var3 = cond(var2[_n-1]==0,var3[_n-1]+var1[_n],var1[_n])
            which will produce
            Code:
            . gen var3 = ""
            (7 missing values generated)
            
            . replace var3 = cond(var2[_n-1]==0,var3[_n-1]+var1[_n],var1[_n])
            variable var3 was str1 now str3
            (7 real changes made)
            
            . list
            
                 +--------------------+
                 | var1   var2   var3 |
                 |--------------------|
              1. |    A      1      A |
              2. |    B      0      B |
              3. |    C      1     BC |
              4. |    D      1      D |
              5. |    E      0      E |
                 |--------------------|
              6. |    F      0     EF |
              7. |    G      1    EFG |
                 +--------------------+

            Comment


            • #7
              BTW, let's get the terminology straight. In Stata an observation is an entire row, record or case in a dataset, with values on one or more variables. The question is perfectly clear, but it's about concatenation of string values across observations.

              If you came here because of the thread title, https://journals.sagepub.com/doi/pdf...36867X20909698 is a reference. It doesn't cover the rule here, which in turn is clear but puzzling without a substantive story.

              Comment

              Working...
              X