Announcement

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

  • How to use Scalar

    Hi All,

    In an earlier question I could not make it so clear what I need.

    I used this code below to get the maximum values I have:

    Code:
    bysort id_com year: gen all=sum(v_local!=v_local[_n-1])
    su all, meanonly
    forval j = 1/`r(max)'{  
        egen v_local`j'=mean(cond(all==`j', v_local, .)), by (id_com year)
    }
    The results were new variables equal to
    Code:
    v_local1,  v_local2, v_local3, and v_local4
    My question is about the "`r(max)'" , how can I use it in the code below:

    Code:
    gen trade_var =1 if domestic == v_local1 | domestic == v_local2 | domestic == v_local3|domestic == v_local4
    replace trade_var==0 if trade_var==.
    which will give me something like this:



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte id_com str1 department byte(v_local domestic) float(all v_local1 v_local2 v_local3 v_local4 trade_var)
    2005 1 "A" 11 11 1 11 12 14 11 1
    2005 1 "A" 11 12 1 11 12 14 11 1
    2005 1 "A" 11 13 1 11 12 14 11 0
    2005 1 "A" 11 14 1 11 12 14 11 1
    2005 1 "B" 12 11 2 11 12 14 11 1
    2005 1 "B" 12 12 2 11 12 14 11 1
    2005 1 "B" 12 13 2 11 12 14 11 0
    2005 1 "B" 12 14 2 11 12 14 11 1
    2005 1 "C" 14 11 3 11 12 14 11 1
    2005 1 "C" 14 12 3 11 12 14 11 1
    2005 1 "C" 14 13 3 11 12 14 11 0
    2005 1 "C" 14 14 3 11 12 14 11 1
    2005 1 "D" 14 11 3 11 12 14 11 1
    2005 1 "D" 14 12 3 11 12 14 11 1
    2005 1 "D" 14 13 3 11 12 14 11 0
    2005 1 "D" 14 14 3 11 12 14 11 1
    2005 1 "E" 11 11 4 11 12 14 11 1
    2005 1 "E" 11 12 4 11 12 14 11 1
    2005 1 "E" 11 13 4 11 12 14 11 0
    2005 1 "E" 11 14 4 11 12 14 11 1
    end

    when I run this code:

    Code:
    bysort id_com year: gen all=sum(v_local!=v_local[_n-1])
    su all, meanonly  
    forval j = 1/`r(max)'{   
        egen v_local`j'=mean(cond(all==`j', v_local, .)), by (id_com year)
    }
    
    
    scalar max = r(max)
    local newvars `newvars'  v_local`j'
     
     
    gen trade_var=1 if domestic ==  v_local`j'
    replace trade_var==0 if trade_var==.
    or
    Code:
    gen trade_var=1 if domestic == `newvars'
    replace trade_var==0 if trade_var==.
    the results are different:


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year byte id_com str1 department byte(v_local domestic) float(all v_local1 v_local2 v_local3 v_local4 trade_var)
    2005 1 "A" 11 11 1 11 12 14 11 1
    2005 1 "A" 11 12 1 11 12 14 11 0
    2005 1 "A" 11 13 1 11 12 14 11 0
    2005 1 "A" 11 14 1 11 12 14 11 0
    2005 1 "B" 12 11 2 11 12 14 11 0
    2005 1 "B" 12 12 2 11 12 14 11 1
    2005 1 "B" 12 13 2 11 12 14 11 0
    2005 1 "B" 12 14 2 11 12 14 11 0
    2005 1 "C" 14 11 3 11 12 14 11 0
    2005 1 "C" 14 12 3 11 12 14 11 0
    2005 1 "C" 14 13 3 11 12 14 11 0
    2005 1 "C" 14 14 3 11 12 14 11 1
    2005 1 "D" 14 11 3 11 12 14 11 0
    2005 1 "D" 14 12 3 11 12 14 11 0
    2005 1 "D" 14 13 3 11 12 14 11 0
    2005 1 "D" 14 14 3 11 12 14 11 1
    2005 1 "E" 11 11 4 11 12 14 11 1
    2005 1 "E" 11 12 4 11 12 14 11 0
    2005 1 "E" 11 13 4 11 12 14 11 0
    2005 1 "E" 11 14 4 11 12 14 11 0
    end
    ------------------ copy up to and including the previous line ------------------


    Please let me know if I should change something in my code above .

    Thanks, Max


  • #2
    First with regard to `r(max)', once you pass the -forval j = 1/`r(max)' {- line of code, `r(max)' is gone. You cannot re-access it after that because the code inside your loop clobbers it. If you want to preserve the value of `r(max)' with a local macro or scalar. you must do so immediately after the -su all, meanonly- command.

    Next, -gen trade_var=1 if domestic == `newvars'- is just a syntax error. You can't have a list of variables on either side of ==, just one variable on each side.

    So what can you do? You can do the whole thing in the original loop (provided the variable domestic already exists).
    Code:
    bysort id_com year: gen all=sum(v_local!=v_local[_n-1])
    su all, meanonly  
    gen trade_var = 0
    forval j = 1/`r(max)'{   
        egen v_local`j'=mean(cond(all==`j', v_local, .)), by (id_com year)
        replace trade_var = 1 if domestic == v_local`j'
    }
    Note: This code is not tested because you did not show an example of the starting data. I am highly confident it is correct, but without testing, not certain.

    Comment


    • #3
      Thank you very much! it worked well!

      Comment

      Working...
      X