Statalist


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

st: SV: RE: file write + if condition?


From   Even Bergseng <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: SV: RE: file write + if condition?
Date   Mon, 17 Nov 2008 20:39:16 +0100

Ahhh. Thanks Nick and David!

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

was exactly what I was looking for.

Indeed, I did not pay proper attention to the thread started by Ashim Kapoor and I have been busted in not being thorough enough when checking neither the manual, FAQ or the web before contacting the list. Please except my apologies.

My point concerning the - file write - command, was that it does not except an - if - condition. However, this is no problem now that I know the - if - command can count!

regards,
Even


________________________________________
Fra: [email protected] [[email protected]] p&#229; vegne av Nick Cox [[email protected]]
Sendt: 17. november 2008 18:55
Til: [email protected]
Emne: st: RE: file write + if condition?

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/

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