Announcement

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

  • Help with code: generate variable with if statement

    Hi all,

    I need to determine cumulative frequencies (in relative terms) for WTP (willingness to pay) variable.

    I am using the following code but something is not right. I get error message that "if not found".

    Code:
    gen demand_oe = sum(c_oe) / $N if c_oe != .
    This is what my data looks like

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(id P_OE) float c_oe
     72 200  .
     42 200  .
    302 200  .
     40 200  .
    184 200  .
     78 199  2
    349 199  2
    192 175  3
     49 175  3
     45 175  3
    272 155  1
    370 150  9
    149 150  9
    317 150  9
     41 150  9
    322 150  9
    173 150  9
    186 150  9
    105 150  9
    293 150  9
    277 145  3
    250 145  3
     53 145  3
     58 130  1
    235 125  6
     64 125  6
    139 125  6
    152 125  6
    111 125  6
    315 125  6
    342 120  5
    167 120  5
    171 120  5
    221 120  5
    166 120  5
    176 115  1
    368 110  5
    183 110  5
     74 110  5
     54 110  5
    206 110  5
    236 109  1
    242 105  2
    329 105  2
    351 100 16
    163 100 16
    337 100 16
    231 100 16
     87 100 16
     39 100 16
     37 100 16
    158 100 16
     30 100 16
     27 100 16
    328 100 16
    232 100 16
    381 100 16
    165 100 16
    204 100 16
    154 100 16
    210  99  2
    271  99  2
    313  95  2
    153  95  2
    216  90  9
    175  90  9
    289  90  9
    188  90  9
    276  90  9
     36  90  9
    279  90  9
    269  90  9
    115  90  9
    164  85  5
     84  85  5
    103  85  5
    114  85  5
    162  85  5
    308  80 14
    373  80 14
    241  80 14
    214  80 14
    382  80 14
    356  80 14
    287  80 14
    288  80 14
    346  80 14
    104  80 14
     28  80 14
    319  80 14
    350  80 14
    197  80 14
    281  79  2
    256  79  2
    109  75 12
    145  75 12
    156  75 12
     89  75 12
    142  75 12
    146  75 12
    end

    and this is how the c_oe was created:

    Code:
    ** determine the frequencies for each level stated in the OE format
    
    bysort P_OE: egen c_oe = count(P_OE) 
    
    
    
    
    ** only keep counts for the first observation per price level
    
    replace c_oe = . if P_OE == P_OE[_N-1]
    
    
    
    
    ** descendingly sort WTP OE data
    
    gsort - P_OE
    
    
    
    
    ** determine cumulative frequencies (in relative terms) for WTP in OE
    
    gen demand_oe = sum(c_oe) / $N if c_oe != .
    
    drop c_oe
    Could someone highlight what I am doing wrong?

    Any help is greatly appreciated.
    Thanks

  • #2
    Where does $N come from? You don't show any place where you defined it, and it is not a system global macro. If it is undefined, then Stata will interpret $N as an empty string. In that case your command reads as -gen demand_oe = sum(c_oe) / if c_oe != .- . And Stata, naturally, expects whatever follows / to be a variable. But there is no variable named if.

    Comment

    Working...
    X