Announcement

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

  • first (and second, and eventually third) non empty variables

    Hello again,
    I have 30 variables A_1 - A_30, and I have to create a variable B1 that is equal to the first non empty variable among A_1 - A_30, and, if it exists, a B2 variable that is equal to the second non empty variable among A_1 - A_30. And eventually even a B3 variable, with the same logic.

    Thank you,

    Ylenia

  • #2
    Sounds very similar to your earlier request today. I might be wrong but it seems you are not sure what you really want, yet. Why don't you tell us more about your ultimate goals here so as to avoid the all-to-common XY Problem?

    Comment


    • #3
      It could be that

      Code:
      egen A = concat(A_1-A_30)
      split A, gen(B) limit(
      is enough. We can't tell without a realistic data example. Otherwise here is a completely untested sketch.

      Code:
      gen id = _n
      reshape long A_, i(id) j(which)
      gen bad = missing(A)
      sort id bad which
      by id : replace which = _n
      reshape wide A_, i(id) j(which)
      If this doesn't help, and you don't get a better answer, then as said we need a real(istic) data example.

      Comment


      • #4
        Hi Daniel,
        thank you for pushing me to question myself about what I really want, it is always useful.

        My previous question ended up being a silly "" problem, I was running the right code, that was not working probably because I am using a french keyboard. I copied and paste Andrew's code, that looked exactly like mine, and it worked.

        Again, I know how to get what I want, I am just trying to write a more compact code.
        I have a list of 30 string variables, most of the time the cell is empty, for a few observation I have 2 non empty variables, and for fewer I have 3. Hence I need to create up to 3 variables that tell me what is not missing. What I did so far was to concatenate my variables and then split them. It works, but I am pretty sure there is a better way to do that.

        Thank you,

        Ylenia

        Comment


        • #5
          Thank you Nick, your answer came while I writing to Daniel.

          So I guess I was already doing it right.

          Thanks again

          Comment


          • #6
            Nick Cox, is there a way to parce not on a specific string but on the content of a string variable? So split the var content according to the string in the var parcing in order to get
            "hola adios"
            "ciao adios"
            "yes bye ciao adios hola"
            "Thank you



            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input strL content str12 parcing
            "hello yes bye ciao hola adios" "ciao" 
            "hello hola yes ehy bye ciao adios" "bye"  
            "hello yes bye ciao adios hola" "hello"
            end

            Comment


            • #7
              Fine. The second line of code in #3 should be more like

              Code:
              split A, gen(B) limit(3)
              Last edited by Nick Cox; 29 Feb 2024, 10:33.

              Comment

              Working...
              X