Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Compact if/else assignment

    I'm wondering if it's possible to write an if/else variable assignment in one line. In R, I can write
    Code:
    var = ifelse( condition, value_if_T, value_if_F)
    or, alternatively,
    Code:
    if (condition) var = value_if_T else var = value_if_F
    In Stata, it's
    Code:
    if condition {
        local var value_if_T
    }
    else {
        local var value_if_F
    }
    Can I write a macro to turn this into a one-liner as above?


    Edit: Found that it's possible in two lines:
    Code:
    if (condition) local var value_if_T
    else local var value_if_F
    Last edited by Allen Sirolly; 23 Oct 2015, 00:02.

  • #2
    Code:
    help cond()
    The syntax strcuture is very similar; it is just a question of a different name.

    As you are familiar with this as a programming construct, you won't need a tutorial, but for anyone interested there is http://www.stata-journal.com/sjpdf.h...iclenum=pr0016

    Comment


    • #3
      Great. Thanks.

      Comment

      Working...
      X