Announcement

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

  • How do I refer to a variable that might not be included in the varlist of a program?

    How do I refer to a variable that might not be included in the varlist of a program?

    I have simple program where I want to generate a variable t that es equal to the second argument. If there is no second argument I want t to be equal to the first argument:

    Code:
    cap prog drop try
    prog try
        syntax varlist  
        gen t = `2'
        replace t=`1' if "`2'" == ""
    end
    when I invoke the program with
    Code:
    try var1 var2
    it works all fine. If I try
    Code:
    try var1
    I get invalid syntax.

    I appreciate your help
    Magnus

  • #2
    Try

    Code:
    if "`2'" != "" gen t = `2' 
    else gen t = `1'

    Comment

    Working...
    X