Announcement

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

  • variable name abbreviation in generate/replace sequence

    There seems to be some inconsistency about how to abbreviate variable names. I would like to abbreviate DX1, DX2, etc. as DX* in logic similar to that below. Stata is letting me abbreviate in the describe command but not in the replace command. Can someone help or at least explain how I can abbreviate in the replace command? I'm using Stata 14.

    . generate cardiovasc=0

    . replace cardiovasc=1 if DX1=="03282"

    (0 real changes made)

    . drop cardiovasc

    . generate cardiovasc=0

    . replace cardiovasc=1 if DX*=="03282"

    DX* invalid name

    r(198);

    . des DX*

    storage display value

    variable name type format label variable label

    ----------------------------------------------------------------------------------

    DX1 str5 %5s Diagnosis 1

    DX2 str5 %5s Diagnosis 2

    DX3 str5 %5s Diagnosis 3

    DX4 str5 %5s Diagnosis 4

    DX5 str5 %5s Diagnosis 5

    DX6 str5 %5s Diagnosis 6

    DX7 str5 %5s Diagnosis 7

    DX8 str5 %5s Diagnosis 8

    DX9 str5 %5s Diagnosis 9

    DX10 str5 %5s Diagnosis 10

    DX11 str5 %5s Diagnosis 11

    DX12 str5 %5s Diagnosis 12

    DX13 str5 %5s Diagnosis 13

    DX14 str5 %5s Diagnosis 14

    DX15 str5 %5s Diagnosis 15


  • #2
    It is not inconsistent, and it is not the replace command that is the problem, it is your if clause, which would cause an error no matter where you used it.

    DX* is a variable list (see the output of help varlist for further discussion).

    Mathematical operators, such as "+" or "==", require two expressions, one on the left and one on the right. A variable is an expression, a variable list is not.

    Code:
    generate cardiovasc = 0
    foreach d of varlist DX* {
        replace cardiovasc = 1 if `d'=="03282"
    }

    Comment


    • #3
      This was very helpful, thanks. I guess I didn't understand that * implies a variable list.

      Comment

      Working...
      X