Announcement

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

  • how to end string input in do file

    I have a Stata data set with 12 observations. I want to create a new string variable named group.

    From manuals, I think the .do file code should be:

    input str8 group
    c6_8
    c9_11
    f12_13
    f14_19
    f20_50
    f51_70
    f71
    m12_13
    m14_19
    m20_50
    m51_70
    m71
    end

    However, Stata gives error message: "command end is unrecognized" (I think because the .do file already reached the 12th observation and therefore had already exited the input command).

    So, I instead tried the code without "end" and it worked correctly. But the do-file editor in v17 may misinterpret all the subsequent lines of the do file (thinking that the input command is still open). I think this is a .do file editor problem rather than a Stata problem.

    You all may chuckle, figuring this was too minor to post, but let me mention that I like the Stata .do file editor and its color coding very much. I saves me a bunch of trouble generally, so losing its help for the rest of the .do file is melancholy.

  • #2
    Originally posted by Parke Wilde View Post
    You all may chuckle, figuring this was too minor to post, but let me mention that I like the Stata .do file editor and its color coding very much. I saves me a bunch of trouble generally, so losing its help for the rest of the .do file is melancholy.
    Welcome to the Statalist. Parke. You'll get no ridicule from me (or anyone else here) for asking a beginner question. Everyone was a beginner at some point. Many of us also like the do-file editor for its many conveniences, including myself.

    If I copy and paste your code into a do-file and execute it, I get a dataset with 12 observations. So, I cannot reproduce your error, unfortunately. This was using Stata 18 but I would be very surprised if Stata 17 acted any differently with -input-. I suspect you may have double-typed (or doubly executed) the -end- statement, since that would give you the exact error you report.

    Comment


    • #3
      You are kind to take the time.

      Here is code that produces the error. (My previous code only produced the error because I already has 12 obs in the loaded dataset).

      capture clear all
      set obs 3
      input str8 group
      entry1
      entry2
      entry3
      end

      Meanwhile, although I did not resolve this error, I found another easy way to accomplish my task of creating the variable, so please feel it is entirely optional for anybody to respond further.

      Thanks again!

      Parke

      Comment


      • #4
        When you're using the -input- command, Stata ordinarily expects (I think) that your data set is empty and that it will determine the number of observations by seeing how many lines there are before -end. When you indicated -set obs 3-, this somehow confused Stata when it saw more than 3 observations. If you had omitted the -set obs-, your code would work. Strangely enough, though, if you use (say) -set obs 10-, it turns out that Stata doesn't complain, and is *not* confused by the -end-, but does include some blank observations. Perhaps someone else can understand the inner logic there.

        By the way: When you're entering string values, it's standard and desirable to enclose them in double quotes, e.g. "entry1", etc. This would not have avoided your problem here, but in all contexts I can think of in Stata, not indicating a string value with quotes will lead to problems.

        Comment


        • #5
          Originally posted by Mike Lacy View Post
          When you're using the -input- command, Stata ordinarily expects (I think) that your data set is empty and that it will determine the number of observations by seeing how many lines there are before -end. When you indicated -set obs 3-, this somehow confused Stata when it saw more than 3 observations. If you had omitted the -set obs-, your code would work. Strangely enough, though, if you use (say) -set obs 10-, it turns out that Stata doesn't complain, and is *not* confused by the -end-, but does include some blank observations. Perhaps someone else can understand the inner logic there.

          By the way: When you're entering string values, it's standard and desirable to enclose them in double quotes, e.g. "entry1", etc. This would not have avoided your problem here, but in all contexts I can think of in Stata, not indicating a string value with quotes will lead to problems.
          Thank you very much for this explanation! It helped me solve the same problem.

          Comment

          Working...
          X