Announcement

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

  • Conditional variable generation

    Hello,

    I’m struggling with generating a variable conditional on the outcome of another variable.
    I created a dummy variable which gets value of 1 when day of a month is 1 to 15 and 0 otherwise. I would like a new variable to get lagged value of umich (already created) if day of a month is between 1 and 15 (dummy == 1) and current value of umich when day of month is greater than 15 (dummy == 0).
    I experimented with egen and xi, but can’t figure it out by myself.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 ticker double(date umich) float(lag_umich day first_half)
    "AAPL" 13165 105.8 102.4 17 0
    "AAPL" 13257 107.8 105.4 18 0
    "AAPL" 13348 107.5 105.4 18 0
    "AAPL" 13439 106.6   102 17 0
    "AAPL" 13530 106.8 104.9 16 0
    "AAPL" 13621 115.2 109.8 17 0
    "AAPL" 13712   114 113.2 17 0
    "AAPL" 13803 109.8 114.1 16 0
    "AAPL" 13894 113.5 111.4 15 1
    "AAPL" 13985 115.5 113.7 16 0
    "AAPL" 14076 113.3 115.4 16 0
    "AAPL" 14166 112.8 111.7 14 1
    end
    format %td date
    Thank you for your time!

    Frank

  • #2
    Hi Frank,

    I think this piece of code should do the trick:
    Code:
    gen new_umich = umich
    replace new_umich = lag_umich if day < 16
    Please, le tme know if this works.

    Comment


    • #3
      Frank:
      I'm not sure I got your question right.
      Hence, a temptative reply follows:
      Code:
      . g new_var=lag_umich if first_half==1
      
      . replace new_var= umich if first_half==0
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        This was indeed the correct solution and much easier than I had thought. Thanks a lot!

        Comment

        Working...
        X