Announcement

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

  • bysort query issues

    Hello all:

    I am trying to replace first row of path with "CLL" and second row of path as "RT" for _merge == 2 cases. The code is replacing both rows with either CLL or RT. What am I doing wrong? studyid 4 and 5 would be examples.

    bys studyid: replace path[`1'] = "CLL" if _merge == 2
    bys studyid: replace path[`_N'] = "RT" if _merge == 2 // not working

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int studyid str55 path byte _merge
     1 "Normal"       3
     1 "CLL"          3
     1 "CLL"          3
     1 "CLL"          3
     1 "CLL/PL"       3
     1 "CLL"          3
     1 "CLL"          3
     1 "CLL"          3
     1 "CLL"          3
     2 "CLL"          3
     3 "CLL"          3
     3 "Mixed CLL-RT" 3
     3 "Mixed CLL-RT" 3
     3 "Mixed CLL-RT" 3
     4 ""             2
     4 ""             2
     5 ""             2
     5 ""             2
     6 "CLL"          3
     6 "Mixed CLL-RT" 3
     6 "CLL"          3
     6 "CLL"          3
     6 "CLL"          3
     6 "CLL"          3
     6 "RT"           3
     6 "CLL"          3
     7 "Mixed CLL-RT" 3
     8 ""             2
     8 ""             2
     9 "CLL"          3
     9 "CLL"          3
     9 "CLL"          3
     9 "CLL"          3
    10 ""             2
    10 ""             2
    11 ""             2
    11 ""             2
    12 "Normal"       3
    12 "CLL"          3
    12 "Normal"       3
    12 "RT"           3
    12 "Normal"       3
    12 "Normal"       3
    12 "Normal"       3
    13 "CLL"          3
    13 "CLL"          3
    14 "Other"        3
    14 "Other"        3
    14 "Other"        3
    14 "Other"        3
    15 "CLL"          3
    15 "Mixed CLL-RT" 3
    15 "CLL"          3
    15 "CLL"          3
    15 "CLL"          3
    16 "Mixed CLL-RT" 3
    16 "Mixed CLL-RT" 3
    16 "CLL"          3
    16 "CLL"          3
    17 "CLL"          3
    17 "Mixed CLL-RT" 3
    17 "CLL"          3
    18 "CLL"          3
    18 "RT"           3
    18 "Normal"       3
    18 "Normal"       3
    18 "."            3
    18 "Normal"       3
    18 "Normal"       3
    19 "CLL"          3
    19 "CLL"          3
    19 "CLL"          3
    20 ""             2
    20 ""             2
    21 "Normal"       3
    22 ""             2
    22 ""             2
    23 "CLL"          3
    23 "Normal"       3
    23 "CLL"          3
    23 "CLL"          3
    23 "CLL"          3
    23 "CLL"          3
    23 "CLL"          3
    23 "CLL"          3
    23 "CLL"          3
    24 "."            3
    24 "CLL"          3
    24 "CLL"          3
    25 "CLL"          3
    25 "."            3
    25 "Mixed CLL-RT" 3
    25 "Mixed CLL-RT" 3
    26 "RT"           3
    26 "CLL"          3
    26 "CLL"          3
    26 "CLL"          3
    26 "Mixed CLL-RT" 3
    27 "Normal"       3
    27 "CLL"          3
    end
    label values _merge _merge
    label def _merge 2 "Using only (2)", modify
    label def _merge 3 "Matched (3)", modify

  • #2
    Girish:
    try it again with -[1]- and -[_N]- when appropriate.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Girish:
      try it again with -[1]- and -[_N]- when appropriate.
      Thanks Carlo. That is what I did initially and it threw the error:

      weights not allowed
      r(101);


      Ended up googling a bit and found the thing about adding `' quotes and the error disappeared. But the code is faulty still.

      Comment


      • #4
        Girish:
        you may want to try:
        Code:
        bysort studyid: replace path="CLL" if _merge==2 & _n==1
        Then replace "CLL" with "RT" and 1 with 2 when appropriate.

        Caveat emptor: this code was not tested.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Girish:
          you may want to try:
          Code:
          bysort studyid: replace path="CLL" if _merge==2 & _n==1
          Then replace "CLL" with "RT" and 1 with 2 when appropriate.

          Caveat emptor: this code was not tested.
          Tested. Your code works. Strange that the earlier code would not work even though it seemed so logical to me. Thanks again.

          Comment


          • #6
            Strange that the earlier code would not work even though it seemed so logical to me.
            Logic has no direct relationship to syntactic correctness, not in computer languages, nor in natural ones. Stata's syntax rules do not allow explicit subscripting on the left hand side of =, only on the right hand side. When Stata interprets commands it does not in any way attempt to figure out what they mean nor if they make sense in any context. It just tries to interpret it in accordance with the syntax rules of the language. You can also write commands that are syntactically perfect but utterly meaningless.

            The same is true in natural languages. "Colorless green ideas sleep furiously" is Noam Chomsky's famous example of perfectly well-formed English syntax that makes no sense at all. In the other direction, "I want to know if or not John is coming." has an obvious meaning, and is even quite similar in structure and sense to "I want to know whether or not John is coming." Yet the former is syntactically incorrect and the latter is just fine. (Example shamelessly stolen from Donfeng Wu.)

            Comment


            • #7
              I never figured I'd see Chomsky quoted on Statalist, which perhaps should be more common given that we discuss computing and syntax.

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                Logic has no direct relationship to syntactic correctness, not in computer languages, nor in natural ones. Stata's syntax rules do not allow explicit subscripting on the left hand side of =, only on the right hand side. When Stata interprets commands it does not in any way attempt to figure out what they mean nor if they make sense in any context. It just tries to interpret it in accordance with the syntax rules of the language. You can also write commands that are syntactically perfect but utterly meaningless.

                The same is true in natural languages. "Colorless green ideas sleep furiously" is Noam Chomsky's famous example of perfectly well-formed English syntax that makes no sense at all. In the other direction, "I want to know if or not John is coming." has an obvious meaning, and is even quite similar in structure and sense to "I want to know whether or not John is coming." Yet the former is syntactically incorrect and the latter is just fine. (Example shamelessly stolen from Donfeng Wu.)
                That syntax rule is useful to know before I craft my commands. Love your analogy. I am embarrassed to say I had to read the line twice to get the gist. Clearly, I need to read more literature to be able to quote like you do. Will start with Noam Chomsky, although I still have to get beyond the 50 pages of Sapiens by YNH that I have read so far.

                Comment

                Working...
                X