Bill TP
Bill TP

Reputation: 1097

Stata: How to top-code a variable

Using the auto.dta data, I want to top-code the PRICE variable at the median of top X%. For example, X% could be 3%, 4%, etc. How can I do this in Stata?

Upvotes: 1

Views: 3094

Answers (1)

Metrics
Metrics

Reputation: 15458

In answering your question, I am assuming that you want to replace all the values above, say top 10%, with value say X(top 90% in the following code).

Here is the sample code:

program topcode
    sysuse auto, clear
    pctile pct =  price, nq(10)
    dis r(r9) 
    gen newprice=price
    replace newprice=r(r9) if newprice>r(r9)
end 

Upvotes: 2

Related Questions