Announcement

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

  • Strange behavior. Syntax-related?

    I'm running a do-file (too long to reproduce here) that defines a program dfbounds and am experiencing what seems to me to be very odd behavior. I'm 99.9% certain that the same code had no problem running previously but can't be absolutely sure this is correct.

    The top of the code that defines the program is
    Code:
    cap prog drop dfbounds
    
    prog define dfbounds
    
     version 17.0
    
     syntax varlist [if] [in]  [, savedat(string)]
    I then ran the program using auto.dta

    Code:
    sysuse auto
    dfbounds rep78 foreign
    dfbounds rep78 foreign if mpg>20, savedat(testdat, replace)
    dfbounds rep78 foreign, savedat(testdat, replace)
    The results were as follows
    Code:
    . sysuse auto
    (1978 automobile data)
     
    .
    . dfbounds rep78 foreign
     
      [output suppressed but program executed properly]
     
     
    . dfbounds rep78 foreign if mpg>20, savedat(testdat, replace)
     
      [output suppressed but program executed properly]
     
    file testdat.dta saved
     
    .
    . dfbounds rep78 foreign, savedat(testdat, replace)
    options not allowed
    r(101);
     
    end of do-file
     
    r(101);

    Thinking that perhaps my executable somehow got corrupted I ran this but the problem persisted

    Code:
    update all, force
    I'm at wits' end trying to figure out what's going on. Why does the program run properly with an if statement but not without an if statement?

    Any suggestions? Thanks very much in advance. Happy to buy a pint for the first person who figures this out.
    Last edited by John Mullahy; 16 Feb 2023, 09:48.

  • #2
    I can't reproduce your problem.
    Code:
    . clear*
    
    .
    . about
    
    Stata/MP 17.0 for Windows (64-bit x86-64)
    Revision 10 Jan 2023
    Copyright 1985-2021 StataCorp LLC
    
    Total physical memory:       32.00 GB
    Available physical memory:   20.39 GB
    
    Stata license: Single-user 4-core  perpetual
    Serial number: REDACTED
      Licensed to: Clyde Schechter
                   Albert Einstein College of Medicine
    
    .
    . cap prog drop dfbounds
    
    . prog define dfbounds
      1.      version 17.0
      2.      syntax varlist [if] [in]  [, savedat(string)]
      3.      display "varlist = `varlist'"
      4.      display "if qualifer = `if'"
      5.      display "in qualifier = `in'"
      6.      display "savedat = `savedat'"
      7.      exit
      8.  end
    
    .  
    . sysuse auto
    (1978 automobile data)
    
    . dfbounds rep78 foreign
    varlist = rep78 foreign
    if qualifer =
    in qualifier =
    savedat =
    
    . dfbounds rep78 foreign if mpg>20, savedat(testdat, replace)
    varlist = rep78 foreign
    if qualifer = if mpg>20
    in qualifier =
    savedat = testdat, replace
    
    . dfbounds rep78 foreign, savedat(testdat, replace)
    varlist = rep78 foreign
    if qualifer =
    in qualifier =
    savedat = testdat, replace
    
    .
    end of do-file
    Have you tried rebooting, or, if that fails, uninstalling and reinstalling Stata?
    Last edited by Clyde Schechter; 16 Feb 2023, 09:57.

    Comment


    • #3
      Thanks Clyde. Much appreciate the effort.

      Comment


      • #4
        For what it's worth the do-file that defines the program is here
        https://uwmadison.box.com/s/u2k7b3i9...9w6nhqcztoexhu

        I rebooted and the problem persisted.

        The problem also arose in my installation of v16.1.
        Last edited by John Mullahy; 16 Feb 2023, 10:14.

        Comment


        • #5
          Hi John
          The problem seems to be related with how you are calling the elements "fed" into the program

          Right now you are using positional parsing:
          `1' = rep78
          `2' = foreign,

          So that comma in `2' is what is throwing your "inspect" command.
          Perhaps you may want to use other approaches to read the variables. (gettoken or varlist?)

          Hope this helps

          Comment


          • #6
            Following FernandoRios''s nice catch in #5

            Code:
            tokenize "`varlist'"
            will map two variable names in varlist to local macros 1 and 2. Then the ensuing code seems valid.

            Comment


            • #7
              Thanks Fernando. I had no clue that -inspect- would work this way.

              Rather than changing the syntax I just added some temporary variables so as not to confuse -inspect- and now things seem to work fine.
              Code:
              cap prog drop dfbounds
              
              prog define dfbounds
              
              version 17.0
               
              syntax varlist [if] [in] [, savedat(string)]
              
              tempvar t1 t2
              
              clonevar `t1'=`1'
              clonevar `t2'=`2'
              
              qui inspect `t1'
              
              if (r(N) != r(N_0)+r(N_posint)) {
               di as error "Error: Specified outcome variable is not count-valued"
               exit
              }
              
              qui inspect `t2'
              
              if (r(N_unique) < 2) {
               di as error "Error: Specified classifying variable has fewer than 2 values"
               exit
              }
              
              if (r(N_unique) > 2) {
               di as error "Error: Specified classifying variable has more than 2 values"
               exit
              }
              
              . . .
              I indeed owe you a pint for your great detective work. The next time we are at the same venue please remind me and I will be happy to pay up. (I don't do Venmo so am unable to transfer the funds to you directly. 😀 )

              Comment


              • #8
                Thanks Nick. That's much more efficient than using my temporary variable fix.

                Unfortunately Fernando beat you to the punch on the free pint. But if all three of us find ourselves in the same venue I'll be happy to buy each of you a pint.

                Comment


                • #9
                  I drink wine rather than beer, so a glass will suffice, thank you!

                  Comment


                  • #10
                    Thanks! Cheers!

                    Comment

                    Working...
                    X