Announcement

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

  • Copying/Replacing values from another variable

    Hi statalist,
    I want to replace values of (month_2 * daylight_hours) with (month_1 * daylight_hours). For instance, instead of (11 to 20), it gives me values from (1 to 10). Is there any way to replace these values directly? Thanks in advance!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(daylight_hours month_1 month_2 month_1Xhours month_2Xhours)
     1 1 0  1  0
     2 1 0  2  0
     3 1 0  3  0
     4 1 0  4  0
     5 1 0  5  0
     6 1 0  6  0
     7 1 0  7  0
     8 1 0  8  0
     9 1 0  9  0
    10 1 0 10  0
    11 0 1  0 11
    12 0 1  0 12
    13 0 1  0 13
    14 0 1  0 14
    15 0 1  0 15
    16 0 1  0 16
    17 0 1  0 17
    18 0 1  0 18
    19 0 1  0 19
    20 0 1  0 20
    end
    Last edited by Steven Ji; 09 Dec 2022, 10:45.

  • #2
    maybe,
    Code:
    replace month_2Xhours = month_2Xhours - 10 if month_2

    Comment


    • #3
      Hello Steven Ji. I imagine there are many ways of doing what you asked. But they all seem rather ad hoc. I think it would be very helpful if you provided a bit more context. For example, if the first variable is indeed the number of daylight hours, and the values 11-20 are correct, why would you want a variable called month2Xhours to be equal to some value that is not month_2 * daylight_hours? This suggests to me some kind of problem with the way your data file is set up.

      Cheers,
      Bruce
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        Hi @Bruce Weaver. Thank you for your reply.
        Yes, they are indeed the number of daylight hours. This is not actual data, I just created a sample data here. Actually, we need to see the effect of month2Xhours if the daylight hours in month 2 are kept the same as in month 1.
        I hope this answers your question. Let me know if you need more clarification. Thanks again!

        Comment


        • #5
          Thanks Steven. If the patter shown in the data sample you provided holds in the actual dataset, then I suppose you could use the mod() function to generate a new variable called m1hours. Something like this:

          Code:
          * Duplicate the data sample that was provided,
          * but with shorter variable names!
          clear
          set obs 20
          generate byte hours = _n
          generate byte m1 = _n < 11
          generate byte m2 = _n > 10
          generate byte m1Xhours = m1*hours
          generate byte m2Xhours = m2*hours
          
          * In the example that was provided, m1hours can be obtained
          * using the mod() function. But note that I have no idea
          * if this will work in the actual dataset!
          
          generate byte m1hours = mod(hours,10)
          replace m1hours = 10 if m1hours==0
          generate m2Xm1hours = m2*m1hours
          list, clean ab(12)
          Output:
          Code:
          . list, clean ab(12)  
          
                 hours   m1   m2   m1Xhours   m2Xhours   m1hours   m2Xm1hours  
            1.       1    1    0          1          0         1            0  
            2.       2    1    0          2          0         2            0  
            3.       3    1    0          3          0         3            0  
            4.       4    1    0          4          0         4            0  
            5.       5    1    0          5          0         5            0  
            6.       6    1    0          6          0         6            0  
            7.       7    1    0          7          0         7            0  
            8.       8    1    0          8          0         8            0  
            9.       9    1    0          9          0         9            0  
           10.      10    1    0         10          0        10            0  
           11.      11    0    1          0         11         1            1  
           12.      12    0    1          0         12         2            2  
           13.      13    0    1          0         13         3            3  
           14.      14    0    1          0         14         4            4  
           15.      15    0    1          0         15         5            5  
           16.      16    0    1          0         16         6            6  
           17.      17    0    1          0         17         7            7  
           18.      18    0    1          0         18         8            8  
           19.      19    0    1          0         19         9            9  
           20.      20    0    1          0         20        10           10
          But as I said in a comment line, I have no idea whether this will work as desired in your actual dataset.
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            Hi @Bruce Weaver. Thank you so much!

            Comment

            Working...
            X