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

st: file write + if condition?

From   Even Bergseng <>
To   "" <>
Subject   st: file write + if condition?
Date   Mon, 17 Nov 2008 18:05:04 +0100

Dear all!

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

but this only checks the first observation and stops. I can make it work using  - macro - which can take - if -conditions

  forvalues i = `minid'(1)`maxid' {
      sum species if treid==`l'
      local species = r(min)
      file write `FILE'   %7.1f   (`species')  _n

but for several variables for several observations, this seems to be a bad solution.

Any hints to how I can use - file write - and loop over observations or use the - if- condition?

best regards,

*   For searches and help try:

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