Announcement

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

  • Command works in command window, not in do file

    Hi,
    I am trying to drop blank observations (those observations which are missing an email address in the datafile).

    When I add the command 'drop if email == ""' to the do-file and run the file, it returns 'invalid name'.

    When I type the same command directly into the command window, it drops the unwanted observations successfully.

    I am at a loss as to what I am doing wrong, as I am typing (and retyping) the same command, using the same keyboard, each time. I have tried searching here and online, but the keywords I am using seem too common to find anything relevant.

    The output is:

    drop if email == “”
    “” invalid name
    r(198);

    end of do-file

    r(198);

    . drop if email == ""
    (11 observations deleted)

    Many thanks.





  • #2
    use QUOTATION MARK
    Code:
    drop if email == ""
    not LEFT DOUBLE QUOTATION MARK and RIGHT DOUBLE QUOTATION MARK:
    Code:
    drop if email == “”
    Last edited by Bjarte Aagnes; 07 Aug 2020, 04:23.

    Comment


    • #3
      Thanks for the response Bjarte. I understand what you mean, inasmuch as the output is shown as “”.

      But I'm typing the same thing both times, using the ordinary quotation mark on a Mac keyboard [shift + '].

      The code in the do file is:

      drop if email == ""

      So how do I stop it from being returned in the output as “” ?

      Comment


      • #4
        You prepared your do-file using Microsoft Word, or TextEdit, or some other editor that has its preferences set to "smarten" quotation marks by substituting typographer's quotes for standard keyboard quotes. You may have typed
        Code:
        drop if email == ""
        into your do-file, but the editor changed it to
        Code:
        drop if email == “”

        For any one of a number of reasons, including this, you are better advised to prepare your do-file in Stata's Do-File editor. Otherwise, in the editor you are using, you need to turn off the "smart quotes" or "typographical quotes" preference.

        Let me add a related warning as long as we're talking about quotation marks.

        To have Stata substitute the value of a local macro
        Code:
         shortcut
        you need to type on your keyboard
        Code:
         `shortcut'
        where the first character is (on an American English keyboard) the "left single quote" beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key. Neither of these are "smart quotes" as understood in programs like Microsoft Word, and in particular the first character is correctly titled the "grave accent".

        Note that the Stata PDF documentation unfortunately displays local macro references incorrectly using smart single quotes as
        Code:
         ‘shortcut’
        which leads to no end of confusion, especially because such code will not work if copied and pasted into Stata. On the other hand, it does look elegant in the printed documentation.
        Last edited by William Lisowski; 07 Aug 2020, 05:10.

        Comment


        • #5
          Hi William,

          I prepared the do file in the do-file editor.

          I have repeatedly rewritten the command in the do file editor as I try to understand what is going on.

          Comment


          • #6
            If you copy code the OS smart quote settings may be set to convert to left/rigth quotes (not a Stata issue) .

            Comment


            • #7
              I have typed this code from scratch. I have not copied it.

              I have used exactly the same keyboard, working in the command editor and the do-file editor, at the same time.

              Comment


              • #8
                I agree with Bjarte that the problem may be at the operating system level. However, while on macOS there is a System Preference to globally use smart quotes (the effect is limited to apps that use the macOS built-in text editing APIs), turning it on does not seem to affect Stata on my system, but if it is on on your system, turn it off.

                Alternatively, do you have any thrid-party extensions installed that may affect text input?

                Contact Stata Technical Services if you don't find something that helps; perhaps they can advise. The have in past diagnosed an obscure problem of mine in the import delimited dialog box as caused by a third-party window manager, so they have some ideas about what is going on under the hood of macOS.

                Comment


                • #9
                  Thanks William, I've tried changing that setting under Keyboard, but it hasn't made any difference. As far as I know I don't have any third-party extensions, as I've cleaned pretty much every possible app off my Mac.

                  I'll try Stata Technical Services.

                  Thanks for your help.

                  Comment


                  • #10
                    I contacted Stata Technical Services. Their analysis was similar to this (smartquotes).

                    Their suggestion was to create a new user on the computer (like this: https://www.imore.com/how-create-new...count-your-mac) and then use this to test if it was a Smart Quote problem. This showed that it was.

                    I then tried a series of different changes to turn of Smart Quotes (there are several ways and I'm not sure which one ultimately made the difference - full list here: https://apple.stackexchange.com/ques...e-smart-quotes)

                    Thanks William and Bjarte.

                    Comment

                    Working...
                    X