Statalist


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

st: RE: file write + if condition?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: file write + if condition?
Date   Mon, 17 Nov 2008 17:55:47 -0000

The point you raise was discussed in a thread started last week by Ashim
Kapoor. As you say, the -if- command you use 

if id = `id' 

which should be 

if id == `id' 

is interpreted as 

if id[1] == `id' 

This behaviour is documented in various places, among them [P] if and 

FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if
qualifier
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J.
Wernow
        6/00    I have an if command in my program that only seems
                to evaluate the first observation, what's going on?
                http://www.stata.com/support/faqs/lang/ifqualifier.html

It is not a case of -file- not allowing -if-. There is no way that a
command can prohibit your writing another command before it. 

Your syntax is perfectly legal: it just does not do what you want. My
guess is that you want 

      if id[`i'] == `id' {

By the way, if you only want minimum and maximum from a -summarize-, use
the -meanonly- option [sic]. 

SJ-7-3  st0135  . . . . . . . . . . . Stata tip 50: Efficient use of
summarize
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        Q3/07   SJ 7(3):438--439                                 (no
commands)
        tip on using the meanonly option of summarize to more
        quickly determine the mean and other statistics of a
        large dataset

Nick 
[email protected] 

Even Bergseng

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?


*
*   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