Statalist


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

Re: st: if not iffing in ado


From   "Eva Poen" <eva.poen@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: if not iffing in ado
Date   Thu, 9 Oct 2008 09:18:30 +0100

Paul,

please always include where you got user written commands from when
posting to the list. The -swapval- I found is by Nick Cox and can be
found on ssc. Note that -swapval-'s [in] syntax is non-standard.

Now, I'm not sure what you want to achieve with your if statement.
Your syntax reads

syntax [if]

but then you don't make use of `if' anywhere in your program. Later
you test it with

local s=1
swap if `s' ==1

However, this is not going to work for two reasons:
a) you didn't make use of `if' in your program;
b) `s' is a local and not a variable. (You are confusing the if
qualifier with the -if- programming command).

So, here is a solution:

program swap
 version 10
 syntax [if]
 swapval iud1 iud2 `if'
 swapval r1 r2 `if'
 swapval se1 se2 `if'
end

Test it:

gen x = uniform()
swap if x > 0.5

If you want to use the -if- programming command:

local s = 1
if `s' == 1 {
   swap
}

HTH,
Eva

2008/10/9 Paul O'Brien <paul.obrien@afe2.org.uk>:
> i have a small program to temporarily swap variables using swapval. but i
> cannot get the 'if' to work.
>
> program swap
> // used to swap treatment and control (iud r and se)
> version 10
> syntax [if]
> swapval iud1 iud2
> swapval r1 r2
> swapval se1 se2
> ...
> end
>
> . local s=1
> . swap if `s' ==1
> (79 missing values generated)
> (84 missing values generated)
>
> . list r1 r2 in 1
>
>     +----------+
>     |  r1   r2 |
>     |----------|
>  1. | 2.8    3 |
>     +----------+
>
> ...
> that's fine, but setting s to 0 still produces a swap
>
> . local s=0
>
> . swap if `s' ==1
> (79 missing values generated)
> (84 missing values generated)
>
> . list r1 r2 in 1
>
>     +----------+
>     | r1    r2 |
>     |----------|
>  1. |  3   2.8 |
>     +----------+
>
> dropping the program does not help.
>
> what am i doing wrong?
>
> thanks.
>
> paul
>
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index