Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: -generate- and run time contingencies

From   Mike Lacy <>
Subject   st: -generate- and run time contingencies
Date   Mon, 30 Jan 2006 08:51:21 -0700

One feature of Stata with which I continue to experience difficulty concerns how to assign a value to a variable based on a run time contingency. As a simple illustration, consider the goal of assigning a uniform r.v. value to a variable, while rejecting values less than, say, 0.8. If -while- was sensitive to run time values of variables, I would do:

gen x = uniform()
while (x < 0.8) {
replace x = uniform()

Obviously, this construct that would work in many languages does not work as desired in Stata, and just as obviously, there are other ways to accomplish the same end in Stata [ gen x = 0.8 + 0.2*uniform() ].

My question, then, concerns a general approach to these run-time contingencies, not all of which suggest simple workarounds to me. What seems most straightforward but clumsy as a generic approach is to do the code fragment involving the contingency as a matrix operation, and then save it back to a variable, e.g.,

matrix X = J(_N, 1, .)
local top = rowsof(X)
forvalues i = 1/`top'{
scalar temp = uniform()
while temp < 0.8 {
scalar temp = uniform()
matrix X[`i',1] = temp
svmat X, name(MyVarName)

Any generic suggestions here?


Mike Lacy
Fort Collins CO USA
(970) 491-6721 office

* For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index