Announcement

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

  • Command does not work on Do-file, but can work on command box

    I have a command that I cannot run from do file, but if I copy paste to the Command box, it returns with result
    Code:
    /*AllList already imported*/
    list AllList
    1. banana
    2. apple
    3. canada
    4. anana
    /*input second list*/
    input str10 FruitList
    banana
    anana
    apple
    end
    levelsof FruitList, sep("$|^")
    /* check whether element is in local list */
    gen Check = ustrregexm(AllList, "\b^`FruitList'$\b")
    it is the last line that does not work from do file, gen Check = ustrregexm(AllList, "\b^`FruitList'$\b").If I run from do file, it returns with all "0"; but if I copy paste to Command and run, it does give me "1" for those that matched.

  • #2
    Accidental duplicate.
    Last edited by William Lisowski; 10 Sep 2022, 06:36.

    Comment


    • #3
      The following was run successfully from the Do-file Editor window.
      Code:
      clear
      input str10 FruitList
      banana
      anana
      apple
      end
      levelsof FruitList, clean sep("|") local(FruitList)
      macro list _FruitList
      
      clear
      input str10 AllList
      banana
      apple
      canada
      anana
      end
      
      gen Check = ustrregexm(AllList, "\b`FruitList'\b")
      list
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD06690.000000"
      
      . clear
      
      . input str10 FruitList
      
            FruitList
        1. banana
        2. anana
        3. apple
        4. end
      
      . levelsof FruitList, clean sep("|") local(FruitList)
      anana|apple|banana
      
      . macro list _FruitList
      _FruitList:     anana|apple|banana
      
      .
      . clear
      
      . input str10 AllList
      
              AllList
        1. banana
        2. apple
        3. canada
        4. anana
        5. end
      
      .
      . gen Check = ustrregexm(AllList, "\b`FruitList'\b")
      
      . list
      
           +-----------------+
           | AllList   Check |
           |-----------------|
        1. |  banana       1 |
        2. |   apple       1 |
        3. |  canada       0 |
        4. |   anana       1 |
           +-----------------+
      
      .
      end of do-file

      Comment


      • #4
        Originally posted by William Lisowski View Post
        The following was run successfully from the Do-file Editor window.
        Code:
        clear
        input str10 FruitList
        banana
        anana
        apple
        end
        levelsof FruitList, clean sep("|") local(FruitList)
        macro list _FruitList
        
        clear
        input str10 AllList
        banana
        apple
        canada
        anana
        end
        
        gen Check = ustrregexm(AllList, "\b`FruitList'\b")
        list
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD06690.000000"
        
        . clear
        
        . input str10 FruitList
        
        FruitList
        1. banana
        2. anana
        3. apple
        4. end
        
        . levelsof FruitList, clean sep("|") local(FruitList)
        anana|apple|banana
        
        . macro list _FruitList
        _FruitList: anana|apple|banana
        
        .
        . clear
        
        . input str10 AllList
        
        AllList
        1. banana
        2. apple
        3. canada
        4. anana
        5. end
        
        .
        . gen Check = ustrregexm(AllList, "\b`FruitList'\b")
        
        . list
        
        +-----------------+
        | AllList Check |
        |-----------------|
        1. | banana 1 |
        2. | apple 1 |
        3. | canada 0 |
        4. | anana 1 |
        +-----------------+
        
        .
        end of do-file
        Thanks William, it works. May I ask what does "macro" command works here? why creating a "macro list _FruitList" here?
        Thanks

        Comment


        • #5
          The code in post #1 was incorrect or misleading in several areas. In particular, nothing there would have created the local macro FruitList that is used in the regular expression argument of the ustrregexm() function.

          The levelsof command in post #2 uses the local() option to create the local macro FruitList.

          I used the macro list command to show precisely what is stored in the local macro FruitList (to list a local macro, the macro name is preceded with "_") so this shows that in the ustrregexm() function `FruitList' will be replaced by anana|apple|banana in the regular expression argument, and thus the generate command that is run would be
          Code:
          gen Check = ustrregexm(AllList, "\banana|apple|banana\b")
          although this probably would have been clearer with parentheses
          Code:
          gen Check = ustrregexm(AllList, "\b(anana|apple|banana)\b")

          Comment


          • #6
            Originally posted by William Lisowski View Post
            The code in post #1 was incorrect or misleading in several areas. In particular, nothing there would have created the local macro FruitList that is used in the regular expression argument of the ustrregexm() function.

            The levelsof command in post #2 uses the local() option to create the local macro FruitList.

            I used the macro list command to show precisely what is stored in the local macro FruitList (to list a local macro, the macro name is preceded with "_") so this shows that in the ustrregexm() function `FruitList' will be replaced by anana|apple|banana in the regular expression argument, and thus the generate command that is run would be
            Code:
            gen Check = ustrregexm(AllList, "\banana|apple|banana\b")
            although this probably would have been clearer with parentheses
            Code:
            gen Check = ustrregexm(AllList, "\b(anana|apple|banana)\b")
            Thanks again.
            And also what can I do if I have multiple FruitList to match the same AllList? Can I still use "levelsof" and "local" command again and again follow on?

            Comment


            • #7
              Here is example code that demonstrates using two lists of words to match.
              Code:
              clear
              input str10 FruitList
              anana
              apple
              end
              levelsof FruitList, clean sep("|") local(FruitList1)
              
              clear
              input str10 FruitList
              banana
              apple
              end
              levelsof FruitList, clean sep("|") local(FruitList2)
              
              clear
              input str10 AllList
              banana
              apple
              canada
              anana
              end
              
              macro list _FruitList1 _FruitList2
              
              generate Check = ustrregexm(AllList, "\b`FruitList1'\b")
              list
              
              replace  Check = Check | ustrregexm(AllList, "\b`FruitList2'\b")
              list
              Code:
              . macro list _FruitList1 _FruitList2
              _FruitList1:    anana|apple
              _FruitList2:    apple|banana
              
              . 
              . generate Check = ustrregexm(AllList, "\b`FruitList1'\b")
              
              . list
              
                   +-----------------+
                   | AllList   Check |
                   |-----------------|
                1. |  banana       0 |
                2. |   apple       1 |
                3. |  canada       0 |
                4. |   anana       1 |
                   +-----------------+
              
              . 
              . replace  Check = Check | ustrregexm(AllList, "\b`FruitList2'\b")
              (1 real change made)
              
              . list
              
                   +-----------------+
                   | AllList   Check |
                   |-----------------|
                1. |  banana       1 |
                2. |   apple       1 |
                3. |  canada       0 |
                4. |   anana       1 |
                   +-----------------+
              
              .
              Last edited by William Lisowski; 11 Sep 2022, 06:59.

              Comment

              Working...
              X