Announcement

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

  • Using r() with recode

    I am trying to recode a variable using the values from the return list but from the error message I am getting it appears that these values cannot be used with the recode command, which I find hard to believe and therefore wonder whether I am not using the r() correctly here:

    #delimit ;
    sysuse auto.dta, clear;
    sum weight, d;
    recode weight (r(min)/r(p25)=1)
    (r(p25)/r(p50)=2)
    (r(p50)/r(p75)=3)
    (r(p75)/r(max)=4),
    gen(weight_qrtls);
    #delimit cr

  • #2
    recode does not evaluate r() contents. You want something like

    Code:
    recode varlist (`r(min)'/`r(p25)'=1) ...
    Best
    Daniel

    Comment


    • #3
      Daniel has pointed out something that I didn't know but have run into several times when trying to use r() or e() in later statements. I don't know if this is clearly documented - I could easily have missed it.
      Thanks!
      Phil

      Comment


      • #4
        Yes, it's documented. For example, go to

        Code:
        help stored results
        and then click through to the manual entry. There it's explained that the syntax ` ' can be used with results like that.

        Saying this kind of thing is irritating as well as informative. Is even anyone supposed to read all the manuals and remember them all? Definitely not. It was possible to read all the manuals in early releases, but now unless there is somebody at StataCorp who still does this, it's not possible for anybody else with a full-time commitment to something else.

        It's irritating the other way round: Even if you know this, where did you first read it? Or did you just spot it in someone's code and realised what it does (which is more than fine!). We don't learn a language by reading a dictionary, except for details, but by reading it and hearing it.

        Often very hard to remember.

        I re-invented a very small trick a little painfully the other day and then discovered it was already documented in the files for my own command. So, there you go.

        Comment

        Working...
        X