Announcement

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

  • How to cumulate the contents of string variables by id?

    As far as I know, sum is used for numberic variables, how about the string variables?
    Here is the data:
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str54 Mediator float tempid
    "negAffect5"                                             1
    "posAffect4"                                             1
    "negAffect2 negAffect3"                                  1
    "negAffect2 negAffect5"                                  1
    "negAffect3 negAffect4"                                  1
    "negAffect3 negAffect5"                                  1
    "negAffect4 negAffect5"                                  1
    "negAffect1 negAffect3 negAffect5"                       1
    "negAffect2 negAffect3 negAffect4"                       1
    "negAffect2 negAffect3 negAffect5"                       1
    "negAffect2 negAffect4 negAffect5"                       1
    "negAffect3 negAffect4 negAffect5"                       1
    "negAffect1 negAffect2 negAffect3 negAffect4"            1
    "negAffect1 negAffect2 negAffect3 negAffect5"            1
    "negAffect1 negAffect3 negAffect4 negAffect5"            1
    "negAffect2 negAffect3 negAffect4 negAffect5"            1
    "negAffect1 negAffect2 negAffect3 negAffect4 negAffect5" 1
    "negAffect5"                                             2
    "posAffect4"                                             2
    "negAffect2 negAffect3"                                  2
    end

  • #2
    This question was addressed in an article last year https://www.stata-journal.com/articl...article=pr0071

    For string variables, technique starts with


    Code:
    gen wanted = given[1] in 1 
    
    replace wanted = wanted[_n-1] + given in 2/L

    with very often some variation such as

    Code:
    replace wanted = wanted[_n-1] + " " + given in 2/L
    Code:
    replace wanted = wanted[_n-1] + "," + given in 2/L

    Comment


    • #3
      Originally posted by Nick Cox View Post
      This question was addressed in an article last year https://www.stata-journal.com/articl...article=pr0071

      For string variables, technique starts with


      Code:
      gen wanted = given[1] in 1
      
      replace wanted = wanted[_n-1] + given in 2/L

      with very often some variation such as

      Code:
      replace wanted = wanted[_n-1] + " " + given in 2/L
      Code:
      replace wanted = wanted[_n-1] + "," + given in 2/L
      However, how to combined with tempid?
      It says ""'in' may not be combined with 'by' when using bys tempid.

      Comment


      • #4
        Under by: you need


        Code:
        ...  if _n == 1 
        
        ... if _n > 1

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Under by: you need


          Code:
          ... if _n == 1
          
          ... if _n > 1
          Thanks Nick! But this is still not correct. In the bellow example, I want to generate a varaible which includes the same contents for each tempid.
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str54 Mediator float tempid
          "negAffect5"                                             1
          "posAffect4"                                             1
          "negAffect2 negAffect3"                                  1
          "negAffect2 negAffect5"                                  1
          "negAffect3 negAffect4"                                  1
          "negAffect3 negAffect5"                                  1
          "negAffect4 negAffect5"                                  1
          "negAffect1 negAffect3 negAffect5"                       1
          "negAffect2 negAffect3 negAffect4"                       1
          "negAffect2 negAffect3 negAffect5"                       1
          "negAffect2 negAffect4 negAffect5"                       1
          "negAffect3 negAffect4 negAffect5"                       1
          "negAffect1 negAffect2 negAffect3 negAffect4"            1
          "negAffect1 negAffect2 negAffect3 negAffect5"            1
          "negAffect1 negAffect3 negAffect4 negAffect5"            1
          "negAffect2 negAffect3 negAffect4 negAffect5"            1
          "negAffect1 negAffect2 negAffect3 negAffect4 negAffect5" 1
          "negAffect5"                                             2
          "posAffect4"                                             2
          "negAffect2 negAffect3"                                  2
          "negAffect2 negAffect5"                                  2
          "negAffect3 negAffect4"                                  2
          "negAffect3 negAffect5"                                  2
          "negAffect4 negAffect5"                                  2
          "negAffect1 negAffect2 negAffect3"                       2
          "negAffect1 negAffect3 negAffect5"                       2
          "negAffect2 negAffect3 negAffect4"                       2
          "negAffect2 negAffect3 negAffect5"                       2
          "negAffect2 negAffect4 negAffect5"                       2
          "negAffect3 negAffect4 negAffect5"                       2
          "negAffect1 negAffect2 negAffect3 negAffect4"            2
          "negAffect1 negAffect2 negAffect3 negAffect5"            2
          "negAffect1 negAffect3 negAffect4 negAffect5"            2
          "negAffect2 negAffect3 negAffect4 negAffect5"            2
          "negAffect1 negAffect2 negAffect3 negAffect4 negAffect5" 2
          "negAffect5"                                             3
          "posAffect4"                                             3
          "negAffect2 negAffect3"                                  3
          "negAffect2 negAffect5"                                  3
          "negAffect3 negAffect4"                                  3
          end
          
          gen tempMediatorTotal = Mediator[1] in 1 if tempid == 1 
          replace tempMediatorTotal = tempMediatorTotal[_n-1] + Mediator in 2/L if tempid>1

          Comment


          • #6
            Sorry, but I don't understand what you want. Please back up and pose a much simpler equivalent problem in which you


            * show what you start with in terms of (say) A B C D

            * show what you want to get from it

            * explain your rules

            The question How to cumulate the contents of string variables says nothing about cumulating conditionally on what is inside the strings.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Sorry, but I don't understand what you want. Please back up and pose a much simpler equivalent problem in which you


              * show what you start with in terms of (say) A B C D

              * show what you want to get from it

              * explain your rules

              The question How to cumulate the contents of string variables says nothing about cumulating conditionally on what is inside the strings.
              Thanks Nick!
              I made a post here:
              https://www.statalist.org/forums/for...ables-by-group

              Comment


              • #8
                Originally posted by Nick Cox View Post
                This question was addressed in an article last year https://www.stata-journal.com/articl...article=pr0071

                For string variables, technique starts with


                Code:
                gen wanted = given[1] in 1
                
                replace wanted = wanted[_n-1] + given in 2/L

                with very often some variation such as

                Code:
                replace wanted = wanted[_n-1] + " " + given in 2/L
                Code:
                replace wanted = wanted[_n-1] + "," + given in 2/L
                Hi Nick, what does "L" mean in your syntax?
                Do you have a guide for such systematic macros?

                Comment


                • #9
                  For in L see the help:

                  Code:
                  help in
                  No Stata macros are used in #2 -- where this code appears -- so I don't understand the other question.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    For in L see the help:

                    Code:
                    help in
                    No Stata macros are used in #2 -- where this code appears -- so I don't understand the other question.
                    I thought L was a macro by mistake, thansk for your information!

                    Comment

                    Working...
                    X