Announcement

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

  • Generate new variable out of a rowmean variable

    Hello,

    currently I'm trying to build a new variable out of an already existing one.
    The existing one needs to be recoded so that the direction of the value is changed (So that 5 equals 1 etc., 4 equals 2 etc.).
    Also, I want to round up the values to numeric numbers without decimal places.

    *The existing variable
    egen help=rownonmiss(pcr1i3 pcr1i8 pcr1i11)
    egen negcomm_pacs=rowmean(pcr1i3 pcr1i8 pcr1i11)
    if inlist(help,2,3)
    drop help

    label var negcomm_pacs "Negative communication in parenting style"
    label def negcomm_pacs 1 "1 low" 5 "5 high"
    label val negcomm_pacs negcomm_pacs


    Negative |
    communicatin |
    on | Freq. Percent Cum.
    ------------+-----------------------------------
    1 | 11 1.43 1.43
    1.333333 | 27 3.52 4.95
    1.666667 | 63 8.21 13.17
    2 | 134 17.47 30.64
    2.333333 | 183 23.86 54.50
    2.5 | 1 0.13 54.63
    2.666667 | 144 18.77 73.40
    3 | 118 15.38 88.79
    3.333333 | 46 6.00 94.78
    3.666667 | 23 3.00 97.78
    4 | 15 1.96 99.74
    4.333333 | 1 0.13 99.87
    4.666667 | 1 0.13 100.00
    ------------+-----------------------------------
    Total | 767 100.00



    *Command for the new variable, which doesn't work
    gen negcomm = negcomm_pacs
    replace negcomm = 5 if (negcomm_pacs==.6666667/1.333333) //
    replace negcomm = 4 if (negcomm_pacs==1.666667/2.333333) //
    replace negcomm = 3 if (negcomm_pacs==2.666667/3.333333) //
    replace negcomm = 2 if (negcomm_pacs==3.666667/4.333333) //
    replace negcomm = 1 if (negcomm_pacs==4.666667/5) //
    replace negcomm = 0 if (negcomm_pacs==-2)


    Do you have any idea, why this command doens't work?

    Thanks in advance!

  • #2
    See

    Code:
    help inrange
    For example:

    Given that \(4\frac{2}{3}\) is \(\frac{14}{3}\):


    Code:
    replace negcomm = 1 if inrange(negcomm_pacs,`=14/3', 5)

    Comment


    • #3
      There is no need for and no advantage in using the local macro persona of e..g 14/3.

      Code:
      if inrange(negcomm_pacs, 14/3, 5)
      should work as well as anything else.

      Comment


      • #4
        Thanks for the answers.

        I tried the code:

        gen negcomm = negcomm_pacs //
        replace negcomm = 5 if inrange(negcomm_pacs, 14/3, 5) //
        replace negcomm = 4 if inrange(negcomm_pacs, 14/3, 4) //
        replace negcomm = 3 if inrange(negcomm_pacs, 14/3, 3) //
        replace negcomm = 2 if inrange(negcomm_pacs, 14/3, 2) //
        replace negcomm = 1 if inrange(negcomm_pacs, 14/3, 1) //

        unfortunately this didn't work out for me and the numbers still have decimal places

        Comment


        • #5
          Not sure what you are doing here. You have different limits defined in #1

          Code:
          replace negcomm = 5 if inrange(negcomm_pacs, 2/3, 4/3)
          replace negcomm = 4 if inrange(negcomm_pacs, 5/3,7/3)
          But note that there are gaps in your intervals, e.g., between 4/3 and 5/3. If you provide a data example using dataex following FAQ Advice #12, you can get a better answer.

          Comment


          • #6
            The code won't be 14/3 for all cases.

            Does this help?

            Code:
             clear
            
            . set obs 12 
            Number of observations (_N) was 0, now 12.
            
            . range numer 3 14 
            
            . 
            . gen problem = numer/3 
            
            . 
            . tab problem
            
                problem |      Freq.     Percent        Cum.
            ------------+-----------------------------------
                      1 |          1        8.33        8.33
               1.333333 |          1        8.33       16.67
               1.666667 |          1        8.33       25.00
                      2 |          1        8.33       33.33
               2.333333 |          1        8.33       41.67
               2.666667 |          1        8.33       50.00
                      3 |          1        8.33       58.33
               3.333333 |          1        8.33       66.67
               3.666667 |          1        8.33       75.00
                      4 |          1        8.33       83.33
               4.333333 |          1        8.33       91.67
               4.666667 |          1        8.33      100.00
            ------------+-----------------------------------
                  Total |         12      100.00
            
            . 
            . gen solution = 6 - round(problem, 1) 
            
            . 
            . table problem solution 
            
            ---------------------------------------
                       |           solution        
                       |  1   2   3   4   5   Total
            -----------+---------------------------
            problem    |                           
              1        |                  1       1
              1.333333 |                  1       1
              1.666667 |              1           1
              2        |              1           1
              2.333333 |              1           1
              2.666667 |          1               1
              3        |          1               1
              3.333333 |          1               1
              3.666667 |      1                   1
              4        |      1                   1
              4.333333 |      1                   1
              4.666667 |  1                       1
              Total    |  1   3   3   3   2      12
            ---------------------------------------

            Comment


            • #7
              Thanks for your answers. I tried solution #5 and it worked.

              Code:
              replace negcomm = 4 if inrange(negcomm_pacs, 5/3, 7/3) //
              replace negcomm = 3 if inrange(negcomm_pacs, 8/3, 10/3) //
              replace negcomm = 2 if inrange(negcomm_pacs, 11/3, 13/3) //
              replace negcomm = 1 if inrange(negcomm_pacs, 14/3, 15/3) //

              However, as you assumed, there are
              some gaps. Therefore, I tried to give you some data example via dataex, of the original variable negcomm_pacs


              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input long id int wave float negcomm_pacs
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float negcomm_pacs
              2
              1.6666666
              3.666667
              2
              2.3333333
              1.6666666
              1.6666666
              2.3333333
              1.3333334
              1
              3.333333
              1.6666666
              1.6666666
              2
              3
              2
              1.6666666
              2
              2
              2
              2
              2
              2
              2.666667
              2
              2.3333333
              2
              1.6666666
              2
              1.6666666
              1.6666666
              1.6666666
              3.333333
              4
              4
              3
              2.3333333
              3
              3.666667
              3.333333
              2.666667
              2.3333333
              2.666667
              2.3333333
              1.6666666
              2.666667
              2
              2.3333333
              3
              3
              2.666667
              3
              3
              3
              3
              2.3333333
              2.3333333
              2
              2
              2
              2
              2.3333333
              2
              2.3333333
              2.3333333
              1.6666666
              2.3333333
              2
              2.3333333
              3
              3.333333
              3.333333
              2.666667
              2.666667
              2
              3
              3
              2.666667
              2.3333333
              2.3333333
              2.666667
              3.666667
              2.3333333
              2.3333333
              3
              2.666667
              2.3333333
              2.3333333
              1.6666666
              2.666667
              2
              2.3333333
              2.666667
              2.3333333
              2
              2.3333333
              2.3333333
              2
              1.6666666
              2.3333333
              end
              [/CODE]

              Comment


              • #8
                ADDED IN EDIT: Actually, #6 does exactly what you want with an adjustment for zero values.

                Borrowing from Nick's code in #6 and your intervals in #1, the following does what you want:

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float negcomm_pacs
                        2
                1.6666666
                 3.666667
                        2
                2.3333333
                1.6666666
                1.6666666
                2.3333333
                1.3333334
                        1
                 3.333333
                1.6666666
                1.6666666
                        2
                        3
                        2
                1.6666666
                        2
                        2
                        2
                        2
                        2
                        2
                 2.666667
                        2
                2.3333333
                        2
                1.6666666
                        2
                1.6666666
                1.6666666
                1.6666666
                 3.333333
                        4
                        4
                        3
                2.3333333
                        3
                 3.666667
                 3.333333
                 2.666667
                2.3333333
                 2.666667
                2.3333333
                1.6666666
                 2.666667
                        2
                2.3333333
                        3
                        3
                 2.666667
                        3
                        3
                        3
                        3
                2.3333333
                2.3333333
                        2
                        2
                        2
                        2
                2.3333333
                        2
                2.3333333
                2.3333333
                1.6666666
                2.3333333
                        2
                2.3333333
                        3
                 3.333333
                 3.333333
                 2.666667
                 2.666667
                        2
                        3
                        3
                 2.666667
                2.3333333
                2.3333333
                 2.666667
                 3.666667
                2.3333333
                2.3333333
                        3
                 2.666667
                2.3333333
                2.3333333
                1.6666666
                 2.666667
                        2
                2.3333333
                 2.666667
                2.3333333
                        2
                2.3333333
                2.3333333
                        2
                1.6666666
                2.3333333
                end
                
                gen wanted=  6 - (int(negcomm_pacs) + (negcomm_pacs-int(negcomm_pacs)>0.5))
                replace wanted=-2 if !negcomm_pacs
                Res.:

                Code:
                . sort wanted negcomm_pacs
                
                . l, sepby(wanted)
                
                     +-------------------+
                     | negcom~s   wanted |
                     |-------------------|
                  1. | 3.666667        2 |
                  2. | 3.666667        2 |
                  3. | 3.666667        2 |
                  4. |        4        2 |
                  5. |        4        2 |
                     |-------------------|
                  6. | 2.666667        3 |
                  7. | 2.666667        3 |
                  8. | 2.666667        3 |
                  9. | 2.666667        3 |
                 10. | 2.666667        3 |
                 11. | 2.666667        3 |
                 12. | 2.666667        3 |
                 13. | 2.666667        3 |
                 14. | 2.666667        3 |
                 15. | 2.666667        3 |
                 16. | 2.666667        3 |
                 17. | 2.666667        3 |
                 18. |        3        3 |
                 19. |        3        3 |
                 20. |        3        3 |
                 21. |        3        3 |
                 22. |        3        3 |
                 23. |        3        3 |
                 24. |        3        3 |
                 25. |        3        3 |
                 26. |        3        3 |
                 27. |        3        3 |
                 28. |        3        3 |
                 29. |        3        3 |
                 30. |        3        3 |
                 31. | 3.333333        3 |
                 32. | 3.333333        3 |
                 33. | 3.333333        3 |
                 34. | 3.333333        3 |
                 35. | 3.333333        3 |
                     |-------------------|
                 36. | 1.666667        4 |
                 37. | 1.666667        4 |
                 38. | 1.666667        4 |
                 39. | 1.666667        4 |
                 40. | 1.666667        4 |
                 41. | 1.666667        4 |
                 42. | 1.666667        4 |
                 43. | 1.666667        4 |
                 44. | 1.666667        4 |
                 45. | 1.666667        4 |
                 46. | 1.666667        4 |
                 47. | 1.666667        4 |
                 48. | 1.666667        4 |
                 49. | 1.666667        4 |
                 50. |        2        4 |
                 51. |        2        4 |
                 52. |        2        4 |
                 53. |        2        4 |
                 54. |        2        4 |
                 55. |        2        4 |
                 56. |        2        4 |
                 57. |        2        4 |
                 58. |        2        4 |
                 59. |        2        4 |
                 60. |        2        4 |
                 61. |        2        4 |
                 62. |        2        4 |
                 63. |        2        4 |
                 64. |        2        4 |
                 65. |        2        4 |
                 66. |        2        4 |
                 67. |        2        4 |
                 68. |        2        4 |
                 69. |        2        4 |
                 70. |        2        4 |
                 71. |        2        4 |
                 72. |        2        4 |
                 73. |        2        4 |
                 74. | 2.333333        4 |
                 75. | 2.333333        4 |
                 76. | 2.333333        4 |
                 77. | 2.333333        4 |
                 78. | 2.333333        4 |
                 79. | 2.333333        4 |
                 80. | 2.333333        4 |
                 81. | 2.333333        4 |
                 82. | 2.333333        4 |
                 83. | 2.333333        4 |
                 84. | 2.333333        4 |
                 85. | 2.333333        4 |
                 86. | 2.333333        4 |
                 87. | 2.333333        4 |
                 88. | 2.333333        4 |
                 89. | 2.333333        4 |
                 90. | 2.333333        4 |
                 91. | 2.333333        4 |
                 92. | 2.333333        4 |
                 93. | 2.333333        4 |
                 94. | 2.333333        4 |
                 95. | 2.333333        4 |
                 96. | 2.333333        4 |
                 97. | 2.333333        4 |
                 98. | 2.333333        4 |
                     |-------------------|
                 99. |        1        5 |
                100. | 1.333333        5 |
                     +-------------------+
                Last edited by Andrew Musau; 03 Feb 2022, 10:40.

                Comment

                Working...
                X