Announcement

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

  • rescaling a variable difficulty

    Hi all.
    I have a continuous variable called GLA ranging from 0.10 to 150.0 (and in some cases it was not measured in participants, so was coded as . for those participants)
    I would like to rescale this so it is in quartiles
    I have been having difficulty, as for some reason my code ends up replacing data that is actually missing (.) with a value of 4.

    Here is my syntax: any help at all would be amazing!

    gen gla_quart = .
    replace gla_quart = 1 if gla>=0.1 & gla<=1.0
    replace gla_quart = 2 if gla>=1.1 & gla<3.0
    replace gla_quart = 3 if gla>=3.1 & gla<10.0
    replace gla_quart = 4 if gla>10.1





  • #2
    Welcome to the Stata Forum / Statalist.

    Please do read the FAQ. There you'll find advice about posting data/command/output.

    That said, you may wish to try this:

    Code:
    xtile myvar = GLA, nq(4)
    Best regards,

    Marcos

    Comment


    • #3
      Note that 10.1 is to Stata less than missing. See e.g.

      FAQ . . . . . . . . . . . . . . . . Logical expressions and missing values
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W. Gould
      2/03 Why is x > 1000 true when x contains missing value?
      http://www.stata.com/support/faqs/data-management/
      logical-expressions-and-missing-values/

      With the original code, what about values between 1.0 and 1.1, and so forth?

      Consider this way of thinking about the problem:

      Code:
      gen gla_quart = cond(gla <= 1, 1, cond(gla < 3,  2,  cond(gla < 10, 3,  4))) if gla < .
      That way, the if condition peels off observations with missing on gla and the others are classified with the rules

      if <= 1 return 1
      otherwise if < 3 return 2
      otherwise if <10 return 3
      otherwise return 4.

      The recipe is the important detail here. Feel free to change < to <= or vice versa and cut-off values according to circumstance.

      Comment

      Working...
      X