Announcement

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

  • cond() function

    Hi all!

    I have the following dilemma. I would like to create variables such that:
    1. if a local takes on a certain name (i.e. price) the variable is created, otherwise it is not.
    2. if a local takes on a certain name (i.e. price) the variable takes on a value otherwise another one.
    I am doing this in order to perform regressions inside a loop. Specifically. as the code below suggests, I would like to perform regressions on "sales, stdunits and price". In particular having the local:
    Code:
    local expl "sales stdunits price"
    in this step,

    Code:
    xtdata tasso_crescita_`k'_prod log_sales_lag `rec'_lag1 `rec'_lag2 `rec'_lag3 dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 mean_gr_rate_atc2 ageprodcat4 ageprodcat3 ageprodcat2 ageprodcat1 recalls_`k' newmolfirm newmolmarket, fe clear
    
    reg tasso_crescita_`k'_prod log_`k'_lag mean_gr_rate_atc2 `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rate_atc2 recalls_`k' ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket dumYear*
    
    estimates store reg_product_`rec'_`k'
    I would like to perform for sales and stdunits (I replace `k' just with sales but this remains valid for stdunits too):
    Code:
    xtdata tasso_crescita_sales_prod log_sales_lag `rec'_lag1 `rec'_lag2 `rec'_lag3 dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 mean_gr_rate_atc2 ageprodcat4 ageprodcat3 ageprodcat2 ageprodcat1 recalls_sales newmolfirm newmolmarket, fe clear
    
    reg tasso_crescita_sales_prod log_sales_lag mean_gr_rate_atc2 `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rate_atc2_sales recalls_sales  ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket dumYear*
    
    estimates store reg_product_`rec'_sales
    and for "price

    Code:
    xtdata price log_sales_lag `rec'_lag1 `rec'_lag2 `rec'_lag3 dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 mean_gr_rate_atc2 ageprodcat4 ageprodcat3 ageprodcat2 ageprodcat1  newmolfirm newmolmarket, fe clear
    
    reg price log_sales_lag mean_gr_rate_atc2 `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rate_atc2_price  ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket dumYear*
    
     estimates store reg_product_`rec'_sales
    (hence price instead of tasso_crescita_`k'_prod, no variable recalls_sales in the reg with price and mean_gr_rate_atc2_`k' defined as mean(price), by(idatc2 Year) for price and mean(tasso_crescita_`k'_prod), by(idatc2 Year)) for sales and stdunits).

    I am running a loop through local expl "sales stdunits price". To discriminate the "price" and "sales, stdunits" regressions I am using "cond()" to define variables, but it does not seem to work (I am doing something wrong). Specifically:

    Code:
    foreach rec of local recall {
    foreach k of local expl {
    
            *PRODUCT
            use "/Users/federiconutarelli/Desktop/PhD/Lavoro_Riccaboni/DATi_USA/Pharma_project_2019 /abnormal_values/DB_product_`rec'_aggregation_nomiss.dta", clear
            
            * STEP 3: generare AV_t = y_it - E(y_it):
            eststo clear
            xtset idpr Year
            rename (tot_sales) (sales)
            gen `k'_lag = L.tot_`k'
            egen tasso_crescita_`k'_prod = ln(`k' - `k'_lag)
            gen log_`k' = ln(`k')
            gen recalls_lagged = L.`rec'
            gen log_`k'_lagged = L.log_`k'
            egen recalls_`k' = cond(`k'=="price",., recalls_lagged*log_`k'_lagged)
    
            replace ageprod = ageprod + 1
            gen ageprod_cat = .
            replace ageprod_cat = 1 if ageproduct <= 5
            replace ageprod_cat = 2 if ageproduct >= 6 & ageproduct <= 10
            replace ageprod_cat = 3 if ageproduct >= 11 & ageproduct <= 30
            replace ageprod_cat = 4 if ageproduct >= 31 & ageproduct < .
            tab ageprod_cat*, gen(ageprodcat)
    
            *Creating average growth rate of sales in ATC2 market:
            egen mean_gr_price = mean(price), by(idatc2 Year)
            egen mean_gr_rateatc2_`k' = cond(`k'=="price",mean_gr_price, (mean(tasso_crescita_`k'_prod), by(idatc2 Year)))
            *save "/Users/federiconutarelli/Desktop/DB_product_`rec'_`k'_nomiss.dta", replace
            
            * ESTIMATES WITH RECALLS GENERALI:
            gen log_`k'_lag = L.log_`k'
            gen `rec'_lag1 = L.`rec'
            gen `rec'_lag2 = L2.`rec'
            gen `rec'_lag3 = L3.`rec'
            xi i.Year, pre(dum)
            
            * E(Y-it):
            local vart
            su ritiri, meanonly
            quietly forval j = 1/`r(max)' {
                local vart `vart' rescaled`j'
                }
            
            local keep `vart' ritiri tasso_crescita_`k'_prod idproduct dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 log_`k'_lagged log_`k' `rec' `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rate_atc2 recalls_`k' ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket Year  
            ds
            local vars `r(varlist)'
            local tokeep : list vars & keep
    
            keep `tokeep'
            
            *HERE I WOULD LIKE TO DISCRIMINATE BETWEEN "price" REGRESSION AND "sales stdunits" REGRESSIONS!
            local keep_no_rescaled  tasso_crescita_`k'_prod idproduct dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 log_`k'_lag log_`k' `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rate_atc2 recalls_`k' ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket Year  
            * Keeping the observations that are not missing:
            foreach v of var `keep_no_rescaled' {
                drop if missing(`v')
            }
            
            
            gen id_unico = _n
            egen y_`k' = cond(`k'=="price",price,tasso_crescita_`k'_prod)
            
            save "/Users/federiconutarelli/Desktop/PhD/Lavoro_Riccaboni/DATi_USA/Pharma_project_2019 /abnormal_values/DB_product_`rec'_`k'_nomiss.dta", replace
    
            xtdata y_`k' log_sales_lag `rec'_lag1 `rec'_lag2 `rec'_lag3 dumYear_2005 dumYear_2006 dumYear_2007 dumYear_2008 dumYear_2009 dumYear_2010 dumYear_2011 dumYear_2012 dumYear_2013 dumYear_2014 dumYear_2015 mean_gr_rateatc2_`k' ageprodcat4 ageprodcat3 ageprodcat2 ageprodcat1 recalls_`k' newmolfirm newmolmarket, fe clear
            reg y`k' log_`k'_lag mean_gr_rate_atc2 `rec'_lag1 `rec'_lag2 `rec'_lag3 mean_gr_rateatc2_`k' recalls_`k' ageprodcat1 ageprodcat2 ageprodcat3 ageprodcat4 newmolfirm newmolmarket dumYear*
            estimates store reg_product_`rec'_`k'
        
            clear
        }
    }
    Am I doing something wrong? (Specifically in the steps of variables definition, e.g.
    Code:
    egen mean_gr_price = mean(price), by(idatc2 Year)
    egen mean_gr_rateatc2_`k' = cond(`k'=="price",mean_gr_price, (mean(tasso_crescita_`k'_prod), by(idatc2 Year)))
    or
    Code:
    egen recalls_`k' = cond(`k'=="price",., recalls_lagged*log_`k'_lagged)
    )


    Thank you very much!

    Federico
    Last edited by Federico Nutarelli; 11 Nov 2019, 02:33.

  • #2
    The first obvious problem is that "cond()" does not work with "egen" but only with "gen". The second problem is that "cond()" does not accept a string comparison as its first argument. See the help file under "help cond()".
    Therefore, I would replace "cond()" with another call.
    Code:
    gen recalls_`k' = recalls_lagged*log_`k'_lagged  if  "`k'" !="price"
    If I am not completely mistaken then that should give the desired result.
    You can replace the other calls to "cond()" with similar calls to "gen" and "replace".

    Comment


    • #3
      Originally posted by Sven-Kristjan Bormann View Post
      The second problem is that "cond()" does not accept a string comparison as its first argument.
      Not so!

      Code:
      . display cond("foo" == "foo", "bar", "foobar")
      bar
      The problem is that

      Code:
      `k'=="price"
      should be

      Code:
      "`k'"=="price"
      note the use of double-quotes.

      Best
      Daniel

      Comment


      • #4
        Thank you both for the reply!

        Appreciated a lot!

        Daniel, indeed I made what you suggested, just, being price actually a variable in the code I put directly

        Code:
        `k'==price
        So basically I removed double quotes also to price.
        Do you think it's the same?

        Thank you again!

        Comment


        • #5
          There was too much code in #1 to be tempting, but I have to point out that egen is compatible with cond() in so far as cond() can be part of an expression and many egen functions allows expressions as arguments. .

          Examples in https://www.stata-journal.com/articl...article=dm0055 and indeed in many other places.

          Comment


          • #6
            Thank you Nick. I will go and check out since both egen and cond() ar very much widespread in Stata!

            Comment


            • #7
              daniel klein and Nick Cox Thanks for pointing out, where my answer was incorrect. I learned something from your corrections. Next time I check my answer more carefully before posting.

              Comment

              Working...
              X