5

I have been doing some statistics work with Stata recently and not enjoying it very much.

It doesn't feel to me like it's a "proper" programming language: in particular I don't think there's a way to loop until a condition is met.

Am I right in my feeling, or is Stata truly Turing-complete?

Tom Smith
  • 1,659
  • 1
  • 15
  • 20

5 Answers5

3

I've never heard of Stata before but the webpage brags that it has "if, while" and "looping and branching".

Wikibooks has this example:

local k = 1
file open myfile using toto.txt, read text
file read myfile line
while r(eof) == 0 {
    local k = `k' + 1
    di "`k' `line'"
    file read myfile line
    }
file close myfile

I don't know what "proper" programming language means but at first glance it definitely appears to be Turing-complete.

Ken
  • 1,159
  • 1
  • 9
  • 13
1

A "proper" programming language in the sense that you could build a webpage or GUI with it? Of course not. But that's a bit extreme. You can certainly write loops with .ado and .do files; i would say it is turing complete.

idclark
  • 948
  • 1
  • 8
  • 27
  • 1
    You "could" build a webpage or GUI with it, though it's not an ideal platform for doing so & its pretty limited. E.g., I can write the html for my webpage w. the -file- commands, write tables for my webpage using -estout- or -tabout- (from SSC), or write GUIs within the Stata environment using the -window- subcommands. See the program/gui written from Stata (using Stata Numerics) for ADePt (http://tinyurl.com/stataadept) on the World Bank site. – eric.a.booth Dec 29 '10 at 22:11
1

Stata's ado language has all the usual conditional statements:

However, it is important that one does not confuse the two:

In addition, Stata's ado language loop constructs include:

Mata, Stata's matrix programming language also supports:

Consequently, both Stata's ado and mata programming languages appear to satisfy the criteria for being characterized as Turing complete.

It is important to note though that these are not general programming languages, but full-fledged statistical languages.

0

While you can use the -while-, -if, -else- commands to perform looping until a condition is met, it's usually a better idea in Stata to use the -foreach- or -forvalues- loops in their place.
So, instead of saying:

while "`1'" != "" {
<do something>
} 

or

if "`a'" == "" {
<do something>
}
else {
<do something else>
}

it's usually better (and more intuitive) to instead to do:

forvalues x = 1/100 {
<do something>
}

-- No -if-, -else-, or -break- conditions needed. See -help forvalues- or -help foreach- in Stata for details.


^NOTE: the while-else loop in my original post was removed--thanks for the heads-up, Keith. The -else- part was intended for the if{] else{} loop example only. Regardless, the point of my post wasn't to suggest the use of a while/else or if/else loop, it was that -foreach-/-forvalues- are usually a preferred approach.

sth
  • 222,467
  • 53
  • 283
  • 367
eric.a.booth
  • 651
  • 4
  • 7
0

@eric.a.booth: I think your example is strange. I'm not sure I've ever seen while { ... } else {...}

Also, note that Stata doesn't test the loop before you run it, and will allow itself to get caught in an infinite loop.

local x = 0
while `x'<5 {
   display `x' / 2
   local ++x
}
Keith
  • 1,037
  • 6
  • 13
  • I fixed the while-else mistake in my post, thanks again. I meant to have the -else- with the -if- example only.

    For your second comment, I don't see how your example would put Stata into an infinite loop (?) If you left out the macro expansion (++i or a -macro shift- or even a -continue, break- ) command then it would go into a infinite loop, but that would be in contrast to the guidelines of the user manual in [P]-while-.

    – eric.a.booth Dec 29 '10 at 22:03
  • I was trying to give a helpful example. Thus, I was pointing out the need to use something like ++x (to avoid the infinite loop). – Keith Jan 08 '11 at 22:05