Announcement

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

  • How to change a trinary variable into a binary with label in 1 line

    Hello.

    I have to recode a trinary variable "a6a" : 1=small, 2=medium, 3=large. Into a binary variable "Size", 1=small, 0=medium-large. But I also want that the variable "Size" displays the label "small" and "medium-marge" for either 0 or 1 values.

    I have this code, which works well :
    Code:
    gen byte size=a6a==1
    label define sizelbl 0 "medium-large" 1 "small"
    label values size sizelbl
    But my question is : Can I transform this 3-lines code into a 1-line code ?
    Thanks

  • #2
    You could write a program that does just that, tales a line of input and does all that you say. But the syntax would be no easier to understand than Stata's official commands, or so I surmise.

    Although I love finding more concise ways to do things, it is a simple but fundamental truth that highly concise, compressed code is often harder to write and to read and thus much more error-prone.

    I once spent a lot of time diverted by a language called J in which, as I recall,

    Code:
    mean =. +/ % #
    is an entire program for calculating the mean. It was a language devised by extraordinarily smart people for very smart people able and willing to learn it and use it daily, which is why it never conquered the world.

    Comment


    • #3
      Not tested but this should work

      Code:
      recode a6a (1 = 1 "small") (2/3 = 0 "medium-large") (else = .) , generate(size) label(sizelbl)
      I do not really see why you would insist on a one-liner, but that is your choice, of course. Arguably, this line above is still both easy to understand and write.

      Best
      Daniel

      Comment

      Working...
      X