Announcement

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

  • replace values?

    Dear All, I have this data set:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id split) byte(zz1 zz2 zz3 zz4 zz5 zz6 other1 other2 other3 other4 other5 other6)
     1 1 0 0 0 1 0 0 0 0 0 0 0 1
     2 0 0 0 0 0 0 1 0 0 0 0 0 1
     3 0 0 0 0 0 0 1 0 0 0 0 0 1
     4 0 0 0 0 0 0 1 0 0 0 0 0 1
     5 1 0 0 0 0 1 0 0 0 1 0 0 0
     6 0 0 0 0 0 0 1 0 0 0 0 0 1
     7 0 1 0 0 0 0 0 1 0 0 0 0 0
     8 0 0 0 0 0 0 1 0 0 0 0 0 1
     9 0 0 0 0 0 0 1 0 0 0 0 0 1
    10 0 0 0 0 0 0 1 0 0 0 0 0 1
    end
    How can I replace 1 in zz* and other* with 0.5 when split=1? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Code:
    foreach x of varlist zz1-zz6 other1-other6{
        replace `x' = 0.5 if `x' == 1 & split == 1
    }

    Comment


    • #3
      Code:
      recode zz* other* (1=.5) if split==1
      Code:
      . recode zz* other* (1=.5) if split==1
      (0 changes made to zz1)
      (0 changes made to zz2)
      (0 changes made to zz3)
      (1 changes made to zz4)
      (1 changes made to zz5)
      (0 changes made to zz6)
      (0 changes made to other1)
      (0 changes made to other2)
      (1 changes made to other3)
      (0 changes made to other4)
      (0 changes made to other5)
      (1 changes made to other6)
      Note that the affected variables are recast from byte to float.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id split) byte(zz1 zz2 zz3) float(zz4 zz5) byte(zz6 other1 other2) float other3 byte(other4 other5) float other6
       1 1 0 0 0 .5  0 0 0 0  0 0 0 .5
       2 0 0 0 0  0  0 1 0 0  0 0 0  1
       3 0 0 0 0  0  0 1 0 0  0 0 0  1
       4 0 0 0 0  0  0 1 0 0  0 0 0  1
       5 1 0 0 0  0 .5 0 0 0 .5 0 0  0
       6 0 0 0 0  0  0 1 0 0  0 0 0  1
       7 0 1 0 0  0  0 0 1 0  0 0 0  0
       8 0 0 0 0  0  0 1 0 0  0 0 0  1
       9 0 0 0 0  0  0 1 0 0  0 0 0  1
      10 0 0 0 0  0  0 1 0 0  0 0 0  1
      end
      Last edited by William Lisowski; 16 Apr 2022, 10:53.

      Comment


      • #4
        Dear Ken, Thanks. It worked.
        Ho-Chuan (River) Huang
        Stata 17.0, MP(4)

        Comment


        • #5
          Dear William, Thanks also for this helpful suggestion.
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment

          Working...
          X