Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: recoding variables with different min & max into standard scale

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: recoding variables with different min & max into standard scale Date Wed, 2 Feb 2011 17:49:30 +0000

```foreach v of var <varlist> {
su `v', meanonly
gen new_`v' = (`v' - r(min))/(r(max) - r(min))
}

There's also a dedicated -egen- function from Stas Kolenikov:

_gstd01 from http://web.missouri.edu/~kolenikovs/stata
_gstd01 -- Standardize to [0,1] / / Author: Stas Kolenikov,
skolenik@recep.glasnet.ru / This program is an extension to the egen
command / that standardize the specified variable into [0,1] range / so
that 0 corresponds to the minimum value, and 1, / to the maximum

Nick
n.j.cox@durham.ac.uk

Daniel Laurison

I have a couple hundred variables all with different ranges, and I
just need to get them all to be 0-1 (not dummies, but continuous with
the min value 0 and max value 1 and everything in between keeping its
original proportions; I'd be just as happy with 0-100 or etc.).  I
don't need ranks or "standardized" i.e. egen std(var) values.  The
mathematical formula is simply [var- min(var)]/[max(var)-min(var)] but
I can't figure out how to make Stata do that; I know I could get the
ranks, but I want to keep the relative values.  My workaround is to
egen the constant max & min of each var, and then do a foreach loop
with the maxvars & minvars for each var in the formula... but there
has to be a neater, more elegant solution, right?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```