Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: file write + if condition?


From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: file write + if condition?
Date   Mon, 17 Nov 2008 13:08:39 -0500

At 12:05 PM 11/17/2008, Even wrote:
I am using the - file write - command to write a series of lines to a text file. The text file is used as a parameter file for an external program.

The text file should contain information on several variables for several different observations and I thus like to loop over observations and use an - if - condition to write information from the correct observation in each line. Hovwever, the - file write - command does not seem to allow - if - conditions. I have tried using the - if - command with code along these lines:

   tempname FILE
   file open `FILE' using test.ajo, write replace
     sum id
     local minid = r(min)
     local maxid = r(max)
   forvalues i = `minid'(1)`maxid' {
      if id=`id' {
        file write `FILE'   %7.1f   (species)   _n
        }
     }
[...]

Nick gave some excellent advice; I was also going to suggest that you really want
   if id[`i'] == `id' {

However, it is not clear what `id' is (i.e., what the local id holds). Was it defined previously? Possibly, you want a nested loop, one being forvalues i , as you have it, the other being a foreach on id.

HTH
--David

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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