Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: encoding unique letter into a unique number for combined combinations


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: encoding unique letter into a unique number for combined combinations
Date   Wed, 14 Feb 2007 15:54:35 -0000

What you want is a mapping from string to string and 
as such is nothing to do with -encode-. 

Let me assume your mapping is from an alphabet 
"A" ... "Z" to "01" "02" "03" ... "26". 

Then given variable -oldcode- and a desire for -newcode-, you
could go 

clonevar newcode = oldcode

tokenize `c(ALPHA)' 

qui forval i = 1/26 {
	local new : di %02.0f `i'
	replace newcode = substr(newcode, "``i''", "`new'", .)
}

The loop is over i = 1/26. 
The letters "A" ... "Z" are in local macros 1/26. 

An alternative to 
	local new : di %02.0f `i'

is
	local new = cond(`i' < 10, "0`i'", "`i'")

Nick 
[email protected] 

T.J. Volant

> How can I program so that it will encode each unique
> letter into a unique number? Example:
> I got hundreds of combinations of strings like:
> ABC
> ABB
> ACA
> Then, encode only gives me 1,2,3 in order. But I would
> like to label letter A=01, B=02, C=01 etc, and then
> have encode give me 010203, etc. How can I program
> this? 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index