William Shakespeare
William Shakespeare

Reputation: 49

Display data in Stata loop

I have a loop in Stata 12 that looks at each record in a file and if it finds a flag equal to 1 it generates five imputed values. My code looks like this:

forvalues i=1/5 {
 gen y3`i' = y2
 gen double h`i' = (uniform()*(1-a)+a) if flag==1
 replace y3`i' = 1.6*(invibeta(7.2,2.6,h`i')/(1-invibeta(7.2,2.6,h`i')))^(1/1.7) if 
   flag==1
 } 

a is defined elsewhere. I want to check the individual imputations. Thus, I need to display the imputed variable preferably only for those cases where flag=1. I also would like to display another value, s, alongside. I need help in figuring out the syntax. I've tried every combination of quotes and subscripts that I can think of, but I keep getting error messages.

One other useful modification occurs to me. Suppose I have three concatenated files on which I want to perform this routine. Let them have a variable file equal to 1, 2 or 3. I'd like to set a separate seed for each and do it in my program so I have a record. I envision something like:

forvalues j=1/3 {
set seed=12345 if file=1
set seed=56789 if file=2
set seed=98765 if file=3

insert code above

}

Will this work?

Upvotes: 1

Views: 381

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

A subsidiary question asks about choosing a different seed each time around a loop. That is easy:

  forval j = 1/3 { 
       local seed : word `j' of 12345 56789 98765 
       set seed `seed' 
       ...
  }

or

  tokenize 12345 56789 98765 
  forval j = 1/3 { 
         set seed ``j'' 
         ...
  } 

Upvotes: 0

Nick Cox
Nick Cox

Reputation: 37208

No comment is possible on code you don't show, but the word "display" may be misleading you.

  list y3`i' if flag == 1 

or some variation may be what you seek. Note that display is geared to showing at most one line of output at a time.

P.S. As you are William Shakespeare, know that the mug http://www.stata.com/giftshop/much-ado-mug/ was inspired by your work.

Upvotes: 3

Related Questions