Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: re: generating a new numerical variable3 using a string variable2 which is nested in a numerical variable1


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: re: generating a new numerical variable3 using a string variable2 which is nested in a numerical variable1
Date   Sat, 14 Nov 2009 10:57:16 -0500

<>
Assumes string variable is lowercase. Easy to modify that with lower(), upper(), etc.

clear 
input var1 str1 var2
1 a
1 a
1 a
1 b
2 m
2 m
2 n
2 n
2 n
2 p
3 c
3 c
3 c
3 c
end
local alpha = subinstr("`c(alpha)'", " ", "", .)
g av2 = strpos("`alpha'", var2)
egen t = tag(var1 av2)
egen av3 = rank(av2) if t, by(var1)
bys var1 av2: egen av4 = mean(av3)
l


Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                              An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
   An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


*
*   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/



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